How to handle URLs that don't appear in spring

Dispatcher Manager 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>

And the controller has a handler like

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

}

Is it possible to request a handler that does not appear in the controller as

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

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

I tried @RequestMapping(value="*",method=RequestMethod.GET)but doest work

+4
source share
2 answers

Since 404 (page not found) actually throws an exception at the web container level, containers usually provide an exception handling mechanism, so you can try exception handling (or the so-called error) as shown below;

First create a controller

@Controller
public class PageNotFoundErrorController {

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

and configure web.xml to map the error to the controller written above;

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

, 403, 500 web.xml .

, , ( ); http://www.mkyong.com/spring-mvc/spring-mvc-exception-handling-example/

+2

, , .

 //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>
  • .
  • , .
+1

All Articles