paj28
paj28

Reputation: 2320

Spring: ModelAndView can't locate view

This controller fails to find the view "index":

@RequestMapping("/test2")
public ModelAndView test2() throws Exception {
    return new ModelAndView("index");
}

It returns a 404 error, with the following in the GlassFish console:

Severe:   PWC6117: File "null" not found

That is strange because this controller finds it ok:

@RequestMapping("/test")
public String test() throws Exception {
    return "index";
}

My project is NetBeans default Spring Web MVC project (using 4.0.1). The only config change is to add this to applicationContext.xml:

<mvc:annotation-driven/>
<context:component-scan base-package="myapp"/>

It seems that either I'm using ModelAndView incorrectly, or for some reason it's using different view resolution.

For reference, this is the view resolver:

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

And this is the web.xml for the dispatcher:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

Upvotes: 1

Views: 3514

Answers (2)

Rashad
Rashad

Reputation: 80

In my case it was wrong folder name as you know moving project from windows to Linux your WEB-INF folder changes name to lowercase and of course in this case your ModelAndView cant find right path for .jsp files in war and all you need to do is uppercase WEB-INF folder

Upvotes: 0

rcockell
rcockell

Reputation: 92

I can't say with no doubt because you didn't post your list of importations of the code, but i think this error (404 not found) occurs when you import "org.springframework.web.portlet.ModelAndView" instead of "org.springframework.web.servlet.ModelAndView" One is used for portlets, the other for servlets. Most of the ide's imports the firs one automaticaly.

Upvotes: 2

Related Questions