标签:
如何自定义一个HttpServletRequest呢?
比如spring mvc,我们通常写成
@RequestMapping(value = "/helloworld", method = RequestMethod.GET) public ModelAndView test(HttpServletRequest req, HttpServletResponse res) { return new ModelAndView("index.jsp"); }
一直在想,能不能不用HttpServletRequest、和HttpServletResponse 呢?最终给我研究出来了下面的写法。
@RequestMapping(value = "/helloworld", method = RequestMethod.GET) public ModelAndView test(WebRequest req, WebResponse res) { return new ModelAndView("index.jsp"); }
其实就是filter、拦截器, 着象了。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; if(!(httpRequest instanceof WebRequest)) { httpRequest = new WebRequest(httpRequest); } HttpServletResponse httpResponse = (HttpServletResponse) response; if(!(httpResponse instanceof WebResponse)) { httpResponse = new WebResponse(httpResponse); } filterChain.doFilter(httpRequest, httpResponse); }
原理就是在web.xml里自定义一个拦截器,拦截所有url并把httpservletrequest交给ServletRequest。
private ServletRequest request; public ServletRequestWrapper(ServletRequest request) { if(request == null) { throw new IllegalArgumentException("Request cannot be null"); } else { this.request = request; } }
附上代码好了(各位可以直接粘贴-哈哈-我的最爱):
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 设置Spring容器加载配置文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </context-param> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>interceptAllFilter</filter-name> <filter-class>com.test.filter.RequestFilter</filter-class> </filter> <filter-mapping> <filter-name>interceptAllFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
pom.xml
<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>Test</groupId> <artifactId>test</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>com.test Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.1.RELEASE</version> <type>jar</type> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.1.RELEASE</version> <type>jar</type> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.1.RELEASE</version> <type>jar</type> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.1.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>test</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat6-maven-plugin</artifactId> <version>2.2</version> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
WebRequest.java
/** * Created by shaoyongyang on 2015/6/5. */ package com.test.core; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; public class WebRequest extends HttpServletRequestWrapper { public WebRequest(HttpServletRequest request) { super(request); } }
WebResponse.java
/** * Created by shaoyongyang on 2015/6/5. */ package com.test.core; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; public class WebResponse extends HttpServletResponseWrapper { public WebResponse(HttpServletResponse response) { super(response); } }
RequestFilter.java
/** * Created by shaoyongyang on 2015/6/5. */ package com.test.filter; import com.test.core.WebRequest; import com.test.core.WebResponse; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class RequestFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; if(!(httpRequest instanceof WebRequest)) { httpRequest = new WebRequest(httpRequest); } HttpServletResponse httpResponse = (HttpServletResponse) response; if(!(httpResponse instanceof WebResponse)) { httpResponse = new WebResponse(httpResponse); } filterChain.doFilter(httpRequest, httpResponse); } public void destroy() { } }
TestController.java
/** * Created by shaoyongyang on 2015/6/5. */ package com.test.action; import com.alibaba.fastjson.JSONObject; import com.test.core.WebRequest; import com.test.core.WebResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; @Controller public class TestController{ @RequestMapping(value = "/helloworld", method = RequestMethod.GET) public ModelAndView test(WebRequest req, WebResponse res) { return new ModelAndView("index.jsp"); } }
springmvc-servlet.xml 位于WEB-INF
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <mvc:annotation-driven /> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> <context:component-scan base-package="com.test.action" /> </beans>
代码拷贝的多了,其实关键的还是在上头。在RequestFilter里面。
maven项目可直接 mvn tomcat:run
参考了github上一位外国老兄写的代码、附上地址:https://github.com/surenrao/nsr-bookstore/tree/1a7e56d306819e9939d9d67bd12ee2043e8997ce/java/bookstore-web
如何自定义HttpServletRequest、HttpServletResponse?
标签:
原文地址:http://my.oschina.net/0x4ad/blog/425240