InternalResourceViewResolver

InternalResourceViewResolver

本文介绍了Spring MVC InternalResourceViewResolver没有获得前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

spring mvc InternalResourceViewResolver不能获取前缀,但可以从控制器添加后缀.

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>

控制器:

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,则显示:

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:

推荐答案

好,问题解决了!您的控制器中的ModelAndView类导入错误.应该是

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

import org.springframework.web.servlet.ModelAndView;

而不是

import org.springframework.web.portlet.ModelAndView;

更改此设置,您的应用程序将像超级按钮一样工作!.

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

这篇关于Spring MVC InternalResourceViewResolver没有获得前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 07:06