Exception Handling in Spring MVC

Spring Exception Handling Steps:

application-context.xml

1)<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
     <property name="exceptionMappings">
          <props>
              <prop key="com.tutorialspoint.SpringException">
                     ExceptionPage
              </prop>
          </props>
     </property> 
      <property name="defaultErrorView" value="error"/>
  </bean>

2)SpringException.java

public class SpringException extends RuntimeException {
/**

*/
private static final long serialVersionUID = 1L;
private String exceptionMsg;

public SpringException(String exceptionMsg) {
this.exceptionMsg = exceptionMsg;
}

public String getExceptionMsg() {
return this.exceptionMsg;
}

public void setExceptionMsg(String exceptionMsg) {
this.exceptionMsg = exceptionMsg;
}

}

3)ExceptionPage.jsp:
                     ${exception.exceptionMsg}.

4)error.jsp

Comments