Announcement Announcement Module
Collapse
No announcement yet.
Custom HTTP Error 404 - Annotation based setting Spring 3.2 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom HTTP Error 404 - Annotation based setting Spring 3.2

    Hi

    If possible, how can I implement annotation based custom HTTP 404 error page using Spring 3.2.1?
    I looked for ways in various forums but couldn't find any clear answer.

    I also tried web.xml based configuration but it is not working when I access an unmapped URL.
    Any help please?

    Log
    Code:
      7259 [DEBUG] org.springframework.web.servlet.DispatcherServlet  - DispatcherServlet with name 'spring-test' processing GET request for [/spring-test/ss]
      7261 [DEBUG] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Looking up handler method for path /ss
      7262 [DEBUG] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Did not find handler method for [/ss]
      7262 [WARN ] org.springframework.web.servlet.PageNotFound  - No mapping found for HTTP request with URI [/spring-test/ss] in DispatcherServlet with name 'spring-test'
      7262 [DEBUG] org.springframework.web.servlet.DispatcherServlet  - Successfully completed request
    web.xml
    Code:
        <!-- The definition of the Spring Container shared by all Servlets and Filters -->
        <context-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </context-param>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>main.java.net.bornil.config</param-value>
        </context-param>   
        
        <!-- Processes application requests -->
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextClass</param-name>
                <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
            </init-param>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>main.java.net.bornil.config</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <!-- Creates the Spring Container shared by all Servlets and Filters -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <welcome-file-list>
            <welcome-file />
        </welcome-file-list>
    
        <error-page>
            <error-code>404</error-code>
            <location>/error/404</location>
        </error-page>
    </web-app>
    Controller class
    Code:
    @Controller
    @RequestMapping(value = "/error")
    public class CommonExceptionHandler {
    
        private static Logger log = Logger.getLogger(CommonExceptionHandler.class.getName());
    
        @RequestMapping(method = RequestMethod.GET, value = "/{code}")
        public ModelAndView handleException(@PathVariable int code) {
    
            if (log.isDebugEnabled()) {
                log.debug("ERROR CODE IS: " + code);
            }
    
            return new ModelAndView("errors/404");
        }
    
    }
Working...
X