我在Spring 2.5.5下的View Resolution中遇到了一些麻煩。基本上我只是試圖用來自傳入的控制器的消息來顯示我的視圖。當Controller返回ModelAndView但DispatcherServelt說它無法找到Handler時,問題就出現了。Spring如何解析視圖?
所有文件似乎都在正確的位置。我認爲問題在於Spring無法解決這個問題。從我看到的我正確使用InternalResourceResolver。我只是不知道爲什麼它失敗。
一旦我提出的要求,這是什麼在catalina.out的日誌:
Feb 8, 2010 3:27:24 PM com.madebymn.newsExample.web.IndexController handleRequest
INFO: Handling a Request: /index.jsp
Feb 8, 2010 3:27:24 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/newsExample/WEB-INF/jsp/index.jsp] in DispatcherServlet with name 'catchAll'
這裏是我的web.xml:
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>A News Example</display-name>
<description>A News Example</description>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-jdbc.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>catchAll</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>catchAll</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>jsp/index.jsp</welcome-file>
</welcome-file-list>
這裏的Servlet的XML:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="indexController" class="com.madebymn.newsExample.web.IndexController" />
<bean name="authorController" class="com.madebymn.newsExample.web.AuthorController">
<constructor-arg>
<ref bean="authorService" />
</constructor-arg>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index.jsp">indexController</prop>
<prop key="/author/*">authorController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
和h ERE的我的IndexController類:
public class IndexController implements org.springframework.web.servlet.mvc.Controller
{
protected final Log logger = LogFactory.getLog(getClass());
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
logger.info("Handling a Request: " + request.getServletPath());
ModelAndView modelAndView = new ModelAndView("index");
modelAndView.addObject("message", "someMessage");
return modelAndView;
}
}
所有文件都在'/ WEB-INF/jsp'下?那就是你在viewResolver中尋找所有jsp的地方。 – 2010-02-08 15:59:42
您要求將調用JSP頁面重定向到DispatcherServlet。然後當你得到index.jsp時,你被重定向到indexController。然後,indexController將您重定向到/newsExample/WEB-INF/jsp/index.jsp。在這裏,它又被重定向到DispatcherServlet。 這裏有點不對勁! :) – rochb 2010-02-08 16:01:11
什麼是您要求錯誤的請求的URL? – 2010-02-08 16:02:17