Spring MVC default-servlet-handler配置阻止JSTL视图 [英] Spring MVC default-servlet-handler configuration blocking JSTL view

查看:169
本文介绍了Spring MVC default-servlet-handler配置阻止JSTL视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的Spring配置

I have simple Spring configuration

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

此配置工作正常(尽管未应用CSS).

This configuration works fine (CSS does not get applied though).

所以我添加

<mvc:default-servlet-handler />

恢复到我的Spring配置,现在页面本身停止加载,并显示404.

to my Spring configuration and now the page itself stops loading giving 404.

令人惊讶的是,一切正常(使用CSS)将遵循config

Also surprisingly everything works fine (with CSS) will following config

<mvc:view-controller path="/test" view-name="test"/>
<mvc:default-servlet-handler />

直接呈现,无需控制器参与.

Direct rendering no controller involvement.

推荐答案

好像我发现了问题.不知何故,default-sevlet-handler会覆盖DefaultAnnotationHandlerMapping处理程序?这就是基于注释的处理程序失败的原因.以下方案对我有用-

Looks like I found the issue. Somehow the default-sevlet-handler is overriding the DefaultAnnotationHandlerMapping handler? which is why annotation based handler is failing. Following scenarios worked for me -

  1. 使用<mvc:annotation-driven/>.这似乎启用了包括DefaultAnnotationHandlerMapping这样的

  1. Use <mvc:annotation-driven/>. This seems to enable default beans including DefaultAnnotationHandlerMapping So combination of

 <mvc:annotation-driven/>
 <mvc:default-servlet-handler />

工作.

  1. 以最高优先级(链接)明确定义所需的处理程序映射和处理程序适配器. Spring会扫描所有处理程序映射,并分配一个顺序属性Integer.MAX(如果未明确定义),该属性赋予其最低的优先级.然后,将根据此顺序对这些处理程序映射进行排序.同样,如果两个处理程序映射相同,则看起来它需要首先定义的bean.所以以下对我有用-

  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. 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. 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"/>

所以我猜想default-sevlet-handler创建了它自己的处理程序映射,该映射覆盖了所有优先级相同的注释.

So I am guessing default-sevlet-handler creates it's own handler mapping which overrides the annotation one all being at same preference.

这篇关于Spring MVC default-servlet-handler配置阻止JSTL视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆