我在我的web應用程序中使用Spring MVC。我正在嘗試將任何匹配http://localhost:8080/my-app/*
模式的網址重定向到特定的控制器。但是,如果該URL是類似於以下,http://localhost:8080/my-app/test
如果有一個控制器如下:如何在應用程序中重定向任何與任何@RequestMapping參數不匹配的url
@Controller @RequestMapping( 「測試」) 公共類識別TestClass {
@RequestMapping(value = "/landing", method = RequestMethod.GET)
public String selectHomePage(ModelMap model)
{
//do something
}
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String listFaqCollections(@ModelAttribute("ownedby") String ownedBy, ModelMap model)
{
//do something
}
}
而且我有另一類如下:
@Controller
public class InvalidUrslRedirect
{
@RequestMapping(method = RequestMethod.GET)
public String redirectInvalidUrls(Model model)
{
//do something
}
所有無效的URL將被重定向到上述控制器類。
有效的URL將如下所示:
http://localhost:8080/my-app/test/landing
http://localhost:8080/my-app/test/list?ownedby=me
但如果URL是這樣的:
http://localhost:8080/my-app/test/list?ownedbys=me
顯示正常Tomcat的404錯誤頁面,並在它沒有被重定向到在InvalidUrslRedirect
類
在我web.xml
文件,我有以下幾點:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/core/root-context.xml, classpath:spring/core/spring-security.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在spring-security.xml
,我有:
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/**" access="isFullyAuthenticated()"/>
我一直在尋找的淨了一段時間,但找不到太大的幫助。有什麼辦法可以實現這樣的功能。在此先感謝
我認爲你沒有正確理解我,所有具有有效路徑的url都應該使用正確的方法,而以http:// localhost:8080/my-app /這樣的模式開頭的無效url應該去一個不同的控制器類 –
在我的示例中,沒有明確映射的所有東西(如「/ test」)將進入控制器方法redirectEverythingOtherThanTest。如果你有任何具有顯式映射的方法(「/ test」,「/ whatever」等),你的請求將達到該方法。所以:本地主機:8080 /我的應用程序/ wrongUrl將去重定向everythingOtherThanTest(),本地主機:8080 /我的應用程序/測試將去testRequest() – wcislo
感謝您的更新,但我想實現一個全新的類錯誤的網址 –