在 Spring 3.1 中定义视图解析器

发布于 2024-11-09 03:18:24 字数 2648 浏览 0 评论 0原文

我正在创建一个基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小鸟爱天空丶 2024-11-16 03:18:24

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

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>
意中人 2024-11-16 03:18:24

当我更改标签时它开始工作:
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.

一曲爱恨情仇 2024-11-16 03:18:24
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

不要将 URL 模式指定为 /*。将 Url 模式指定为 *.htm。肯定会起作用的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文