views:

55

answers:

1

Hi,

I'm trying to set up a skeleton Spring 3 MVC project but i'm having difficulties getting the views to render. I have followed the structure as described in the mvc-basic sample project and at http://blog.springsource.com/2009/12/21/mvc-simplifications-in-spring-3-0/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Interface21TeamBlog+%28SpringSource+Team+Blog%29 to set up the web.xml, app-config.xml and mvc-config.xml files. The controller gets called but when it reaches the point of finding the view and rendering it i get a 404 error. The files are as follows:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;

    <!-- Handles all requests into the application -->
    <servlet>
        <servlet-name>myServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/app-config.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>

</web-app>

app-config.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="com.myProject" />

    <!-- Application Message Bundle -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages/messages" />
        <property name="cacheSeconds" value="0" />
    </bean>

    <!-- Configures Spring MVC -->
    <import resource="mvc-config.xml" />

</beans>

mvc-config.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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt;

    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />

    <mvc:view-controller path="/app" view-name="welcome"/>

    <!-- Configures Handler Interceptors -->    
    <mvc:interceptors>
        <!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    </mvc:interceptors>

    <!-- Saves a locale change using a cookie -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

In "Java Resources : src" -> com.myProject -> HelloWorldController.java i have:

package com.myProject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value="/helloworld")
public class HelloWorldController {

    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView helloWorld() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("helloworld");
        mav.addObject("message", "Hello World!");
        return mav;
    }

    @RequestMapping(value="/Second", method = RequestMethod.GET)
    public ModelAndView Second(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("Second");
        mav.addObject("message", "Hello World!");
        return mav;
    }
}

and in WebContent/WEB-INF/views i have:

WebContent              (folder)
  WEB-INF               (folder)
    views               (folder)
      helloworld        (folder)
        helloworld.jsp  (.jsp view)
      helloworld.jsp    (.jsp view)
      welcome.jsp       (.jsp view)

The views have straighforward html in them. When i request http://localhost:8080/projectname/app i correctly get the the views -> welcome.jsp page. However when i request http://localhost:8080/projectname/app/helloworld or http://localhost:8080/projectname/app/helloworld/ execution hits the correct controller actions but i get HTTP Status 404 - /projectname/WEB-INF/views/helloworld.jsp

Can anyone advise as to what is wrong?

Thanks

A: 

Looks like it is because you have not specified the viewClass property.

Can you try with

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
</bean>

It works for welcome page due to this

<mvc:view-controller path="/app" view-name="welcome"/>
Raghuram
Specifying `viewClass` should not be necessary, Spring will pick a sensible default.
skaffman