`

诡异的InternalResourceViewResolver

阅读更多
现象:
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean> 
 
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".html" />
</beans:bean>
   第一种可以正常工作,第二种不行,当然了/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,应该是没有问题的,最终我跟踪代码到InternalResourceViewrenderMergedOutputModel函数。
    
// 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'
   

 

 
      从调用的轨迹来看,jsp file直接在response流里面进行了处理,但是html file又发起了一次请求。
     但是我在普通的servlet里面运行工程是可以的,由于没有时间下载tomcat的源码进行调试,没有看到具体的代码走的分支,有熟悉的给帮我解答下。
  • 大小: 61.5 KB
  • 大小: 102.4 KB
0
1
分享到:
评论
6 楼 mingnianshishenian 2015-07-20  
我也遇到了这个问题,这个问题的原因是web.xml里<servlet-mapping>拦截的*.html和你的html的扩展名一样了!
<servlet-mapping>
	<servlet-name>action</servlet-name>
	<url-pattern>
引用
[b][color=red]*.html[/color][/b]
</url-pattern> </servlet-mapping>

这个名字和你的html文件的扩展名不能一样,要不会出问题!
5 楼 asialee 2014-09-17  
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 [<span style="color: #ff0000;">/microshield-radius</span>/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' 
4 楼 asialee 2014-09-17  
zhaobohao 写道
这是两个问题,兄弟,你的方向错了.
首先有一个问题要明确,jsp,html,php,.do这些个后缀文件用什么去解析,是在web容器里配置的,和InternalResourceViewResolver没有一点关系。
你现在的问题是要去web容器里配置 .html的文件用jsp的解析器去解析。



这个我是知道的,但是我现在的问题是InternalResourceViewResolver为什么只能转到jsp或者servlet里面去,普通的html为什么就行,会添加一个contextPath,这个是我比较好奇的。

3 楼 asialee 2014-09-17  
zhaobohao 写道
http://haohaoxuexi.iteye.com/blog/1770554 可以看一个这个对InternalResourceViewResolver的解释,基本和你代码跟踪的结果一样。



嗯,路径相同,但是没有解决这个问题。
2 楼 zhaobohao 2014-09-17  
http://haohaoxuexi.iteye.com/blog/1770554 可以看一个这个对InternalResourceViewResolver的解释,基本和你代码跟踪的结果一样。
1 楼 zhaobohao 2014-09-17  
这是两个问题,兄弟,你的方向错了.
首先有一个问题要明确,jsp,html,php,.do这些个后缀文件用什么去解析,是在web容器里配置的,和InternalResourceViewResolver没有一点关系。
你现在的问题是要去web容器里配置 .html的文件用jsp的解析器去解析。

相关推荐

    Spring MVC中InternalResourceViewResolver视图解析器的默认行为.docx

    `InternalResourceViewResolver`是Spring MVC提供的一种内置的视图解析器,主要用于处理JSP视图。当我们不对其进行任何额外配置时,它会遵循一定的默认行为。 默认情况下,`InternalResourceViewResolver`会将控制...

    国庆SpringBoot相关知识点.docx

    本文档主要介绍了 SpringBoot 框架相关的知识点,包括 IOC 容器、SpringMVC 概念、DispatcherServlet、InternalResourceViewResolver 等。 一、IOC 容器 IOC 容器是 Spring 框架的核心组件之一,负责管理对象的...

    spring 学习 springview例子

    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } ``` 这样,当我们返回逻辑视图名...

    spring mvc 在 intellij 的 helloworld 基本配置

    - 使用InternalResourceViewResolver来解析JSP页面,需要指定JSP文件的前缀和后缀,这样DispatcherServlet就能根据视图名找到对应的JSP页面。 - 在Controller中处理请求后,返回的字符串通常就是视图名,...

    springmvc_day02

    Spring MVC 提供了几种内置的视图解析器,例如 InternalResourceViewResolver 和 BeanNameViewResolver。InternalResourceViewResolver 是最常用的视图解析器,它支持JSP和其他基于servlet的视图技术。配置...

    spring mvc 简单demo

    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } ``` 这样,当控制器返回视图名称...

    Spring MVC 解读——View,ViewResolver.zip

    Spring MVC提供了多种内置的ViewResolver实现,如InternalResourceViewResolver(用于JSP)和FreeMarkerViewResolver(用于FreeMarker)。开发者可以根据项目需求选择合适的视图解析器,也可以自定义实现。 **1. ...

    gradle的gretty插件使用例子

    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } } ``` 这个配置类启用了Spring MVC...

    springboot_笔记.docx

    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } } ``` 在这个例子中,`WebConfig`...

    基于springMVC注解的简单例子

    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } @Override public void ...

    SpringMvcDemo

    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } } ``` 通过这个简单的配置,项目就...

    springmvc注解版 helloworld

    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } ``` 这样,"hello"视图名称就会被...

    Spring-mvc,例题

    &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/"&gt;&lt;/property&gt; &lt;property name="suffix" value=".jsp"&gt;&lt;/property&gt; &lt;!-...

    将spring mobile集成到spring mvc

    InternalResourceViewResolver mobileViewResolver = new InternalResourceViewResolver(); mobileViewResolver.setPrefix("/WEB-INF/views/mobile/"); mobileViewResolver.setSuffix(".jsp"); viewResolver....

    java web hibernate struts spring 全方位解决乱码

    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setContentType("text/html;charset=UTF-8"); // ... } ``` 文件名乱码问题通常发生在读写文件时。Java的`File`类和`...

    SpringMVC+Hibernate实例

    &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt; ...

    springmvc.zip

    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } } ``` `@EnableWebMvc` 注解开启了...

    SpringMVC两种配置的Demo

    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } @Override public void ...

    SpringBoot整合jsp示例,SpringBoot项目创建示例

    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp"); registry.viewResolver(resolver); } } ``` 这段配置指定...

    springboot集成Jsp案例

    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp"); registry.viewResolver(resolver); } } ``` 3. 创建JSP...

Global site tag (gtag.js) - Google Analytics