可运行的附件地址:http://files.cnblogs.com/files/douJiangYouTiao888/springWithXML.zip
项目说明:
作者环境:maven3+jdk1.8+spring4
项目结构:
父pom:主要是进行一系列版本定义
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!-- 基础组件模块,定义各种公用的组件 --> <modelVersion>4.0.0</modelVersion> <groupId>com.wang</groupId> <artifactId>basicmodule</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>basicmodule</name> <url>http://maven.apache.org</url> <!-- 常量属性 --> <properties> <!-- 编码集 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- spring版本 --> <springframework.version>4.3.7.RELEASE</springframework.version> </properties> <!-- 模块定义 --> <dependencyManagement> <dependencies> <!-- spring模块 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework.version}</version> </dependency> <!-- servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <!-- <scope>provide</scope> --> </dependency> <!-- jstl,需要引入jstl.jar和standard.jar --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provide</scope> </dependency> --> </dependencies> </dependencyManagement> </project>
当前pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wang</groupId> <artifactId>springWithXML</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springWithXML Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 父模块,基础模块 --> <parent> <groupId>com.wang</groupId> <artifactId>basicmodule</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <!-- springMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <!-- servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies> <build> <finalName>sprintWithXML</finalName> </build> </project>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- 源码:spring-web.jar 功能:字符集过滤器,设置编码集为UTF-8,解决POST的中文乱码问题。 参数说明: encoding:request的编码集(request.setCharacterEncoding("UTF-8")) forceEncoding:默认为false设置为true,response的编码集也强制转变成UTF-8(response.setCharacterEncoding("UTF-8")) --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 源码:spring-web.jar 默认配置:/WEB-INF/${servlet-name}-servlet.xml,即/WEB-INF/wly-servlet.xml 覆盖默认配置:初始化参数:contextConfigLocation,参数多个值使用逗号隔开,如:a.xml,b.xml --> <servlet> <servlet-name>wly</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:wly-servlet.xml</param-value> </init-param> <!-- 容器初始化加载此servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>wly</servlet-name> <!-- 一般有以下写法: *.do 拦截固定格式的请求 / rest风格的写法:拦截所有资源,需要针对静态资源做单独处理 /* 错误写法:会在处理完请求后拦截jsp导致404错误 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 测试使用的servlet --> <servlet> <servlet-name>testServlet</servlet-name> <servlet-class>com.wang.servlet.TestServlet</servlet-class> </servlet> </web-app>
springmvc核心配置文件:wly-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.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 公共配置,默认配置在org.springframework.web.servlet.Dispatcher类同包DispatcherServlet.properties下 --> <!-- HandlerMapping,映射器,配置的bean.name对应请求的url,将请求的url映射为一个HandlerExecutionChain对象(包含一个Handler和多个HandlerInterceptor) --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- HandlerAdapter,将handler包装为适配器,满足各种不同适配器的使用,Controller、SimpleFormController等 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- ViewResovler,将拼接视图逻辑名称转为物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 需要引入jstl的相关jar包,jstl1.2以下需要引入jstl.jar、standard.jar --> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 静态资源处理,不拦截静态资源 --> <mvc:resources location="/" mapping="/**/*.js"></mvc:resources> <!-- handler,处理器,需要开发实现 --> <bean name="/wang" class="com.wang.controller.WangController"/> <!-- 接口return null的情况代表已经把输出通过流的形式返回,无需进行视图的解析 --> <bean name="/testNull" class="com.wang.controller.NullController"/> <!-- GET、POST请求方式的限定,supportedMethods需要配合AbstractController使用 --> <bean name="/helloPost" class="com.wang.controller.HelloController"> <property name="supportedMethods" value="POST"></property> </bean> <!-- 缓存响应信息 --> <bean name="/helloCache" class="com.wang.controller.HelloController"> <!-- 默认为-1,>0的值代表缓存时间 --> <property name="cacheSeconds" value="8"></property> </bean> <!-- 请求转发给servlet来处理 --> <bean name="/helloServlet" class="org.springframework.web.servlet.mvc.ServletForwardingController"> <!--value对应web.xml的<servlet-name>--> <property name="servletName" value="testServlet"></property> </bean> </beans>
java类:
package com.wang.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class HelloController extends AbstractController{ @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { System.err.println("deal request"); ModelAndView mv =new ModelAndView(); //模型 mv.addObject("message","test"); //视图 mv.setViewName("index"); return mv; } }
package com.wang.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class NullController implements Controller{ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { response.getWriter().write("<a href=''>this</a>"); //return null给DispatcherServlet,即告知servlet无需进行视图解析。 return null; } }
package com.wang.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class WangController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { System.err.println("deal request!"); ModelAndView mv =new ModelAndView(); //模型 mv.addObject("message","test"); //视图 mv.setViewName("index"); return mv; } }
package com.wang.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.err.println("servlet处理"); resp.sendRedirect("index.jsp"); } }
测试URL:
http://ip:port/具体URL
1.Controller返回ModeAndView:/wang
2.Controller返回null:/testNull
3.只接受POST请求:/helloPost
4.缓存响应信息:/helloCache
5.请求转发给servlet来处理:/helloServlet
例:http://localhost:8088/wang
OK,大功告成~