简体   繁体   English

在Spring 3.1中定义视图解析器

[英]defining a view resolver in Spring 3.1

I am creating a new project based on 3.1 M1 as a test case. 我正在创建一个基于3.1 M1的新项目作为测试用例。 I have my web.xml set up to use DispatcherServlet with a contextClass of org.springframework.web.context.support.Annotation ConfigWebApplicationContext and a contextConfigLocation of domain.ApplicationConfiguration. 我将我的web.xml设置为使用DispatcherServlet和contextClass of org.springframework.web.context.support.Annotation ConfigWebApplicationContext和contextConppigLocation of domain.ApplicationConfiguration。

However, when a method from one of my @Controller annotated classes with attempts to return a ModelAndView with a view name of "test" I it looks for a method in the same controller class with a @RequestMapping of "test" when I would like it to look for a jsp named "test.jsp" in the WebContent directory, and it looks like no viewresolver is never instantiated. 但是,当我的一个@Controller注释类中的方法试图返回一个视图名称为“test”的ModelAndView时,我会在同一个控制器类中查找一个方法,当我想要@RequestMapping为“test”时它在WebContent目录中查找名为“test.jsp”的jsp,看起来似乎没有实例化viewresolver。 I have tried declaring a view resolver in the ApplicationConfiguration class but it seems to be ignored. 我已经尝试在ApplicationConfiguration类中声明一个视图解析器,但它似乎被忽略了。 I always get a log message something like: WARNING: No mapping found for HTTP request with URI [/test/foo/test] in DispatcherServlet with name 'dispatcher' 我总是得到一条日志消息:警告:在DispatcherServlet中找不到带有URI [/ test / foo / test]的HTTP请求的映射,名称为“dispatcher”

How do I configure a view resolver in 3.1? 如何在3.1中配置视图解析器?

web.xml 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>

What other pieces of configuration would be useful? 其他哪些配置有用?

From the documentation , the usual way of defining a JSP viewResolver is: 文档中 ,定义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>

It started working when I changed the tag from: 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_3_0.xsd“id =”WebApp_ID“version =”3.0“>

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

I know that servlet 3.0 support was due in Milestone 2, I just didn't expect that kind of failure mode for preemptively declaring it. 我知道servile 3.0支持是在Milestone 2中提供的,我只是没想到那种先发制人的故障模式。 I got no errors, it just ignored all of my Controller mappings. 我没有错误,它只是忽略了我的所有控制器映射。

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

Do not give the Url Pattern as /* . 不要将Url模式设为/* Mention the Url Pattern as *.htm . 提到Url模式为*.htm Surely it will work. 当然它会奏效。

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

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