2012-08-02 98 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/>