The following example explains the readers on how to use Error Handling and Validatorsin forms using Spring Web MVC framework. Firstly, a working Eclipse IDE should be in place and follow the steps below to create a Dynamic Form based Web Application
Step |
Description |
1 |
Create a project with a nameHelloWebunder a packagecom.wisdomjobs as explained in theSpring MVC - Hello World Examplechapter. |
2 |
Create a Java classesStudent,StudentControllerunder thecom. wisdomjobs package. |
3 |
Create a view filesstudent.jsp,result.jspunderjspsub-folder. |
4 |
The final step is to create the content of all the source and configuration files and export the application as explained below. |
In the above example, we are creating a service method called student(). We are passing a blank Student object in ModelAndView object. In this object, command name is specified because the framework expects an object with “command” name if we are using <form:form> tags in JSP file. addStudent.jsp view is returned if student() method is called.
addStudent() is the next method which is called against a POST method on HelloWeb/addStudent URL. Model object is prepared based on the submitted information. A “result” view is returned from service method at the end which results in showing result.jsp. If any errors occurred during validation then “addStudent” view is returned. BindingResult is used to inject errors automatically in view.
We use<form:errors />tag with path=”*” to display error messages. Example below-
It will present the error messages for all the input validations.
Also we use <form:errors /> tag with path="name" to present error message for the name field. Example below-
It will present the error messages for the name field validations.
Once the source and configuration files are created export the application. Do right click on the application and Export > WAR File option and save HelloWeb.war file in Tomcat’s webapps folder.
Start the Tomcat Server and using a standard browser check if you are having access to other web pages from webapps folder. Try the URL http://localhost:8080/HelloWeb/addStudent and you must see the below result if everything is fine with Spring Web Application:
Submit the form once the required information is filled by clicking Submit button. You must see the below result if everything is fine with Spring Web Application:
|
|
Spring MVC Framework Related Tutorials |
|
---|---|
J2EE Tutorial | Hibernate Tutorial |
MVC Framework Tutorial | Framework7 Tutorial |
Maven Tutorial | JUnit Tutorial |
Log4j Tutorial |
Spring MVC Framework Related Interview Questions |
|
---|---|
J2EE Interview Questions | Hibernate Interview Questions |
MVC Framework Interview Questions | Framework7 Interview Questions |
Maven Interview Questions | JUnit Interview Questions |
Log4j Interview Questions | Spring MVC Framework Interview Questions |
Java J2ee Architect Interview Questions | Spring Boot Interview Questions |
Spring Batch Interview Questions | Spring Security Interview Questions |
Spring MVC Framework Related Practice Tests |
|
---|---|
J2EE Practice Tests | Hibernate Practice Tests |
Maven Practice Tests | JUnit Practice Tests |
Log4j Practice Tests | Spring MVC Framework Practice Tests |
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.