【发布时间】:2015-07-08 16:06:59
【问题描述】:
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 则显示:
也在调试中我看到正确传递了视图名称:
【问题讨论】:
-
你可以清理项目并重新运行吗?一切似乎都很好..
-
清理项目后同样的问题:HTTP 状态 404 - /PizzaOrderMVC/WEB-INF/.jsp 视图名称未作为前缀传递给视图解析器。知道如何找到原因吗?
-
index.jsp 文件是否不在 /WEB-INF 的子文件夹中?即我的 jsps 都在 /WEB-INF/jsps 内,因此我的前缀值为
/WEB-INF/jsps/ -
没有。它们直接位于 /WEB-INF/ 文件夹中。但是如果我在 @RequestMapping("/test") 中将 / 修改为 /test 会出现问题,我会得到: HTTP Status 404 - /PizzaOrderMVC/WEB-INF/test.jsp 它想以某种方式获取 uri 参数并传递给查看解析器,而不是视图名称索引...
标签: java spring model-view-controller viewrendering