Spring MVC中的InternalResourceViewResolver犯规获得前缀(spr

2019-10-23 17:16发布

Spring MVC中的InternalResourceViewResolver犯规从控制器获得的前缀,但后缀。

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

控制器:

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

}

如果我修改后缀来的index.jsp,它工作得很好。 如果离开后缀的.jsp它表明:

还在调试我看到视图名称正确传递:

Answer 1:

好,我知道的问题! 您有ModelAndView的类错误的进口在您的控制器。 它应该是

import org.springframework.web.servlet.ModelAndView;

并不是

import org.springframework.web.portlet.ModelAndView;

改变这一点,您的应用程序将工作就像一个魅力!



文章来源: spring mvc InternalResourceViewResolver doesnt get prefix