2012-08-02 77 views
6

我想映射春天mvc控制器到根(/**)路径(而不是子文件夹,如“/东西”),同时使mvc:resources异常(打开到另一种方法) 。春天mvc网站的根(“/”)

这应该是该框架的基础知识,但显然是要求它的一个非常复杂的东西。

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"; 
    } 
} 

现在,当我打 “/” 或 “/测试” 或“/测试/页“我得到如下输出:

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

..看到了吗? service()即使在明确排除的情况下也会被呼叫/favicon.ico

现在我想在XML上有@Controller的“优先权”,但是,我该如何使排除工作?

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

P.S This answer回答一个非常类似的问题。

另一个注意:这个问题不是关于tomcat的上下文。

回答

7

这里的问题是,与<mvc:annotation-driven/>注册的相比,注册HandlerMapping<mvc:resources的优先级非常低。如果你的要求是简单的有一些应对“/”更好的办法可能会是有不同的@RequestMapping比/**,而不是说把它作为/home,并定义这些方针的东西:

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

如果这是行不通的,唯一的其他选择是降低<mvc:resources的底层handlerMapping的优先级,这可以通过明确定义HandlerMapping来完成 - 有点复杂但可以完成。

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

尝试与眼前这个第一:

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

如果不能单独工作,改变<mvc:annotation-driven/>继续沿着这条春季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> 
+0

您的解决方案是指一个单一的URL(“/” - >“家”)。我需要它是一张通配符。如果你可以慷慨解释如何提高'@ RequestMapping'的'mvc:resources'的优先级,那么我猜测,它会解决它。谢谢! – Poni 2012-08-02 22:19:45

+0

添加更新 – 2012-08-02 22:41:33

+0

工作 - 谢谢Biju! – Poni 2012-08-02 22:51:16

8

我想澄清,而不是重写<mvc:annotation-driven/>一个可以改变处理指令的声明顺序:

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