如何处理未在spring中映射的url

时间:2013-11-29 11:03:56

标签: spring spring-mvc url-mapping

我的调度程序servlet映射

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/springconfig/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>

控制器有像

这样的处理程序
@RequestMapping("moduleone")
public class ApplicationController {    

    @RequestMapping(value="Login.html",method=RequestMethod.GET)
    public ModelAndView showLoginPage(){

        ModelAndView mv=new ModelAndView("../moduleone/Login");
        mv.addObject("loginForm", new LoginForm());
        return mv;

    }
    @RequestMapping(value="Home.html", method = RequestMethod.GET)
    public  ModelAndView showHome(HttpServletRequest request)  {
        ModelAndView mv=new ModelAndView("Home");       
        mv.addObject("customerName",appCon.getFirstName() );
        return mv;  
    }   

}

是否可以处理未在控制器中映射的请求 像

  http://localhost:8090/Project/moduleone/invalidpage.html

  http://localhost:8090/Project/moduleone/invalidurl/invalidpage

我尝试了@RequestMapping(value="*",method=RequestMethod.GET)但是做了工作

2 个答案:

答案 0 :(得分:2)

由于404(页面未找到)实际上在Web容器级别上产生异常,容器通常会提供异常处理机制,因此您可以尝试异常(或所谓的错误)处理,如下所示;

首先创建一个控制器

@Controller
public class PageNotFoundErrorController {

    @RequestMapping(value="/pageNotFound.html")
    public String handlePageNotFound() {
            // do something
        return "pageNotFound";
    }
}

并配置 web.xml ,以便将错误映射到上面写的控制器;

<error-page>
    <error-code>404</error-code>
    <location>/pageNotFound.html</location>
</error-page>

你也可以通过向web.xml添加403,500和其他错误代码并将它们映射到任何控制器来扩展它。

更令人着迷的是,您还可以映射任何异常(甚至是代码创建的异常);在这里你可以找到一个很好的例子http://www.mkyong.com/spring-mvc/spring-mvc-exception-handling-example/

答案 1 :(得分:1)

我尝试了代码块,如果我改变你的场景,我可以处理它。

 //This one is OK
 http://localhost:8090/Project/moduleone/invalidpage.html
 //add invalid.html not a folder it should be file
 http://localhost:8090/Project/moduleone/invalidurl/invalidpage.html

HomeController.java

@RequestMapping(value = {"*/*.html","*.html"}, method = RequestMethod.GET)
public String test(HttpServletResponse response) throws IOException {
    return new String("home");
}

调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>TestSpringMVC</display-name>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
    <servlet-name>SpringDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/springconfig/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringDispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>
  • 我可以用这种方式处理这两个请求。
  • 我认为你应该为你的第二个场景定义一个例外页面。
  • 您也可以阅读this issue