3:31 pm - Saturday January 21, 2017

Spring MVC with SimpleMappingExceptionResolver Tutorial and Example

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

Spring MVC with SimpleMappingExceptionResolver Tutorial and Example


dispatcher-Servlet.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	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-2.5.xsd">

	<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

	<!-- Register the bean -->
	<bean class="com.candidjava.controller.CustomerController" />

	<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
		<props>
			<prop key="com.candidjava.controller.GenericException">
				GenericExceptionPage
			</prop>
			<prop key="java.lang.Exception">error</prop>
		</props>
		</property>
	</bean>

	<bean id="viewResolver"
	      class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
          <property name="prefix">
              <value>/WEB-INF/jsp/</value>
           </property>
          <property name="suffix">
             <value>.jsp</value>
          </property>
    </bean>

</beans>

CustomerController.java

package com.candidjava.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.candidjava.exception.GenericException;

public class CustomerController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		throw new GenericException("Oppss...System error, please visit it later");

	}

}

GenericException.java


package com.candidjava.exception;

public class GenericException extends RuntimeException{

	private String customMsg;

	public String getCustomMsg() {
		return customMsg;
	}

	public void setCustomMsg(String customMsg) {
		this.customMsg = customMsg;
	}

	public GenericException(String customMsg) {
		this.customMsg = customMsg;
	}

}

GenericExceptionPage.jsp

</span></h2>
<pre><html>
<body>

<h2>
${exception.customMsg}
</h2>

</body>
</html>

Error.jsp

<h4 style="text-align: center;"><u>SimpleMappingExceptionResolver</u></h4>
<ol>
<li><a href="contacts.htm">Click</a> here to contacts.htm</li>
<li><a href="customer.htm">Click</a> here to customer.htm</li>
</ol>

404.jsp

<html>
<body>
<h1>Opppss..Page not found!</h1>
</body>
</html>

index.jsp


<h4 style="text-align: center;"><u>SimpleMappingExceptionResolver</u></h4>
<ol>
<li><a href="contacts.htm">Click</a> here to contacts.htm</li>
<li><a href="customer.htm">Click</a> here to customer.htm</li>
</ol>

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/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
  <javaee:display-name>Spring Web MVC Application</javaee:display-name>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
  <context-param>
    <javaee:param-name>contextConfigLocation</javaee:param-name>
    <javaee:param-value>/WEB-INF/dispatcher-servlet.xml</javaee:param-value>
  </context-param>
  <listener>
    <javaee:listener-class>
      org.springframework.web.context.ContextLoaderListener
    </javaee:listener-class>
  </listener>
  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/404.jsp</location>
  </error-page>
  <error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/WEB-INF/jsp/404.jsp</location>
  </error-page>
</web-app>
&nbsp;

Output screenshots

  Download SimpleMappingExceptionResolver  +  Spring_mvc_jar

BACK

Filed in: Z-A Spring Codes

Comments are closed.