根上的spring mvc网站(“ /”)


问题内容

我想将spring mvc控制器映射到root(/**)路径(而不是子文件夹,例如“ /
something”),同时使用mvc:resources(对另一种方法开放)例外。

这应该是该框架的基础知识,但显然是一个非常复杂的问题。

我的app-servlet.xml有以下明显的映射异常:

<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />
<mvc:resources mapping="/robots.txt" location="/robots.txt" />

我有这个控制器:

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/**")
public class MainController {

    @RequestMapping(method = RequestMethod.GET)
    public String service(final HttpServletRequest request) {
        final String servlet_path = request.getServletPath();
        System.out.println(String.format("%s %s", new Date().toString(), servlet_path));
        return "test";
    }
}

现在,当我点击“ /”或“ / test”或“ / test / page”时,我得到如下输出:

Fri Aug 03 00:22:12 IDT 2012 /
Fri Aug 03 00:22:13 IDT 2012 /favicon.ico

..看到了吗?即使service()/favicon.ico明确排除在外也被要求!

现在,我想@ControllerXML 有一些“优先权” ,但是,如何使排除工作起作用?

一个简单的要求-网站位于“ /”上。

另一个说明:这个问题与tomcat上下文无关。


问题答案:

这里的问题是,与注册的基础HandlerMapping<mvc:resources相比,其优先级非常低<mvc:annotation- driven/>。如果您只需要让某些内容响应“
/”,则更好的方法可能是使用不同的@RequestMapping,/**而不是像这样说/home并按以下方式定义内容:

<mvc:view-controller path="/" view-name="home" />

如果这不起作用,则唯一的其他选择是降低的基础handlerMapping的优先级<mvc:resources,这可以通过显式定义HandlerMapping来完成-
有点复杂,但是可以做到。

已更新 这里是一个可能的配置:

首先尝试一下:

<mvc:resources mapping="/favicon.ico" location="/favicon.ico" order="0"/>
<mvc:resources mapping="/robots.txt" location="/robots.txt" order="0"/>

如果仅此一项不起作用,请<mvc:annotation-driven/>针对Spring 3.1.x 更改为以下内容:

<bean name="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="conversionService" ref="conversionService"></property>
            <property name="validator">
                <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
                    <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
                </bean>
            </property>
        </bean>
    </property>
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
        </list>
    </property>
</bean>

<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="order" value="2"></property>
</bean>