2012-06-04 62 views
2

我有一個相當簡單的任務,我想完成,但似乎無法找到有關Spring MVC路由的信息。我有一個非常簡單的控制器的路由到一個視圖的路徑:如何在Spring MVC控制器的@RequestMapping路徑中限制路由擴展?

@Controller 
@RequestMapping(value = "/help") 
public class HelpController { 

    private static final String HELP = "help"; 

    @RequestMapping(method = RequestMethod.GET) 
    public String help(Model model, Locale locale) { 
     model.addAttribute("locale", locale); 
     return HELP; 
    } 
} 

我想拋出一個404,如果http://mysite.com/help.some.extension.is.entered,但春天似乎解決了/幫助的例子。 javadoc說@RequestMapping註釋只是一個servlet URI映射,但我認爲/幫助意味着它需要完全匹配。任何澄清將不勝感激。

回答

0

您可以在@RequestMapping註解

是相同的,因爲只有Servlet的URL模式提到它。

@Controller 
public class HelpController { 

    private static final String HELP = "help"; 

    @RequestMapping(value = "/help" method = RequestMethod.GET) 
    public String help(Model model, Locale locale) { 
     model.addAttribute("locale", locale); 
     return HELP; 
    } 

    @RequestMapping(value = "help/*" method = RequestMethod.GET) 
    public String helpWithExtraWords() { 
     return "error"; 
    } 
} 
+0

感謝您的回覆!但是,這並不能解決問題,因爲路線在前面有*。我的目標是在輸入/help.suffix時返回404。 Spring接受句點字符作爲/ help的有效擴展名。 – Roosh

0

我能想到的最好的辦法是明確地配置您的RequestMappingHandlerMapping不考慮suffixpaths,這種方式:

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

但是,如果你已經使用mvc:annotation-driven配置你的Spring MVC,這不會工作,你將不得不展開整個處理適配器的定義,這是不難做到的(這不是完整的,你可以查看整個定義的org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser):

<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> 
     </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="useSuffixPatternMatch" value="false"></property> 
</bean> 
<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> 
     </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="useSuffixPatternMatch" value="false"></property> 
</bean> 
+0

我嘗試了一些非常類似於springource論壇上的帖子。它似乎沒有區別。 http://forum.springsource.org/showthread.php?91379-RequestMapping-drops-requested-quot-file-quot-extension – Roosh

+1

這很奇怪@Roosh,這個配置適用於我,你能否確認你已經完全刪除'mvc:annotation-driven'並且只有這個handlerMapping。另外,你能告訴我你使用的Spring版本 - Spring 3.0.5的配置與我提供的不同(這是Spring 3.1+) –

+0

我使用的是Spring 3.0.6。我沒有從spring文件中刪除我的語句。我會嘗試,並讓你知道它是否有效。 – Roosh

0

使用Spring 3.0.X您可以使用useDefaultSuffixPattern屬性。

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
    <property name="useDefaultSuffixPattern" value="false" /> 
</bean> 

你將需要刪除</mvc:annotation-driven>

參考URL Pattern Restricting in SPRING MVC

+0

I當我刪除時開始獲取servlet異常。 javax.servlet.ServletException:處理程序沒有適配器[[email protected]]:你的處理程序是否實現了像Controller這樣的受支持的接口? – Roosh

+0

@Roosh您還應該使用'」這將工作! – schhajed

1

春天4這是很容易解決:

<mvc:annotation-driven> 
    <mvc:path-matching suffix-pattern="false" /> 
</mvc:annotation-driven> 

所以,你仍然可以使用mvc:annotation-driven爲你的配置。