1

spring mvc InternalResourceViewResolver doesnt get prefix but suffix from controller.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">


<context:component-scan base-package="pizzaorder" />

<mvc:annotation-driven />

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

controller:

package pizzaorder;

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

@Controller
public class PizzaController {

@RequestMapping("/")
public ModelAndView getAlap(){

    ModelAndView model1=new ModelAndView("index");

    return model1;
}

}

if I modify suffix to index.jsp, it works well. if left suffix .jsp it shows:

enter image description here

also in debug I see that view name is correctly passed: enter image description here

Istvan
  • 73
  • 12
  • Can you clean the project and re-run ? All seems fine .. – Kalyan Chavali Jul 08 '15 at 16:15
  • same issue after cleaning project: HTTP Status 404 - /PizzaOrderMVC/WEB-INF/.jsp view name is not passed as prefix to view resolver. any idea how to find why? – Istvan Jul 08 '15 at 16:28
  • is the index.jsp file not within a sub-folder of /WEB-INF ? i.e. my jsps are all within /WEB-INF/jsps and as such my prefix value is /WEB-INF/jsps/ – smoggers Jul 08 '15 at 16:28
  • no. they are directly in /WEB-INF/ folder. but something is wrong cause if I modify / to /test in @RequestMapping("/test"), I get: HTTP Status 404 - /PizzaOrderMVC/WEB-INF/test.jsp somehow it wants to grab uri parameter and pass to view resolver, not the view name index... – Istvan Jul 08 '15 at 16:37
  • http://stackoverflow.com/questions/22156112/spring-mvc-neither-bindingresult-nor-plain-target – Sotirios Delimanolis Jul 08 '15 at 17:34

1 Answers1

3

Ok, got the issue ! You have the wrong import for ModelAndView class in your controller. It should be

import org.springframework.web.servlet.ModelAndView;

and not

import org.springframework.web.portlet.ModelAndView;

Change this and your application would work like a charm !.

Kalyan Chavali
  • 1,330
  • 8
  • 24
  • 1
    great. it works now. thank you so much. i should be more careful with auto code completion – Istvan Jul 08 '15 at 17:18
  • This did resolve the issue but I had to ask a question to find the difference between both http://stackoverflow.com/questions/31299611/difference-between-org-springframework-web-servlet-modelandview-class-and-org-sp – Kalyan Chavali Jul 08 '15 at 17:31