简体   繁体   English

Spring MVC default-servlet-handler配置阻止JSTL视图

[英]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 - 以下方案对我有用-

  1. 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. 工作了。

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

相关问题 <mvc:default-servlet-handler />阻塞控制器 - <mvc:default-servlet-handler /> blocking controller 将default-servlet-handler放在Spring MVC配置中的位置 - Where to put default-servlet-handler in Spring MVC configuration Spring MVC-配置:找不到元素[default-servlet-handler]的BeanDefinitionParser - Spring MVC - Configuration : Cannot locate BeanDefinitionParser for element [default-servlet-handler] 基于Spring注释的配置中的mvc:default-servlet-handler的等价物? - Equivalent of mvc:default-servlet-handler in Spring annotation-based configuration? 为什么Spring(4)mvc:default-servlet-handler会覆盖控制器映射? - Why Spring(4) mvc:default-servlet-handler overrides controller mapping? 尝试使用mvc:resources或mvc:default-servlet-handler时,Spring MVC返回XmlBeanDefinitionStoreException - Spring MVC returning XmlBeanDefinitionStoreException when trying to use mvc:resources or mvc:default-servlet-handler 如何使用default-servlet-handler - How to use default-servlet-handler 通配符匹配非常全面,但是找不到元素&#39;mvc:default-servlet-handler&#39;语句) - Wildcard match is very comprehensive, but can not find the element 'mvc: default-servlet-handler' statement) Spring Web MVC Java配置 - 默认Servlet名称 - Spring Web MVC Java Configuration- Default Servlet Name spring mvc默认映射处理程序 - spring mvc default mapping handler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM