sts + gradle + tomcat 運行 spring mvc

環境

各軟件版本

sts : spring-tool-suite-3.7.3.RELEASE-e4.5.2-win32-x86_64html

gradle : 2.7java

tomcat : 7web

以上環境自行安裝spring

gradle 插件配置

sts 的 gradle 插件 安裝,你須要打開eclipse市場,而後搜索gradle,會看到以下兩個插件,都要安裝apache

Alt sts gradle 插件

建立項目

建立

新建gradle項目,這裏要注意,不要選擇gradle sts,選上面的api

Alt sts gradle 建立項目01

填寫你的gradle和jdk路徑瀏覽器

Alt sts gradle 建立項目02

修改項目配置

修改項目結構以下:spring-mvc

Alt sts gradle 項目目錄結構

項目右鍵屬性,作以下修改tomcat

Alt sts gradle 項目目錄結構

若是沒有 Project and External Dependentcies 這一項,則點擊右側的Addmvc

Alt sts gradle 建立項目02

選擇 Java Build Path Entries

Alt sts gradle 建立項目02

選擇,finish

Alt sts gradle 建立項目02

增長Dynamic Web Module 3.0支持,這裏要注意,sts會自動添加一個WebContent目錄,刪除便可

Alt sts gradle 建立項目02

到這裏項目的配置基本上就算完成了,最後的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

選擇適合你的版本,寫好名字,

Alt sts gradle server

而後再把你的項目添加到你建立服務中,手動build,而後debug啓動,在BookTypeController的list方法第一行批一斷點,在瀏覽器中輸入以下地址:

http://localhost:8088/sshone/goodtypes/goodtypes

若是成功,那麼你會看到以下的debug畫面

Alt sts gradle debug

結語

之因此選擇這種運行方式,是由於gradle的jettyRun只能開,不能關,若是想關閉,要麼在任務管理器中關閉,要麼用命令行運行,並且我不會gradle的debug模式。這種新建一個服務的方式不只能夠爲每一個項目定製tomcat配置,並且能夠進debug模式,就是比起jettyRun有點慢,項目大一點,電腦性能再差點,你可能得等上一下子。解決方法就是 換個電腦 -_-#!

若是遇到 依賴沒法加載的問題,則項目右鍵->gradle->refresh 便可

相關文章
相關標籤/搜索