如何重定向与应用程序中的任何@RequestMapping参数不匹配的任何URL

时间:2016-01-13 15:10:22

标签: spring web.xml

我在我的Web应用程序中使用Spring MVC。我正在尝试将任何与模式http://localhost:8080/my-app/*匹配的网址重定向到特定的控制器。但是,如果网址类似于以下内容,http://localhost:8080/my-app/test并且如果存在如下控制器:

@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 
    }

所有无效的网址都将被重定向到上述控制器类。

有效网址如下:

http://localhost:8080/my-app/test/landing
http://localhost:8080/my-app/test/list?ownedby=me

但如果网址如下:

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 个答案:

答案 0 :(得分:2)

如果我理解正确,您希望重定向除了例如&#34; /测试&#34 ;.在这种情况下,您需要两种方法:

@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注释。

答案 1 :(得分:1)

您的网络应用程序是如何设置的? 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 {  
    ...
}
}