在 Spring 3.1 中定义视图解析器
我正在创建一个基于 3.1 M1 的新项目作为测试用例。我将 web.xml 设置为使用带有 org.springframework.web.context.support.Annotation ConfigWebApplicationContext 的 contextClass 和 domain.ApplicationConfiguration 的 contextConfigLocation 的 DispatcherServlet。
但是,当我的 @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>
还有哪些其他有用的配置?
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</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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 文档,定义JSP viewResolver的常用方法是:
From the documentation, the usual way of defining a JSP viewResolver is:
当我更改标签时它开始工作:
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 支持是在 Milestone 2 中到期的,我只是没想到会出现这种情况先发制人地声明它的失败模式我没有错误,它只是忽略了我的所有控制器映射。
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.
不要将 URL 模式指定为
/*
。将 Url 模式指定为*.htm
。肯定会起作用的。Do not give the Url Pattern as
/*
. Mention the Url Pattern as*.htm
. Surely it will work.