The following content is translated from: https://www.tutorialspoint.com/springmvc/springmvc_form_handling.htm
Description: The sample is based on spring MVC 4.1.6.
The following example shows how to use the Spring Web MVC framework to write a simple web-based application that uses HTML forms. First, let's use the Eclipse IDE and follow these steps to develop a dynamic form-based Web application using the Spring Web framework:
Steps |
Description |
1 |
Create a project named Helloweb, under a package com.tutorialspoint, as described in the Spring Mvc-hello World Example section. |
2 |
Create a Java class Student,studentcontroller under the Com.tutorialspoint package. |
3 |
Create a view file under the JSP subfolder student.jsp,result.jsp. |
4 |
The final step is to create the contents of all the source and configuration files and export the application as described below. |
Student.java
PackageCom.tutorialspoint; Public classStudent {PrivateInteger age; PrivateString name; PrivateInteger ID; Public voidsetage (Integer age) { This. Age =Age ; } PublicInteger getage () {returnAge ; } Public voidsetName (String name) { This. Name =name; } PublicString GetName () {returnname; } Public voidsetId (Integer id) { This. ID =ID; } PublicInteger getId () {returnID; }}
Studentcontroller.java
PackageCom.tutorialspoint;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.web.bind.annotation.ModelAttribute;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.ui.ModelMap; @Controller Public classStudentcontroller {@RequestMapping (value= "/student", method =requestmethod.get) PublicModelandview Student () {return NewModelandview ("Student", "command",NewStudent ()); } @RequestMapping (Value= "/addstudent", method =requestmethod.post) PublicString addstudent (@ModelAttribute ("Springweb") Student Student, Modelmap model) {Model.addattribute ("Name", Student.getname ()); Model.addattribute ("Age", Student.getage ()); Model.addattribute ("id", Student.getid ()); return"Result"; }}
Here the first service method student (), we have passed a blank student object named "command" in the Modelandview object, because if you use <form in a JSP: Form> tags, the spring framework expects an object file called "command". So when the student () method is called, the student.jsp view is returned.
The second service method , Addstudent (),is called on the helloweb/addstudent URL for the Post method. You will prepare your model objects based on the information submitted. Finally, the results view is returned from the service method, which causes the rendering result.jsp
student.jsp
<%@taglib URI="Http://www.springframework.org/tags/form"prefix="form"%><HTML><Head> <title>Spring MVC Form Handling</title></Head><Body><H2>Student Information</H2><Form:formMethod= "POST"Action= "/helloweb/addstudent"> <Table> <TR> <TD><Form:labelPath= "Name">Name</Form:label></TD> <TD><Form:inputPath= "Name" /></TD> </TR> <TR> <TD><Form:labelPath= "Age">Age</Form:label></TD> <TD><Form:inputPath= "Age" /></TD> </TR> <TR> <TD><Form:labelPath= "id">Id</Form:label></TD> <TD><Form:inputPath= "id" /></TD> </TR> <TR> <TDcolspan= "2"> <inputtype= "Submit"value= "Submit"/> </TD> </TR></Table> </Form:form></Body></HTML>
In result.jsp
<%@taglib URI="Http://www.springframework.org/tags/form"prefix="form"%><HTML><Head> <title>Spring MVC Form Handling</title></Head><Body><H2>Submitted Student information</H2> <Table> <TR> <TD>Name</TD> <TD>${name}</TD> </TR> <TR> <TD>Age</TD> <TD>${age}</TD> </TR> <TR> <TD>Id</TD> <TD>${id}</TD> </TR></Table> </Body></HTML>
After you finish creating the source and configuration files, export the application. Right-click the application and use the export->war file option and save your springweb.war file in the Tomcat WebApps folder.
Now start your Tomcat server and make sure you can access other pages from the WebApps folder using a standard browser. Now try URL http://localhost:8080/SpringWeb/student, if everything in your spring Web application is fine, you should see the following results:
After submitting the required information, click the Submit button to submit the form. If everything in your spring Web application is fine, you should see the following results:
Maven Example:
Spring mvc-form (form) Processing example (reprint practice)