2016-01-13 16 views
2

我在我的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()"/> 

我一直在尋找的淨了一段時間,但找不到太大的幫助。有什麼辦法可以實現這樣的功能。在此先感謝

回答

2

如果我正確地理解了你,你想要重定向每一個請求,而不是例如。 「/測試」。在這種情況下,您需要兩種方法:

@RequestMapping(method = RequestMethod.GET) 
public String redirectEverythingOtherThanTest(){ 
return "redirect:/pageToRedirectTo.html" 
} 

@RequestMapping(value="/test", method = RequestMethod.GET) 
public String testRequest(){ 
//some stuff 
return "somepage.html"; 
} 

還要記住關於您的類的@Controller註釋。

+0

我認爲你沒有正確理解我,所有具有有效路徑的url都應該使用正確的方法,而以http:// localhost:8080/my-app /這樣的模式開頭的無效url應該去一個不同的控制器類 –

+0

在我的示例中,沒有明確映射的所有東西(如「/ test」)將進入控制器方法redirectEverythingOtherThanTest。如果你有任何具有顯式映射的方法(「/ test」,「/ whatever」等),你的請求將達到該方法。所以:本地主機:8080 /我的應用程序/ wrongUrl將去重定向everythingOtherThanTest(),本地主機:8080 /我的應用程序/測試將去testRequest() – wcislo

+0

感謝您的更新,但我想實現一個全新的類錯誤的網址 –

1

您的Web應用程序設置如何? Spring尋找哪些網址?你有一個AbstractAnnotationConfigDispatcherServletInitializer嗎?

public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

/** 
* Configure Spring MVC to handle ALL requests 
*/ 
@Override 
protected String[] getServletMappings() { 
    return new String[] { "/" }; 
} 

@Override 
protected Class<?>[] getRootConfigClasses() { 
    .... 
} 

@Override 
protected Class<?>[] getServletConfigClasses() { 
    .... 
} 

@Override 
public void onStartup(ServletContext container) throws ServletException { 
    ... 
} 
} 
+0

我沒有AbstractAnnotationConfigDispatcherServletInitializer,並且我編輯了問題以提供web.xml文件和spring-security.xml文件中的更多詳細信息 –

+0

嘗試@RequestMapping(「/ test」)(注意「/」 ) –

相關問題