关于使用Gradle整合Springmvc构建JavaWeb项目的那点事
2016-01-16 20:42
881 查看
关于项目创建
如果你看到这里,我就假设你对gradke已经有了初步的了解,已经创建好了一个基本的web项目了,如下图你已经有了一个基本的项目结构,我使用的是Intellij Idea15.
![](http://img.blog.csdn.net/20160116140358775)
build.gradle
3.web.xml配置:
4.spring-mvc.xml
5.Controller
6.SpringMVC运行流程图
![](http://images0.cnblogs.com/blog2015/694841/201506/052340331602684.png)
最近工作比较忙,陆续添加
关于项目创建
如果你不知道如何创建一个通过Gradle构建的基本web项目,那么本文无法帮助你,你先去把项目创建好,此项目仅作为练习用,我将再次以插件形式集成shiro,mybatis,oauth等等。如果你看到这里,我就假设你对gradke已经有了初步的了解,已经创建好了一个基本的web项目了,如下图你已经有了一个基本的项目结构,我使用的是Intellij Idea15.
build.gradle
group 'com.51tutechan' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'war' sourceCompatibility = 1.7 repositories { mavenCentral() } dependencies { compile( 'commons-logging:commons-logging:1.2', 'org.springframework:spring-aop:4.2.4.RELEASE', 'org.springframework:spring-beans:4.2.4.RELEASE', 'org.springframework:spring-context:4.2.4.RELEASE', 'org.springframework:spring-core:4.2.4.RELEASE', 'org.springframework:spring-expression:4.2.4.RELEASE', 'org.springframework:spring-web:4.2.4.RELEASE', 'org.springframework:spring-webmvc:4.2.4.RELEASE' ) runtime("jstl:jstl:1.2") } task copyJars(type: Copy) { from configurations.runtime into 'lib' // 目标位置 }
3.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>classpath: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>
4.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.tutechan"/> <!-- 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>
5.Controller
package com.tutechan.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Created by benny on 2016/1/16. */ @Controller @RequestMapping("/test") public class HelloController { @RequestMapping(value = "/hello/{0}", method = RequestMethod.GET) public String helloWorld(@PathVariable("id") Integer id) { System.out.println("get" + id); return "hello"; } }
6.SpringMVC运行流程图
![](http://images0.cnblogs.com/blog2015/694841/201506/052340331602684.png)
最近工作比较忙,陆续添加
相关文章推荐
- Spring框架概况(一)
- POJ 1007 Java:DNA Sorting
- POJ 1006 Java:中国剩余定理
- Java基础(七)——枚举类
- webservice详解(CXF)
- JAVA AOP 动态代理 的例子
- java map 遍历
- SpringMVC: web.xml中声明DispatcherServlet时一定要加入load-on-startup标签
- 使用JNA,让java调用原生代码
- 如何在Eclipse导入jar包
- Java 接口
- 【JVM系列】Java虚拟机体系结构
- java String split
- 工欲善其事-Eclipse设置
- Java基础(六)——接口和抽象类
- 一张图,理解JAVA体系结构、运行机制、JVN运行机制、Java平台(初学)
- java动态代理demo
- Java基础(五)——final修饰符
- 深入分析JavaWeb 技术内幕
- RMI与Hession使用