簡體   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