[英]Spring MVC default-servlet-handler configuration blocking JSTL view
I have simple Spring configuration 我有简单的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" />
And my controller is 我的控制器是
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";
}
}
And my JSP is 而我的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>
This configuration works fine (CSS does not get applied though). 此配置工作正常(尽管CSS未应用)。
So I add 所以我加
<mvc:default-servlet-handler />
to my Spring configuration and now the page itself stops loading giving 404. 到我的Spring配置,现在页面本身停止加载,显示404。
Also surprisingly everything works fine (with CSS) will following config 同样令人惊讶的是,一切正常(使用CSS)将遵循配置
<mvc:view-controller path="/test" view-name="test"/>
<mvc:default-servlet-handler />
Direct rendering no controller involvement. 直接渲染无需控制器参与。
我认为对资源进行简单配置就足够了。
<mvc:resources mapping="/resources/**" location="/resources/" />
Looks like I found the issue. 看起来我找到了问题。 Somehow the default-sevlet-handler is overriding the
DefaultAnnotationHandlerMapping
handler? 以某种方式default-sevlet-handler覆盖了
DefaultAnnotationHandlerMapping
处理程序? which is why annotation based handler is failing. 这就是基于注释的处理程序失败的原因。 Following scenarios worked for me -
以下方案对我有用-
Use <mvc:annotation-driven/>
. 使用
<mvc:annotation-driven/>
。 This seems to enable default beans including DefaultAnnotationHandlerMapping
So combination of 这似乎启用了包括
DefaultAnnotationHandlerMapping
在内的默认bean,因此
<mvc:annotation-driven/> <mvc:default-servlet-handler />
worked. 工作了。
Explicitly define the handler mapping and handler adapter you need with highest preference ( chaining ). 以最高优先级( 链接 )明确定义所需的处理程序映射和处理程序适配器。 Spring scans all handler mappings and assigns an order property Integer.MAX (if not explicitly defined) which gives it lowest preference.
Spring会扫描所有处理程序映射并分配一个顺序属性Integer.MAX(如果未明确定义),该属性赋予其最低的优先级。 Then these handler mappings are sorted based on this order.
然后,将根据此顺序对这些处理程序映射进行排序。 Also if two handler mappings are same it looks like it takes bean which is defined first.
同样,如果两个处理程序映射相同,则看起来它需要首先定义的bean。 So following worked for me -
所以以下对我有用-
<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"/>
So I am guessing default-sevlet-handler
creates it's own handler mapping which overrides the annotation one all being at same preference. 因此,我猜测
default-sevlet-handler
创建了它自己的处理程序映射,该映射覆盖了所有优先级相同的注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.