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

标签 spring web.xml

我在我的 Web 应用程序中使用 Spring MVC。我正在尝试重定向任何匹配模式 http://localhost:8080/my-app/* 的 url到特定的 Controller 。但如果 url 如下所示,http://localhost:8080/my-app/test如果有如下 Controller :

@ Controller
@RequestMapping("测试")
公共(public)类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 都会被重定向到上面的 Controller 类。

有效的网址如下:
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类(class)

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

我已经在网上搜索了一段时间,但找不到太多帮助。有什么办法可以实现这样的功能。提前致谢

最佳答案

如果我对你的理解正确,你想重定向除例如之外的每个请求。 “/测试”。在这种情况下,您将需要两种方法:

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

还要记住您类(class)上的 @Controller 注释。

关于spring - 如何重定向与应用程序中的任何@RequestMapping 参数不匹配的任何 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34770156/

相关文章:

Spring 安全: Custom exception message from UserDetailsService

jsf - 使用 JSF 生成 HTTP 错误代码页面以及异常错误页面?

tomcat - 我应该如何从带有嵌入式 jetty 的 web.xml 加载 servlet?

java - session 超时未发生

java - 我想使用 Spring 在 servlet 中注入(inject)一个对象

java - 如何将 spring bean 注入(inject) Validator(hibernate)

java - 我应该在 Spring REST 应用程序中使用什么类型的安全性

Spring Security,自定义授权,AccessDecisionManager 与安全过滤器

java - { "readyState": 0 ,"status": 0 ,"statusText": "NetworkError: Failed to execute ' send' on 'XMLHttpRequest' : Failed to load 'http://localhost:8088/' .“}

java - tomcat在包路径找不到servlet