在 Spring 3.1 中定义视图解析器

Posted

技术标签:

【中文标题】在 Spring 3.1 中定义视图解析器【英文标题】:defining a view resolver in Spring 3.1 【发布时间】:2011-05-20 20:40:38 【问题描述】:

我正在创建一个基于 3.1 M1 的新项目作为测试用例。我将 web.xml 设置为使用 DispatcherServlet,其 contextClass 为 org.springframework.web.context.support.Annotation ConfigWebApplicationContext, contextConfigLocation 为 domain.ApplicationConfiguration。

但是,当我的一个 @Controller 注释类中的方法尝试返回视图名称为“test”的 ModelAndView 时,它会在同一控制器类中查找 @RequestMapping 为“test”的方法我希望它在 WebContent 目录中查找名为“test.jsp”的 jsp,并且看起来没有任何 viewresolver 永远不会被实例化。我尝试在 ApplicationConfiguration 类中声明一个视图解析器,但它似乎被忽略了。 我总是收到类似以下的日志消息: 警告:在名称为“dispatcher”的 DispatcherServlet 中找不到带有 URI [/test/foo/test] 的 HTTP 请求的映射

如何在 3.1 中配置视图解析器?

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>domain.test.configuration.ApplicationConfiguration</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>domain.test</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<display-name>test</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

还有哪些其他配置有用?

【问题讨论】:

【参考方案1】:

从documentation,定义JSP viewResolver的常用方法是:

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

【讨论】:

这需要创建一个 servletName.xml 文件,在 3.1 中不再需要该文件。您应该能够使用注释进行所有配置,其中唯一需要的 xml 文件是 web.xml 文件。【参考方案2】:

当我从以下位置更改标签时它开始工作: http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" 版本="3.0">

到: http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

我知道 servlet 3.0 支持是在里程碑 2 中到期的,我只是没想到会出现这种抢先声明它的失败模式。我没有收到任何错误,它只是忽略了我所有的控制器映射。

【讨论】:

将此添加到您的初始问题中,您可以编辑您的问题。这不应该是一个答案。【参考方案3】:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

不要将 URL 模式指定为 /*。将 URL 模式称为 *.htm。肯定会成功的。

【讨论】:

以上是关于在 Spring 3.1 中定义视图解析器的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Spring Boot 中使用注释配置视图解析器? [复制]

配置视图解析器

spring mvc中的多个视图解析器

笔记30 视图解析 ——TilesViewResolver

Spring MVC

Spring 安全会话管理和 Spring MVC 视图解析器错误