0

hey how can I handle no-mapping found exception in spring dispatcher?? so that, if no-mapping to a controller is found in the dispatcher, I would like to redirect the user to a separate Resource not found page

Suppose I have only 2 pages in my web application, "/about", "/help" and "/not-found"..Now if a user requsts for "/home", i want to redirect the user to "/not-found" page..could anyone help me please?? i am using spring framework

2
Contributors
2
Replies
8
Views
3 Years
Discussion Span
Last Post by cool_zephyr
0

This is probably the millionth question about this Spring MVC error, but I can't get it to work still.

I am trying to map a simple controller method to /account and later on I want to add /account/{id}, but I can't even get /account to work.

Here is my web.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app 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"
    version="2.4">

    <display-name>My Spring MVC web application</display-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:application-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param>
</web-app> 
0

the mapping is done in the dispatcher so please post the dispatcher.xml file
if you want to map "/account" to controller "AccountController" in package "com.xxx.xxx" then your dispatcher should look like given below

the folder structure is :

-Java Resources
    -src
        -com.xxx.xxx
            -AccountController.java
            -IndexController.java
-WebContent
    -images
    -pages
        -account.jsp
        -index.jsp
    -styles
    -WEB-INF
        -dispatcher-servlet.xml
        -web.xml

Below is dispatcher-servlet.xml :

 <?xml version="1.0" encoding="UTF-8"?>
            <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:context="http://www.springframework.org/schema/context"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

                <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
                <bean name="/account" class="com.xxx.xxx.AccountController" />
                <bean name="/" class="com.xxx.xxx.IndexController" />
<bean name="/index" class="com.xxx.xxx.IndexController" />
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix" value="./pages/" />
                    <property name="suffix" value=".jsp" />
                </bean>
            </beans>

Your web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Your Web App Name</display-name>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/dispatcher-servlet.xml
    </param-value>
  </context-param>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>defaultYourApp</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>defaultYourApp</servlet-name>
    <url-pattern>*.jpg</url-pattern>
    <url-pattern>*.css</url-pattern>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.png</url-pattern>
    <url-pattern>*.gif</url-pattern>
  </servlet-mapping>
</web-app>

Your AccountController.java :

package com.xxx.xxx

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class AccountController {
    @RequestMapping(value="/account",method=RequestMethod.GET)
    public String getAccountPage() {
        return "account.jsp";
    }
}

Given below is IndexController.java

package com.xxx.xxx

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    @Controller
    public class IndexController {
        @RequestMapping(value="/",method=RequestMethod.GET)
        public String getAccountPage() {
            return "index.jsp";
        }

        @RequestMapping(value="/index",method=RequestMethod.GET)
        public String getAccountPage() {
            return "index.jsp";
        }
    }

Edited by cool_zephyr

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.