sts : spring-tool-suite-3.7.3.RELEASE-e4.5.2-win32-x86_64html
gradle : 2.7java
tomcat : 7web
以上環境自行安裝spring
sts 的 gradle 插件 安裝,你須要打開eclipse市場,而後搜索gradle,會看到以下兩個插件,都要安裝apache
新建gradle項目,這裏要注意,不要選擇gradle sts,選上面的api
填寫你的gradle和jdk路徑瀏覽器
修改項目結構以下:spring-mvc
項目右鍵屬性,作以下修改tomcat
若是沒有 Project and External Dependentcies 這一項,則點擊右側的Addmvc
選擇 Java Build Path Entries
選擇,finish
增長Dynamic Web Module 3.0支持,這裏要注意,sts會自動添加一個WebContent目錄,刪除便可
到這裏項目的配置基本上就算完成了,最後的Java Build Path Entries 和 Dynamic Web Module 3.0配置主要是爲了項目能夠運行在tomcat中
編碼參考自這裏:http://blog.csdn.net/jpweb2013/article/details/39673329
個人代碼以下:
build.gradle
apply plugin: 'java' apply plugin: 'war' apply plugin: 'jetty' // JDK 7 sourceCompatibility = 1.7 targetCompatibility = 1.7 repositories { mavenLocal() mavenCentral() jcenter() } String springVersion = '4.0.2.RELEASE' dependencies { compile 'org.slf4j:slf4j-api:1.7.12' // j2ee base providedCompile 'javax.servlet:javax.servlet-api:3.0.1' compile 'javax.servlet:jstl:1.2' // spring core compile ('org.springframework:spring-context:' + springVersion) compile ('org.springframework:spring-core:' + springVersion) compile ('org.springframework:spring-webmvc:' + springVersion) testCompile 'junit:junit:4.12' } jettyRun { httpPort = 8080 contextPath = 'sshone' }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/dispatcherServlet-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <display-name>bookstore</display-name> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> </web-app>
root-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- Root Context: defines shared resources visible to all other web components --> </beans>
dispatcherServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <mvc:annotation-driven /> <!-- 自動掃描該包,使SpringMVC認爲包下用了@controller註解的類是控制器 --> <context:component-scan base-package="com.laolang.ssh" /> <!--對靜態資源文件的訪問 --> <mvc:resources mapping="static/**" location="static/" /> <!-- 定義跳轉的文件的先後綴 ,視圖模式配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
BookTypeController
package com.laolang.ssh.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/goodtypes") public class BookTypeController { @RequestMapping(value="goodtypes",method=RequestMethod.GET,produces="text/html") public String list( Model model ){ System.out.println("goodtype list"); model.addAttribute("msg", "Hello Gradle!"); return "goodtype/list"; } }
建議你把sts的自動build關掉,手動build。
你須要建立一個tomcat server new -> other -> server -> apache -> tomcat
選擇適合你的版本,寫好名字,
而後再把你的項目添加到你建立服務中,手動build,而後debug啓動,在BookTypeController的list方法第一行批一斷點,在瀏覽器中輸入以下地址:
http://localhost:8088/sshone/goodtypes/goodtypes
若是成功,那麼你會看到以下的debug畫面
之因此選擇這種運行方式,是由於gradle的jettyRun只能開,不能關,若是想關閉,要麼在任務管理器中關閉,要麼用命令行運行,並且我不會gradle的debug模式。這種新建一個服務的方式不只能夠爲每一個項目定製tomcat配置,並且能夠進debug模式,就是比起jettyRun有點慢,項目大一點,電腦性能再差點,你可能得等上一下子。解決方法就是 換個電腦 -_-#!
若是遇到 依賴沒法加載的問題,則項目右鍵->gradle->refresh 便可