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