The following content is translated from: https://www.tutorialspoint.com/springmvc/springmvc_textarea.htm
Description: The sample is based on spring MVC 4.1.6.
The following example shows how to use textarea in a form that uses 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 User,usercontroller under the Com.tutorialspoint package. |
3 |
Create a view file under the JSP subfolder user.jsp,users.jsp. |
4 |
The final step is to create the contents of all the source and configuration files and export the application as described below. |
User.java
PackageCom.tutorialspoint; Public classUser {PrivateString username; PrivateString password; PrivateString address; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString getaddress () {returnaddress; } Public voidsetaddress (String address) { This. Address =address; }}
Usercontroller.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 classUsercontroller {@RequestMapping (value= "/user", method =requestmethod.get) PublicModelandview User () {return NewModelandview ("User", "command",NewUser ()); } @RequestMapping (Value= "/adduser", method =requestmethod.post) PublicString AddUser (@ModelAttribute ("Springweb"user User, Modelmap model) {Model.addattribute ("Username", User.getusername ()); Model.addattribute ("Password", User.getpassword ()); Model.addattribute ("Address", User.getaddress ()); return"Users"; }}
Here the first service method user (), we have passed an empty user object in the Modelandview object named "command", because if you use <form:form> in JSP Tag, the spring framework expects an object file named "command". So when the user () method is called, it returns the user.jsp view.
The second service method AddUser ()is called for the Post method of the helloweb/adduser URL. You will prepare your model objects based on the information submitted. Finally, a "user" view is returned from the service method, which results in rendering the users.jsp
user.jsp
<%@taglib URI="Http://www.springframework.org/tags/form"prefix="form"%><HTML><Head> <title>Spring MVC Form Handling</title></Head><Body><H2>User Information</H2><Form:formMethod= "POST"Action= "/helloweb/adduser"> <Table> <TR> <TD><Form:labelPath= "username">User Name</Form:label></TD> <TD><Form:inputPath= "username" /></TD> </TR> <TR> <TD><Form:labelPath= "Password">Age</Form:label></TD> <TD><Form:passwordPath= "Password" /></TD> </TR> <TR> <TD><Form:labelPath= "Address">Address</Form:label></TD> <TD><Form:textareaPath= "Address"rows= "5"cols= "+" /></TD> </TR> <TR> <TDcolspan= "2"> <inputtype= "Submit"value= "Submit"/> </TD> </TR> </Table> </Form:form></Body></HTML>
Here we use the <form:textarea/> tag to render the HTML text area box. For example
<path= "Address" rows= "5" cols= "30" />
It renders the following HTML content.
<id= "Address " name= "Address" rows= "5" cols= "></"textarea>
users.jsp
<%@taglib URI="Http://www.springframework.org/tags/form"prefix="form"%><HTML><Head> <title>Spring MVC Form Handling</title></Head><Body><H2>Submitted User information</H2> <Table> <TR> <TD>Username</TD> <TD>${username}</TD> </TR> <TR> <TD>Password</TD> <TD>${password}</TD> </TR> <TR> <TD>Address</TD> <TD>${address}</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 the URL http://localhost:8080/HelloWeb/user, if everything in the 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/test6
Spring mvc-Form Label-Multiline text box (TextArea) example (reprint practice)