<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">In this example show how to write a simple web based application using <b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/">Spring MVC Framework</a></b>, which can handle one or more exceptions raised inside its controllers. To start with it, let us have working STS IDE in place and follow the following steps to develop a Dynamic Form based Web Application using Spring Web Framework:</p>
<div align='center' id="ads-id"></div>
<div style="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;">
<span style="color: red;font-size: x-large;text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p><b>Step 1:</b> Create a <b><i>Dynamic Web Project</i></b> with a name <b><i>SpringMVCExecptionApp</i></b> and create a package <i><b>com</b>.<b>dineshonjava.controller</b></i> under the <b><i>src</i> </b>folder in the created project.</p>
<p><b>Step 2: </b>Add below mentioned <b>Spring 3.0 libraries</b> and other libraries into the folder <b><i>WebRoot/WEB-INF/lib</i></b>. ;</p>
<ul class="list">
<li>commons-logging-1.1.1.jar</li>
<li>org.springframework.asm-3.0.0.jar</li>
<li>org.springframework.beans-3.0.0.jar</li>
<li>org.springframework.context-3.0.0.jar</li>
<li>org.springframework.core-3.0.0.jar</li>
<li>org.springframework.expression-3.0.0.jar</li>
<li>org.springframework.web.servlet-3.0.0.jar</li>
<li>org.springframework.web-3.0.0.jar</li>
<li>spring-web.3.0.0.jar</li>
</ul>
<p> ;<b>Step 3: </b>Create a Java class <b><i>EmployeeController</i> </b>and <i><b>Employee</b></i> under the <i><b>com</b>.<b>dineshonjava.controller</b></i> <i><b>com</b>.<b>dineshonjava.bean </b></i>package respectively.</p>
<p><b>Step 4:</b> Create Spring configuration files <b><i>Web.xml</i></b> and <b><i>sdnext-servlet.xml</i></b> under the <b><i>WebRoot/WEB-INF/ </i></b>folder.</p>
<p><b>Step 5:</b> Create a sub-folder with a name <i><b>views </b></i>under the <i><b>WebRoot/WEB-INF </b></i>folder. Create a view file <b><i>employeeForm.jsp</i></b> and <i><b>employeeDetail.jsp</b></i> under this sub-folder.</p>
<p><b>Step <i>6</i>:</b> The final step is to create the content of all the source and configuration files name <i><b>sdnext-servlet.xml</b></i> under the sub-folder <i><b>WebRoot/WEB-INF/config </b></i>and export the application as explained below.</p>
<p><b>Software versions used to run the sample code:</b></p>
<ul>
<li>Spring 3.0</li>
<li>Java 1.6</li>
<li>Tomcat 7</li>
<li>JSTL 1.2</li>
<li><b>STS</b> Java EE IDE</li>
</ul>
<p><b><i> ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; </i>Web Application Structure:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2012/12/mvcException.jpg"/></div>
<p></p>
<div class="separator" style="clear: both; text-align: center;"></div>
<p><b> ;</b><b> Employee.java</b></p>
<pre class="highlight" name="code">package com.dineshonjava.emp.bean; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class Employee { 
 private int empId; 
 private String name; 
 private Long salary; 
 private int age; 
 /** 
 * @return the empId 
 */ 
 public int getEmpId() { 
 return empId; 
 } 
 /** 
 * @param empId the empId to set 
 */ 
 public void setEmpId(int empId) { 
 this.empId = empId; 
 } 
 /** 
 * @return the name 
 */ 
 public String getName() { 
 return name; 
 } 
 /** 
 * @param name the name to set 
 */ 
 public void setName(String name) { 
 this.name = name; 
 } 
 /** 
 * @return the salary 
 */ 
 public Long getSalary() { 
 return salary; 
 } 
 /** 
 * @param salary the salary to set 
 */ 
 public void setSalary(Long salary) { 
 this.salary = salary; 
 } 
 /** 
 * @return the age 
 */ 
 public int getAge() { 
 return age; 
 } 
 /** 
 * @param age the age to set 
 */ 
 public void setAge(int age) { 
 this.age = age; 
 } 
 
 public String toString(){ 
 return "Employee{ name-"+name+" age-"+age+" salary-"+salary+"}"; 
 } 
} 
</pre>
<p><b>EmployeeException.java</b></p>
<pre class="highlight" name="code">package com.dineshonjava.emp.exception; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class EmployeeException extends RuntimeException { 
 
 private static final long serialVersionUID = -4794572499177930357L; 
 
 private String exceptionMsg; 
 
 public EmployeeException(String exceptionMsg) { 
 this.exceptionMsg = exceptionMsg; 
 } 
 
 public String getExceptionMsg(){ 
 return this.exceptionMsg; 
 } 
 
 public void setExceptionMsg(String exceptionMsg) { 
 this.exceptionMsg = exceptionMsg; 
 } 
} 
</pre>
<p><b>EmployeeController.java</b></p>
<pre class="highlight" name="code">package com.dineshonjava.emp.controller; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
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; 
 
import com.dineshonjava.emp.bean.Employee; 
import com.dineshonjava.emp.exception.EmployeeException; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@Controller 
public class EmployeeController { 
 
 @RequestMapping(value = "/employee", method = RequestMethod.GET) 
 public ModelAndView employee() { 
 return new ModelAndView("employeeForm", "command", new Employee()); 
 } 
 
 @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) 
 public String addEmployee(@ModelAttribute("SpringWeb")Employee employee, ModelMap model) { 
 
 if(employee.getName().length() <; 5 ){ 
 throw new EmployeeException("Given name is too short"); 
 }else{ 
 model.addAttribute("name", employee.getName()); 
 } 
 if( employee.getAge() <; 10 ){ 
 throw new EmployeeException("Given age is too low"); 
 }else{ 
 model.addAttribute("age", employee.getAge()); 
 } 
 model.addAttribute("empId", employee.getEmpId()); 
 model.addAttribute("salary", employee.getSalary()); 
 return "employeeDetail"; 
 } 
} 
</pre>
<p>Spring Web configuration file <i><b>web.xml</b></i></p>
<pre class="highlight" name="code"><;web-app id="WebApp_ID" version="3.0" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">; 
 
 <;display-name>;SpringMVCExceptionApp<;/display-name>; 
 <;welcome-file-list>; 
 <;welcome-file>;/<;/welcome-file>; 
 <;/welcome-file-list>; 
 
 <;servlet>; 
 <;servlet-name>;sdnext<;/servlet-name>; 
 <;servlet-class>; 
 org.springframework.web.servlet.DispatcherServlet 
 <;/servlet-class>; 
 <;init-param>; 
 <;param-name>;contextConfigLocation<;/param-name>;<;param-value>;/WEB-INF/config/sdnext-servlet.xml<;/param-value>;<;/init-param>; 
 <;load-on-startup>;1<;/load-on-startup>; 
 <;/servlet>; 
 <;servlet-mapping>; 
 <;servlet-name>;sdnext<;/servlet-name>; 
 <;url-pattern>;/<;/url-pattern>; 
 <;/servlet-mapping>; 
<;/web-app>; 
</pre>
<p>Spring Web configuration file <b><i>sdnext-servlet.xml</i></b></p>
<pre class="highlight" name="code"><;beans xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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 
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">; 
 
<;!-- Enable annotation driven controllers, validation etc... -->; 
<;mvc:annotation-driven>;<;/mvc:annotation-driven>; 
 
<;context:component-scan base-package="com.dineshonjava.emp.controller">; 
<;/context:component-scan>; 
 
<;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">; 
 <;property name="prefix" value="/WEB-INF/views/">;<;/property>; 
 <;property name="suffix" value=".jsp">;<;/property>; 
<;/bean>; 
 
<;bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">; 
 <;property name="exceptionMappings">; 
 <;props>; 
 <;prop key="com.dineshonjava.emp.exception.EmployeeException">; 
 ExceptionPage 
 <;/prop>; 
 <;/props>; 
 <;/property>; 
 <;property name="defaultErrorView" value="error">;<;/property>; 
<;/bean>; 
 
<;/beans>; 
</pre>
<p>Here you specified ExceptionPage as an exception view in case EmployeeException occurs, if there is any other type of exception then a generic view error will take place.<br />
<b>employeeForm.jsp</b></p>
<pre class="highlight" name="code"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>; 
 
<;html>; 
 <;head>; 
 
 <;title>;Spring MVC Form Handling<;/title>; 
 <;/head>; 
 &;body>; 
<h2>Employee Data Form</h2><;form:form action="/sdnext/addEmployee" method="POST">; 
<;table>;<;tbody>; 
<;tr>; <;td>;<;form:label path="empId">;Employee :<;/form:label>;<;/td>; <;td>;<;form:input path="empId">;<;/form:input>;<;/td>; <;/tr>; 
<;tr>; <;td>;<;form:label path="name">;EmployeeName:/form:label&;gt;<;/form:label>;<;/td>; <;td>;<;form:input path="name">;<;/form:input>;<;/td>; <;/tr>; 
<;tr>; <;td>;<;form:label path="age">;Employee Age:<;/form:label>;<;/td>; <;td>;<;form:input path="age">;<;/form:input>;<;/td>; <;/tr>; 
<;tr>; <;td>;<;form:label path="salary">;Employee Salary:<;/form:label>;<;/td>; <;td>;<;form:input path="salary">;<;/form:input>;<;/td>; <;/tr>; 
<;tr>; <;td colspan="2">;&;lt;input type="submit" value="Submit"/&;gt; <;/td>; <;/tr>; 
<;/tbody>;<;/table>; 
<;/form:form>; 
<;/body>; 
<;/html>; 
</pre>
<p><b>employeeDetail.jsp</b></p>
<pre class="highlight" name="code"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>; 
 
<;html>; 
 <;head>; 
 <;title>;Spring MVC Form Handling<;/title>; 
 <;/head>; 
<;body>; 
<;h2>; 
Submitted Employee Information<;/h2>; 
<;table border="1">;<;tbody>; 
<;tr>; <;td>;Employee ID <;/td>; <;td>;${empId}<;/td>; <;/tr>; 
<;tr>; <;td>;Employee Name<;/td>; <;td>;${name}<;/td>; <;/tr>; 
<;tr>; <;td>;Employee Age<;/td>; <;td>;${age}<;/td>; <;/tr>; 
<;tr>; <;td>;Employee Salary<;/td>; <;td>;${salary}<;/td>; <;/tr>; 
<;/tbody>;<;/table>; 
<;/body>; 
<;/html>; 
</pre>
<p>ExceptionPage.jsp</p>
<pre class="highlight" name="code"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
 
<;html>; 
<;head>; 
 
<;title>;Spring MVC Exception Handling<;/title>; 
<;/head>; 
<;body>; 
 <h2>Spring MVC Exception Handling</h2><h3>${exception.exceptionMsg}</h3><;/body>; 
<;/html>; 
</pre>
<p>error.jsp</p>
<pre class="highlight" name="code"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
 
<;html>; 
<;head>; 
 <;title>;Spring Error Page<;/title>; 
<;/head>; 
<;body>; 
 
An error occurred, please contact dineshonjava at admin@dineshonjava.com 
<;/body>; 
<;/html>; 
</pre>
<p>Once you are done with creating source and configuration files, export your application. Right click on your application and use <b>Export->; WAR</b> File option and save your<i><b> SpringExceptionApp.war </b></i>file in Tomcat&#8217;s webapps folder.</p>
<p> Now start your Tomcat server and make sure you are able to access other web pages from webapps folder using a standard browser. Now try a URL <i><b>http://localhost:8080/sdnext/employee</b></i> and you should see the following result if everything is fine with your Spring Web Application:</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2012/12/exceptionmvc.jpg"/></div>
<p> ;After submitting required information click on submit button to submit the form. You should see the following result if everything is fine with your Spring Web Application:</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2012/12/errormvc.jpg"/></div>
<p> ;Now enter the age to low the see what happens.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2012/12/errormvcage.jpg"/></div>
<p>
 ;After submitting required information click on submit button to submit the form. You should see the following result if everything is fine with your Spring Web Application:</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2012/12/erroragemvc.jpg" /></div>
<p> ;If every thing is ok what ever we enter into the employee form then after submitting for we will find the following output:</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2012/12/okmvc.jpg"/></div>
<p> ;If we submit such data into form which give exception beyond the EmployeeException then we will get the following output:</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2012/12/errorNaN.jpg"/></div>
<p>
 ;If every thing is ok then after submitting for we will find the following output:</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2012/12/NaNMvc.jpg"/></div>
<p>
 <b>Download Source code</b><br />
<b><a href="https://sites.google.com/site/dinesh9582486434/my-forms/SpringMVCExecptionApp.zip?attredirects=0&;d=1" target="_blank">SpringMVCExecptionApp.zip</a> </b></p>
<div style="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;">
<span style="color: red;font-size: x-large;text-align: center;"><b>Spring MVC Related Posts</b></span></p>
<ol style="text-align: left;"><span style="color: #274e13;"></p>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><span style="color: red;">Spring MVC Web Tutorial</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-interview-questions-and-answers/"><span style="color: red;">Spring MVC Interview Questions</span></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-mvc/"><span style="color: red;">MVC Design Pattern</span></a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-dispatcherservlet-in-spring-and-its-uses/"><span style="color: red;">Spring MVC DispatcherServlet </span></a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc/"><span style="color: red;">Spring MVC WebApplicationContext and Root Application Context</span></a></b></li>
<li><b><a href="https://dineshonjava.com/stereotype-annotations-in-spring/"><span style="color: red;">Spring MVC @Controller Annotation</span></a></b></li>
<li><b><a href="https://dineshonjava.com/requestmapping-annotation-in-spring-mvc/"><span style="color: red;">Spring MVC @RequestMapping Annotation</span></a></b></li>
<li><b><a href="https://dineshonjava.com/requestparam-annotation-in-spring-mvc-with-example/"><span style="color: red;">Spring MVC @RequestParam Annotation</span></a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc/"><span style="color: red;">Spring MVC ContextLoaderListener</span></a></b></li>
<li><b><a href="https://dineshonjava.com/requestparam-vs-pathvariable-annotations-in-spring-mvc/"><span style="color: red;">Spring MVC @RequestParam and @PathVariable annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-30-mvc-hello-world-example/"><span style="color: red;">Spring MVC Hello World Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-exception-handling-example/"><span style="color: red;">Spring MVC Exception Handling Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-with-hibernate-crud-example/"><span style="color: red;">Spring MVC with Hibernate CRUD Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-3-mvc-tiles-plugin-with-example/"><span style="color: red;">Spring MVC Tiles Plugin with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-3-mvc-and-interceptor-with/"><span style="color: red;">Spring MVC Interceptor with example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/integration-spring-mvc3-and-mongodb/"><span style="color: red;">Spring MVC with MongoDB CRUD Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-3-mvc-internationalization/"><span style="color: red;">Spring MVC Internationalization &#038; Localization with Example</span></a></b></li>
<p></span></ol>
</div>
<p></p>
<div style="text-align: center;"><b><;<;</b><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/">Spring Web MVC Framework</a></b><b> |<a href="https://dineshonjava.com/spring-tutorial/">index</a>| </b><b><a href="https://dineshonjava.com/spring-mvc-with-hibernate-crud-example/" target="_blank">Spring 3.0 MVC with Hibernate 3.0 CRUD Example</a></b><b>>;>;</b></div>
</div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-static-pages-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-mvc-with-hibernate-crud-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '618'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…