关于项目创建
如果你不知道如何创建一个通过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运行流程图
最近工作比较忙,陆续添加