we will learn Spring MVC Form handling without using database. Here, we will use @Controler, @RequestMapping and @ModelAttribute annotations.

To display the input form, we are going to use <form:form> tag of spring framework. Let’s see a simple example to store form data in a model object and display data of a list.

Following is the content of Student.java file.

Student.java

Following is the content of StudentController.java file.

StudentController.java

Here the first service method showform(), we have passed a blank Student object in the ModelAndView object with the name “command” because the Spring framework expects an object with the name “command” if you are using tags in your JSP file. So, when showform() method is called , it returns studentform.jsp view.

The second service method save() will be called against a POST method on the MVCFormExample/studentform URL. You will prepare your model object based on the submitted information. Finally a result view will be returned from the service method viewstudent(), which will result in rendering viewstudent.jsp.

Following is the content of Spring Web configuration file web.xml.

web.xml

Following is the content of another Spring Web configuration file spring-servlet.xml

spring-servlet.xml

Following is the content of index.jsp file.

index.jsp

Following is the content of studentform.jsp file.

studentform.jsp

Following is the content of viewstudent.jsp file.

viewstudent.jsp

Output

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *