诡异的InternalResourceViewResolver

阅读更多
现象:

    
    
 
 

    
    

    第一种可以正常工作,第二种不行,当然了/WEB-INF/views里面 jsp和html结尾的都可以。
  
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myapp/WEB-INF/views/home.html] in DispatcherServlet with name 'appServlet'
    
    错误的原因就是html页面被加上了/myapp前缀,myapp为我的工程名称。
    分析,从spring的 InternalResourceViewResolver 入手:
   
@Override
    protected AbstractUrlBasedView buildView(String viewName) throws Exception {
         InternalResourceView view = (InternalResourceView) super .buildView(viewName);
          if (this .alwaysInclude != null) {
             view.setAlwaysInclude( this.alwaysInclude );
         }
          if (this .exposeContextBeansAsAttributes != null) {
             view.setExposeContextBeansAsAttributes( this.exposeContextBeansAsAttributes );
         }
          if (this .exposedContextBeanNames != null) {
             view.setExposedContextBeanNames( this.exposedContextBeanNames );
         }
         view.setPreventDispatchLoop( true);
          return view;
    }
    
       从代码来看这个地方只是构建了一个view,应该是没有问题的,最终我跟踪代码到 InternalResourceView renderMergedOutputModel 函数。
    
// Determine the path for the request dispatcher.
         String dispatcherPath = prepareForRendering(requestToExpose, response);

          // Obtain a RequestDispatcher for the target resource (typically a JSP).
         RequestDispatcher rd = getRequestDispatcher(requestToExpose, dispatcherPath);
          if (rd == null) {
              throw new ServletException("Could not get RequestDispatcher for [" + getUrl() +
                       "]: Check that the corresponding file exists within your web application archive!");
         }

          // If already included or response already committed, perform include, else forward.
          if (useInclude(requestToExpose, response)) {
             response.setContentType(getContentType());
              if (logger .isDebugEnabled()) {
                  logger.debug("Including resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'" );
             }
             rd.include(requestToExpose, response);
         }

          else {
              // Note: The forwarded resource is supposed to determine the content type itself.
             exposeForwardRequestAttributes(requestToExpose);
              if (logger .isDebugEnabled()) {
                  logger.debug("Forwarding to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
             }
             rd.forward(requestToExpose, response);
         }
      从这段代码来看,prepareForRendering这个函数是来准备url的,也就是刚才的InternalResourceViewRoslver的解析的结果,从调试来看,这两个的配置的返回的路径一致。那问题应该出在 RequestDispatcher的fowward函数了,从这个地方来看,又回到了servlet里面,然后我写了个小例子。
     
    从小例子来看是完全一样的。
    
 10:55:58.283 [http-bio-8080-exec-4] DEBUG o.s.web.servlet.view.JstlView - Added model object 'org.springframework.validation.BindingResult.user' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'index'
10:55:58.283 [http-bio-8080-exec-4] DEBUG o.s.web.servlet.view.JstlView - Added model object 'user' of type [microshield.com.cn.radius.admin.model.User] to request in view with name 'index'
10:55:58.284 [http-bio-8080-exec-4] DEBUG o.s.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/pages/index.jsp] in InternalResourceView 'index'
10:56:11.719 [http-bio-8080-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request

10:48:22.161 [http-bio-8080-exec-10] DEBUG o.s.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/pages/index.html] in InternalResourceView 'index'
10:48:22.178 [http-bio-8080-exec-10] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'Spring MVC Dispatcher Servlet' processing GET request for [/microshield-radius/WEB-INF/pages/index.html]
10:48:22.180 [http-bio-8080-exec-10] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Looking for URL mappings in application context: WebApplicationContext for namespace 'Spring MVC Dispatcher Servlet-servlet': startup date [Mon Sep 15 10:47:54 CST 2014]; parent: Root WebApplicationContext
10:48:22.182 [http-bio-8080-exec-10] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'userAction'
   
诡异的InternalResourceViewResolver_第1张图片
 
诡异的InternalResourceViewResolver_第2张图片
 
       从调用的轨迹来看,jsp file直接在response流里面进行了处理,但是html file又发起了一次请求。
     但是我在普通的servlet里面运行工程是可以的,由于没有时间下载tomcat的源码进行调试,没有看到具体的代码走的分支,有熟悉的给帮我解答下。
  • 诡异的InternalResourceViewResolver_第3张图片
  • 大小: 61.5 KB
  • 诡异的InternalResourceViewResolver_第4张图片
  • 大小: 102.4 KB
  • 查看图片附件

你可能感兴趣的:(spring,mvc)