springMVC controller层的request和对应view层的request区别

web容器用resin,项目采用springMVC,关键配置如下:

<bean

        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/view/" />

        <property name="suffix" value=".jsp" />

    </bean>

控制器层有个方法如下:

 @RequestMapping("/forum")
    public String forum(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {

      	System.out.println("controller getContextPath: " + request.getContextPath());
      	System.out.println("controller getServletPath: " + request.getServletPath());
    	System.out.println("controller query string: " + request.getQueryString());
    	request.setAttribute("controllerrequest", request);
return "/templates/" + systemConfig.getAppName() + "/forum"; }

 返回的页面forum.jsp页面部分代码如下:

<%
	out.print(request.getContextPath());
	out.print("---");
	out.print(request.getServletPath());
	out.print("---");	
	out.print(request.getQueryString());
	out.print("---");	
	out.print(request.equals(request.getAttribute("controllerrequest")));
	
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
//以下省略
扫描二维码关注公众号,回复: 505378 查看本文章

控制器层结果:

controller getContextPath: /bbs7

controller getServletPath: /forum.do

controller query string: fid=1988&filter=type&fval=g6559g80b2g89c2g70b9

view层输出:

/bbs7---/WEB-INF/view/templates/baby/forum.jsp---fid=1988&filter=type&fval=g6559g80b2g89c2g70b9---false

controller层和view层的request有什么异同呢?从打印的结果来看,可以得出如下结论:

(1)这两个request是两个不同的对象

(2)这两个request拿到的getContextPath()是相同的,因为都是拿同一个应用于的ContextPath

(3)两个request 拿到的servletPath是不同的,controller层拿到的是最初请求的servlet路径,view层拿到的是请求经过mvc处理转发之后,jsp页面的路径

(4)两个request 拿到的 queryString是一样的,原因是mvc请求转到view层的时候,把最初请求的queryString带上了

猜你喜欢

转载自breezylee.iteye.com/blog/2165095