简体   繁体   中英

spring mvc InternalResourceViewResolver doesnt get prefix

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:

在此输入图像描述

also in debug I see that view name is correctly passed: 在此输入图像描述

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 !.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM