2013-03-01 159 views
1

我想了解spring的mvc中的requestmapping,我有一個應用程序,實際上我剛剛開始使用spring mvc。在春天的請求映射mvc

我有這兩個網址

第一個,並在最後一個斜槓似乎是工作的罰款。 http://localhost:8080/contactmanager/index/

結尾沒有斜槓第二個不工作

http://localhost:8080/contactmanager/index 

這第二個給了我「HTTP狀態404 - 」的錯誤,我怎麼能強制應用程序可以附加一個轉發網址結尾的斜槓?

在控制器的方法是這樣的

@RequestMapping("/index") 
public String listContacts(Map<String, Object> map) { 

    map.put("contact", new Contact()); 
    map.put("contactList", contactService.listContact()); 
    //org.springframework.web.context.ContextLoaderListener 
    //org.springframework.web.context.ContextLoaderListener 
    //org.springframework.web.servlet.DispatcherServlet 
    //org.springframework.web.servlet.DispatcherServlet 
    return "contact"; 
} 

和我的web.xml看起來像事先此

<servlet> 
    <servlet-name>contactmanager</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/contactmanager-servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>contactmanager</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

感謝。

+0

「/ **指數」 ....... – user1428716 2013-03-01 01:28:43

+0

@ user1428716問題是,如果我寫了一個正斜槓(/)在最後像索引/ /,我得到它的工作,但當我最後省略斜線像索引,然後我得到404,與它的方式可能做web.xml配置? – 2013-03-01 08:12:32

回答

2

用java配置:

@Configuration 
@EnableWebMvc 
public class WebConfig {} 

,或者使用XML配置

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

<mvc:annotation-driven /> 

Spring's reference documentation

在上述寄存器一個熱曲在支持與諸如@RequestMapping, @ExceptionHandler,和其他人使用的註解 控制器方法的處理請求的estMappingHandlerMapping,一個 RequestMappingHandlerAdapter和ExceptionHandlerExceptionResolver (等等)。

RequestMappingHandlerMapping有有用的布爾標誌 - 從Javadoc中能解密:

/** 
* Whether to match to URLs irrespective of the presence of a trailing slash. 
* If enabled a method mapped to "/users" also matches to "https://stackoverflow.com/users/". 
* <p>The default value is {@code true}. 
*/ 
public void setUseTrailingSlashMatch(boolean useTrailingSlashMatch) { 
    this.useTrailingSlashMatch = useTrailingSlashMatch; 
} 

因爲這是默認啓用的只是確保您的配置使用RequestMappingHandlerMapping

如果不幫你,檢查彈簧的默認servlet配置:<mvc:default-servlet-handler/>並嘗試添加到web.xml

<!-- Disables Servlet Container welcome file handling. Needed for compatibility with Servlet 3.0 and Tomcat 7.0 --> 
<welcome-file-list> 
    <welcome-file></welcome-file> 
</welcome-file-list> 
+0

謝謝,我想我沒有在spring mvc中顯示整個請求映射的明確映射,現在我明白了,您可以指定控制器類或方法上的映射,並且在該方法中嵌套意味着您將類的路徑追加到一個在方法中。感謝您的領導 – 2013-04-07 23:19:49

+0

對於那些使用擴展'WebMvcConfigurerAdapter'的配置類的人,請重寫'configurePathMatch(PathMatchConfigurer configurer)'方法並設置'configurer.setUseTrailingSlashMatch(false);'。默認值是'true',因爲您無疑會發現您是否正在閱讀這個問題和答案。 – Paul 2016-01-03 20:59:25