环境
各软件版本
sts : spring-tool-suite-3.7.3.RELEASE-e4.5.2-win32-x86_64
gradle : 2.7
tomcat : 7
以上环境自行安装
gradle 插件配置
sts 的 gradle 插件 安装,你需要打开eclipse市场,然后搜索gradle,会看到如下两个插件,都要安装
创建项目
创建
新建gradle项目,这里要注意,不要选择gradle sts,选上面的
填写你的gradle和jdk路径
修改项目配置
修改项目结构如下:
项目右键属性,做如下修改
如果没有 Project and External Dependentcies 这一项,则点击右侧的Add
选择 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 即可