[英]Spring MVC default-servlet-handler configuration blocking JSTL view
我有簡單的Spring配置
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- Scan for components under this package -->
<context:component-scan base-package="com.osfg.test" />
我的控制器是
package com.osfg.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author athakur
*/
@Controller
public class TestController {
@RequestMapping(value="/test", method=RequestMethod.GET)
public String welcome() {
return "test";
}
}
而我的JSP是
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>OSFG Test Page</title>
<link href="CSS/test.css" rel="stylesheet">
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
所以我加
<mvc:default-servlet-handler />
到我的Spring配置,現在頁面本身停止加載,顯示404。
同樣令人驚訝的是,一切正常(使用CSS)將遵循配置
<mvc:view-controller path="/test" view-name="test"/>
<mvc:default-servlet-handler />
直接渲染無需控制器參與。
我認為對資源進行簡單配置就足夠了。
<mvc:resources mapping="/resources/**" location="/resources/" />
看起來我找到了問題。 以某種方式default-sevlet-handler覆蓋了DefaultAnnotationHandlerMapping
處理程序? 這就是基於注釋的處理程序失敗的原因。 以下方案對我有用-
使用<mvc:annotation-driven/>
。 這似乎啟用了包括DefaultAnnotationHandlerMapping
在內的默認bean,因此
<mvc:annotation-driven/> <mvc:default-servlet-handler />
工作了。
以最高優先級( 鏈接 )明確定義所需的處理程序映射和處理程序適配器。 Spring會掃描所有處理程序映射並分配一個順序屬性Integer.MAX(如果未明確定義),該屬性賦予其最低的優先級。 然后,將根據此順序對這些處理程序映射進行排序。 同樣,如果兩個處理程序映射相同,則看起來它需要首先定義的bean。 所以以下對我有用-
<mvc:default-servlet-handler /> <bean id="annotationUrlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="order" value="0" /> </bean> <bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
因此,我猜測default-sevlet-handler
創建了它自己的處理程序映射,該映射覆蓋了所有優先級相同的注釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.