Spring MVC textarea tag example –
Spring MVC Textarea tag
Syntax
1 | <form:textarea path="fieldName" rows="ROW_NUM" cols="COL_NUM" /> |
Example
1 2 3 4 5 | <form:textarea path="additionalNotes" rows="10" cols="30" /> // Converts in HTML to <textarea id="additionalNotes" name="additionalNotes" rows="10" cols="30"></textarea> |
Lets see example on how Spring MVC Textarea tag is used in form
1.Model – Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Employee { private String firstName; private String lastName; private String department; private String additionalNotes; // Getters and Setters } |
2.Controller – EmployeeController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.kscodes.sampleproject.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.kscodes.sampleproject.model.Employee; @Controller public class EmployeeController { @RequestMapping(value = "/employee", method = RequestMethod.GET) public ModelAndView showEmployeeForm() { Employee employee = new Employee(); // Add the command object to the modelview ModelAndView mv = new ModelAndView("employee"); mv.addObject("employee", employee); return mv; } @RequestMapping(value = "/employee", method = RequestMethod.POST) public String submitForm(Model model, Employee employee) { model.addAttribute("employee", employee); return "employee"; } } |
3.View – employee.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC - Employee</title> </head> <body> <h2>Employee Details</h2> <form:form method="post" commandName="employee"> <table> <tr> <td><form:label path="firstName">First Name :</form:label></td> <td><form:input path="firstName" /></td> </tr> <tr> <td><form:label path="lastName">Last Name :</form:label></td> <td><form:input path="lastName" /></td> </tr> <tr> <td><form:label path="department">Department :</form:label></td> <td><form:input path="department" /></td> </tr> <tr> <td><form:label path="additionalNotes">Additional Notes :</form:label></td> <td><form:textarea path="additionalNotes" rows="10" cols="30" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit" /></td> </tr> </table> </form:form> </body> </html> |