defining a view resolver in Spring 3.1
I am creating a new project based on 3.1 M1 as a test case. 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.
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. I have tried declaring a view resolver in the ApplicationConfiguration class but it seems to be ignored. 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'
How do I configure a view resolver in 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</ser开发者_StackOverflow社区vlet-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:
<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">
to: 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. 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 /*
. Mention the Url Pattern as *.htm
. Surely it will work.
精彩评论