Matt-Lloyd
Matt-Lloyd

Reputation: 117

How does Spring resolve views?

I've been having a little trouble with View Resolution under Spring 2.5.5. Basically I'm just trying to show my view with a message from the controller passed in. The issue comes when the Controller returns the ModelAndView but the DispatcherServelt says it can't find a Handler.

All the files seem to be in the correct place. I think the issue is that Spring can't resolve the view. From what I've seen I'm using the InternalResourceResolver correctly. I'm just at a loss as to why it is failing.

Once I've made a request this is whats in the catalina.out log:

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'

Here's my 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>

Here's the 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>

And here's my IndexController Class:

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;
    }
}

Upvotes: 4

Views: 7247

Answers (1)

axtavt
axtavt

Reputation: 242686

The problem is that you mapped your DispatcherServlet as *.jsp, when views are JSPs too. Try to map DispatcherServlet to something different, like *.html

Upvotes: 4

Related Questions