现象:
<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,应该是没有问题的,最终我跟踪代码到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'
从调用的轨迹来看,jsp file直接在response流里面进行了处理,但是html file又发起了一次请求。
但是我在普通的servlet里面运行工程是可以的,由于没有时间下载tomcat的源码进行调试,没有看到具体的代码走的分支,有熟悉的给帮我解答下。
相关推荐
`InternalResourceViewResolver`是Spring MVC提供的一种内置的视图解析器,主要用于处理JSP视图。当我们不对其进行任何额外配置时,它会遵循一定的默认行为。 默认情况下,`InternalResourceViewResolver`会将控制...
本文档主要介绍了 SpringBoot 框架相关的知识点,包括 IOC 容器、SpringMVC 概念、DispatcherServlet、InternalResourceViewResolver 等。 一、IOC 容器 IOC 容器是 Spring 框架的核心组件之一,负责管理对象的...
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } ``` 这样,当我们返回逻辑视图名...
- 使用InternalResourceViewResolver来解析JSP页面,需要指定JSP文件的前缀和后缀,这样DispatcherServlet就能根据视图名找到对应的JSP页面。 - 在Controller中处理请求后,返回的字符串通常就是视图名,...
Spring MVC 提供了几种内置的视图解析器,例如 InternalResourceViewResolver 和 BeanNameViewResolver。InternalResourceViewResolver 是最常用的视图解析器,它支持JSP和其他基于servlet的视图技术。配置...
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } ``` 这样,当控制器返回视图名称...
Spring MVC提供了多种内置的ViewResolver实现,如InternalResourceViewResolver(用于JSP)和FreeMarkerViewResolver(用于FreeMarker)。开发者可以根据项目需求选择合适的视图解析器,也可以自定义实现。 **1. ...
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } } ``` 这个配置类启用了Spring MVC...
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } } ``` 在这个例子中,`WebConfig`...
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } @Override public void ...
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } } ``` 通过这个简单的配置,项目就...
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } ``` 这样,"hello"视图名称就会被...
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> <!-...
InternalResourceViewResolver mobileViewResolver = new InternalResourceViewResolver(); mobileViewResolver.setPrefix("/WEB-INF/views/mobile/"); mobileViewResolver.setSuffix(".jsp"); viewResolver....
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setContentType("text/html;charset=UTF-8"); // ... } ``` 文件名乱码问题通常发生在读写文件时。Java的`File`类和`...
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> ...
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } } ``` `@EnableWebMvc` 注解开启了...
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } @Override public void ...
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp"); registry.viewResolver(resolver); } } ``` 这段配置指定...
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp"); registry.viewResolver(resolver); } } ``` 3. 创建JSP...