Spring mvc-form (form) Label-text Box example (reprint practice)

Source: Internet
Author: User
Tags tomcat server tutorialspoint

The following content is translated from: https://www.tutorialspoint.com/springmvc/springmvc_textbox.htm

Description: The sample is based on spring MVC 4.1.6.

The following example shows how to use a text box in a form using the Spring WEB MVC framework. 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 < in a JSP Form: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 result view is returned from the service method, which results in rendering the 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>

Here we use the <form:input/> tag to render the HTML text box. For example

<path= "name"/>

It renders the following HTML content.

<id= " name" name= "name"  type= "text"  value= ""/>

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 helloweb.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/HelloWeb/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:

Https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test4

Spring mvc-form (form) Label-text Box example (reprint practice)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.