Easy learning with example program codes

Spring MVC exception handling tutorial

Spring mvc framework provides the facility to build flexible and loosely coupled web applications. MVC stands for Model-View-Controller design pattern which separates the business logic, presentation logic and navigation logic. Let us discuss spring mvc exception handling example in eclipse.

Example Explanation:

Use http://localhost:8080/SpringMVCExample6/ student url to start the application. A request for respective resource will generate. Request will be handled by DispatcherServlet. It delegates the request to the ExceptionHandlingController controller. The LoginController controller resolve the request with help of RequestMapping annotation, executes the specific functionality and returns the ModelAndView object to the DispatcherServlet. The DispatcherServlet then take the help of InternalResourceViewResolver to get the actual view name. In our example it will return the “/WEB-INF/jsp/student.jsp. The DispatcherServlet then insert the model data into view and render response. Same request response cycle will execute when we click on submit button after entering the student details.

Note: The @ExceptionHandler annotation is used to specify the exception handlers. Use comma separated handlers if more than one exceptions are specified.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@taglib uri="" prefix="form"%>
    <title>Spring MVC exception handling example.</title>
<form:form method="POST" action="/SpringMVCExample6/addStudent">
        <td><form:label path="name">Name</form:label></td>
        <td><form:input path="name" /></td>
        <td><form:label path="className">Class</form:label></td>
        <td><form:input path="className" /></td>
        <td><form:label path="rollNo">RollNo</form:label></td>
        <td><form:input path="rollNo" /></td>
        <td colspan="2">
            <input type="submit" value="Submit"/>


<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.4"


<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns=""
   <context:component-scan base-package="" />
  <bean class=
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
  <bean class=
   <property name="exceptionMappings">
         <prop key="">
   <property name="defaultErrorView" value="error"/>

public class Student {
	private String name;
	private String className;
	private String rollNo;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getClassName() {
		return className;
	public void setClassName(String className) {
		this.className = className;
	public String getRollNo() {
		return rollNo;
	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;

public class SpringException extends RuntimeException{
   private String exceptionMsg;
   public SpringException(String exceptionMsg) {
      this.exceptionMsg = exceptionMsg;
   public String getExceptionMsg(){
      return this.exceptionMsg;
   public void setExceptionMsg(String exceptionMsg) {
      this.exceptionMsg = exceptionMsg;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
public class ExceptionHandlingController {
   @RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
      return new ModelAndView("student", "command", new Student());
   @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
   public String addStudent(@ModelAttribute("SpringWeb")Student student, 
   ModelMap model) {
      if(student.getName().length() < 5 ){
         throw new SpringException("Student name should " +
         		"contain atleast 5 characters.");
    	  model.addAttribute("name", student.getName());
      model.addAttribute("className", student.getClassName());     
      model.addAttribute("rollNo", student.getRollNo());
      return "welcome";


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <title>Spring MVC exception handling example.</title>
    <h2>Student Details</h2>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <title>Spring MVC exception handling example.</title>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <title>Spring MVC exception handling example.</title>
   <h3>Unknown exception.</h3>


Spring example6-1
Enter username with < 5 characters.
Spring example6-2
Click on Submit button.
Spring example6-3
Download this example.

Spring Tutorial

Spring framework.
Spring framework architecture.
Spring ioc container.
Spring bean.
Spring bean scopes.
Spring bean life cycle.
Spring callback methods.
Spring hello world.
Spring bean definition inheritance.
Spring bean definition template.
Spring dependency injection.
Spring constructor based injection.
Constructor injection type ambiguities.
Setter based dependency injection.
Spring dependency injection collections.
Spring autowire
Spring autowire by name
Spring autowire by type
Spring autowire by constructor
Spring JDBC tutorial
Spring JDBC Prepared Statement
Spring ResultSetExtractor
Spring RowMapper
Spring aop tutorial.
Spring AOP AspectJ Xml.
Spring AOP AspectJ Annotation.
Spring MVC tutorial.
Spring mvc framework.
Spring mvc configuration file.
Spring mvc hello world.
Spring MVC multiple controller.
Spring MVC login.
Spring mvc form handling.
Spring mvc exception handling.
Spring spel tutorial.
Spring spel hello world.
Spring spel operators.
Spring spel ternary operator.
Spring spel standardevaluationcontext.
Spring spel bean reference.
Spring spel method invocation.
Spring spel list, map.
Spring spel regex.
Maven Eclipse Spring
Spring boot overview
Spring boot architecture diagram
Spring boot components
Spring boot starter parent
Spring boot web app configuration
Run spring boot application
Spring boot change port
Spring boot change context path
Spring boot log sql statements
Spring boot hello world
Spring boot JSP
Spring boot thymeleaf
Spring boot with mysql
Spring security overview
Spring security architecture
Spring security maven dependency
Spring security xml
Spring security annotation
Spring security custom login xml
Spring security custom login annotation
Spring security form login
Spring security remember me
Spring security method level

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki