Spring Exception Handling Example

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

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago