idea-gradle-springmvc-web
1.首先创建gradle项目,详细参考http://www.jianshu.com/p/c0fc6a91d3e7
2.springmvc的一些配置:
a. build.gradle文件,引入一些必要的包文件
1 group 'Example' 2 version '1.0-SNAPSHOT' 3 4 apply plugin: 'java' 5 apply plugin: 'war' 6 7 sourceCompatibility = 1.5 8 9 repositories { 10 maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } 11 mavenLocal() 12 jcenter() 13 maven { url "http://repo.maven.apache.org/maven2/"} 14 maven { url 'https://repo.spring.io/libs-milestone'} 15 mavenCentral() 16 } 17 18 String springVersion = '4.1.0.RELEASE' 19 String slf4jVersion = '1.7.7' 20 21 configurations.all { 22 resolutionStrategy.eachDependency { DependencyResolveDetails details -> 23 if (details.requested.group == 'org.springframework') { 24 details.useVersion springVersion 25 } 26 } 27 } 28 dependencies { 29 compile "org.apache.commons:commons-lang3:3.4" 30 compile "com.itextpdf:kernel:7.0.2" 31 compile "com.itextpdf:io:7.0.2" 32 compile "com.itextpdf:layout:7.0.2" 33 compile "com.itextpdf:forms:7.0.2" 34 compile "com.itextpdf:pdfa:7.0.2" 35 compile "com.itextpdf:pdftest:7.0.2" 36 compile "com.itextpdf:font-asian:7.0.2" 37 compile "com.itextpdf:sign:7.0.2" 38 //compile "com.itextpdf:typography:1.0.2-SNAPSHOT" 39 compile "org.slf4j:slf4j-log4j12:1.7.18" 40 compile "org.bouncycastle:bcpkix-jdk15on:1.49" 41 compile "org.bouncycastle:bcprov-jdk15on:1.49" 42 testCompile group: 'junit', name: 'junit', version: '4.11' 43 44 //core spring 45 compile ('org.springframework:spring-context:' + springVersion) 46 compile ('org.springframework:spring-core:' + springVersion) 47 compile ('org.springframework:spring-webmvc:' + springVersion) 48 49 //aspects 50 compile 'org.aspectj:aspectjrt:1.8.2' 51 compile 'org.aspectj:aspectjweaver:1.8.2' 52 53 //logging 54 compile ('org.slf4j:jcl-over-slf4j:' + slf4jVersion) 55 compile ('org.slf4j:jul-to-slf4j:' + slf4jVersion) 56 compile ('ch.qos.logback:logback-classic:1.1.2') 57 58 //json 59 compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3' 60 compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13' 61 compile 'org.codehaus.jackson:jackson-core-asl:1.9.13' 62 63 //db access 64 compile 'org.springframework:spring-jdbc:' + springVersion 65 compile 'org.springframework:spring-orm:' + springVersion 66 compile 'org.springframework.data:spring-data-jpa:1.7.0.RELEASE' 67 compile 'org.apache.tomcat:tomcat-jdbc:8.0.9' 68 compile 'org.hsqldb:hsqldb:2.3.2' 69 70 71 //Servlet 72 compile "javax.servlet:jstl:1.2" 73 // https://mvnrepository.com/artifact/javax.servlet/servlet-api 74 compile group: 'javax.servlet', name: 'servlet-api', version: '2.5' 75 76 // https://mvnrepository.com/artifact/org.imgscalr/imgscalr-lib 77 compile group: 'org.imgscalr', name: 'imgscalr-lib', version: '4.2' 78 79 // https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload 80 compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.2' 81 82 // https://mvnrepository.com/artifact/com.google.guava/guava 83 compile group: 'com.google.guava', name: 'guava', version: '18.0' 84 85 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' 86 87 //testing 88 testCompile group: 'junit', name: 'junit', version: '4.11' 89 testCompile "org.mockito:mockito-all:1.9.0" 90 testCompile "org.hamcrest:hamcrest-core:1.3" 91 testCompile("org.spockframework:spock-core:0.7-groovy-2.0") { 92 exclude group: 'org.codehaus.groovy', module: 'groovy-all' 93 } 94 testRuntime("org.spockframework:spock-spring:0.7-groovy-2.0") { 95 exclude group: 'org.spockframework', module: 'spock-core' 96 } 97 testRuntime "cglib:cglib-nodep:2.2.2" 98 testRuntime "org.objenesis:objenesis:1.2" 99 testCompile 'org.springframework:spring-test:' + springVersion 100 testRuntime 'com.jayway.jsonpath:json-path:0.8.1' 101 }
b. spring-mvc.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.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- scan the package and the sub package --> <context:component-scan base-package="com.example"/> <!-- don't handle the static resource --> <mvc:default-servlet-handler /> <!-- if you use annotation you must configure following setting --> <mvc:annotation-driven /> <!-- configure the InternalResourceViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/view/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>
c. 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"> <!--START 设置字符编码过滤器--> <filter> <description>字符集过滤器</description> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <description>字符集编码</description> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--END 设置字符编码过滤器--> <!--configure the setting of springmvcDispatcherServlet and configure the mapping--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <!-- <load-on-startup>1</load-on-startup> --> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/</welcome-file> </welcome-file-list> <session-config> <session-timeout>20</session-timeout> </session-config> </web-app>
3. Controller
package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Created by Administrator on 2017/4/11. */ @Controller @RequestMapping("/example") public class HomeControler { @RequestMapping(value = "", method = RequestMethod.GET) public String helloWorld() { return "home"; } }
4. 访问路径:http://localhost:8080/example
5. 项目目录结构