2011-05-20 14 views
4

我正在創建一個基於3.1 M1的新項目作爲測試用例。我有我的web.xml設置使用DispatcherServlet具有org.springframework.web.context.support.Annotation ConfigWebApplicationContext的contextClass和domain.ApplicationConfiguration的contextConfigLocation。在Spring 3.1中定義視圖解析器

但是,當從我的@Controller註釋類之一嘗試返回帶有視圖名稱「test」的ModelAndView的方法時,它會在同一控制器類中使用@RequestMapping的「test」當我希望它在WebContent目錄中尋找一個名爲「test.jsp」的jsp時,它看起來沒有viewresolver永遠不會實例化。我曾嘗試在ApplicationConfiguration類中聲明一個視圖解析器,但它似乎被忽略。 我總是得到一個日誌消息是這樣的: 警告:沒有映射的DispatcherServlet發現HTTP請求與URI [/測試/富/測試]名爲「調度」

如何配置在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> 

什麼配置的其他部分將是有益的?

回答

2

documentation,定義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> 
+1

這將需要創建一個servletName.xml文件,這在3.1中不再需要。您應該能夠使用註釋進行所有配置,其中所需的唯一xml文件是web.xml文件。 – 2011-05-24 22:39:47

1

它開始工作時,我從變更後的標籤: http://java.sun.com/xml/ns/的JavaEE /網絡app_3_0.xsd 「 ID = 」WebApp_ID「 版本= 」3.0「>

到: http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd」 版本=「2.5」>

我知道servlet 3.0的支持是d在里程碑2中,我只是沒有預料到那種預先聲明它的失敗模式。我沒有錯誤,它只是忽略了我所有的控制器映射。

+0

將此添加到您的第一個問題,您可以編輯您的問題。這不應該是一個答案。 – 2011-05-25 14:56:24

+0

@MichaelB。爲什麼不,如果它解決了這個問題呢? – eis 2013-12-05 16:16:35

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

不要給URL模式作爲/*。提及Url模式爲*.htm。肯定會起作用。

相關問題