繁体   English   中英

在Spring 3.1中定义视图解析器

[英]defining a view resolver in Spring 3.1

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

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

如何在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>

其他哪些配置有用?

文档中 ,定义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>

当我更改标签时,它开始工作:http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd“id =”WebApp_ID“version =”3.0“>

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

我知道servile 3.0支持是在Milestone 2中提供的,我只是没想到那种先发制人的故障模式。 我没有错误,它只是忽略了我的所有控制器映射。

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

不要将Url模式设为/* 提到Url模式为*.htm 当然它会奏效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM