Nearby lessons
22 of 35Spring - Command Controllers and Forms
- Understand what a command class is and why Spring populates it for you
- Write a command class whose properties match the form field names
- Read the populated command object inside a controller method
Learn how a Spring MVC command class binds request parameters to a Java object automatically, and how AbstractCommandController hands that populated object to your controller — with complete command and controller classes for each step.
Command Controllers and Forms
<form:password path="--" value="--" showPassword="--"/>
Where "path" atribute will take reference name to the password field, it must be same as the property defined in the respective Command class having setter and getter methods.
Where "value" atrribute will take default password value to display while creating password field.
Where "showPassword" property will take either true/false value to show or not to show password data in password field while providing password data.
EX:
<form:form -----> User Password<form:password name="upwd" showPassword="false"/> </form:form>
In Command class
public class User{ private String upwd; ---- public void setUpwd(String upwd){
this.upwd = upwd; } public String getUpwd(){ return upwd; } --- }
Note: "showPassword" attribute is not working in Applications as part of my testing, but, Spring Framework has provided "showPassword" attribute in documentation.
- <form:checkbox>
It will provide checkbox in user form, it is same as Html <input type="checkbox"> tag and it will submit a boolean value to the Server side application on the basis of the selection. If we select checkbox then it will send "true" value , if we are not selecting checkbox then it will send false value to server side application.
Syntax:
<form:checkbox path="--" value="--"/>
Where "path" attribute will take reference name to the checkbox, it must be same as the boolean property existed in Command class having setXXX() and isXXX() method. Where "value" will take a value to send to the Server side application, where at Server side
Command class we need a property of String or xxx data type with setXXX() and getXXX() method.
Ex:
<form:form ----> R U Married?<form:checkbox path="Married"/>
</form:form>
In Command Class
public class User{
private boolean married; ---- public void setMarried(boolean married){
this.married = married; } public boolean isMarried(){
return married; } }
EX:
<form:form --- > R u Married? <form:checkbox path="maritalStatus" value="Married"/>
</form:form>
In Command Class
public class User{
private String maritalStatus; ---- public void setMaritalStatus(String maritalStatus) {
this.maritalStatus = maritalStatus; } public String getMaritalStatus() {
if(maritalStatus == null || maritalStatus.equals("")) { maritalStatus="Not Married";
} return maritalStatus; } ---- }
- <form:checkboxes>
It will provide collection of checkboxes to select items.
Syntax:
<form:checkboxes path="--" items="--"/>
Where "path" attribute will take reference value of all checkboxes , it must have a property of String[] in the respective command class having setXXX() and getXXX() methods.
Where "items" attribute will take an expression including a property from Command class providing checkbox labels and values or a Property data which is generated by overriding referenceData() method in controller class.
EX:
<form:form ---- > User Quaqlifications<form:checkboxes path="uqual" items="{qual_List}"/>
</form:form>
In Controller class
public class RegistrationController extends SimpleFormController{
@Override protected Map referenceData(HttpServletRequest request) throws Exception {
Map<String, Object> map = new HashMap<>(); List<String> qual_List = new ArrayList<>(); qual_List.add("BSC"); qual_List.add("BTech"); qual_List.add("MCA"); qual_List.add("MTech"); qual_List.add("PHD"); map.put("qual_List", qual_List); return map; }
@Override protected ModelAndView onSubmit(Object command) throws Exception {
User user = (User)command; return new ModelAndView("status", "user", user); } }
In Command Class
public class User{ private String[] uqual; ---- public void setUqual(String[] uqual){
this.uqual = uqual; } public String getUqual(){ return uqual; } }
- <form:radiobutton>
It will display a radio button to select an item, it is same as html <input type="radio"> tag.
Syntax:
<form:radiobutton path="--" value="--"/>
Where "path" attribute will take a reference name , it must be same as the property in Command class having setXXX() method and getXXX() method.
Where "value" attribute will take radio button value inorder to send to Server side application.
EX:
<form:radiobutton path="ugender" value="Male"/>Male <form:radiobutton path="ugender" value="Female"/>Female
In Command class
public class User{ private String ugender; ---- public void setUgender(String ugender){ this.ugender = ugender; } public String getUgender(){ return ugender; } --- }
- <formradiobuttons>
It will provide multiple radio buttons as a group to select one item.
Syntax:
<form:radiobuttons path="--" items="--"/>
Where "path" attribute will take reference name to the radio buttons which same as the property name in command class inorder to store the seleted radio button value.
Where "items" attribute will take the property name of the List or String[] which contains all the names of the Radio Buttons which is generated from referencedData() method from the respective controller class.
EX:
<form:radiobuttons path="uworkLocation" items="${uworkLocation}"/>
In Command class
public class User{ private String uworkLocation; ---- public void setUworkLocation(String uworkLocation){
this.uworkLocation = uworkLocation; } public String getUworkLocation(){
return uworkLocation; } }
In Controller class
public class UserController extends SimpleFormController{ protected Map referenceData(HttpServletRequest request) throws Exception { Map<String, Object> map = new HashMap<>();
List<String> uworkLocation = new ArrayList<>(); uworkLocation.add("Hyderabad"); uworkLocation.add("Chennai"); uworkLocation.add("Pune"); uworkLocation.add("Mumbai"); map.put("uworkLocation", uworkLocation);
return map; }
- <form:select>
<form:select> tag can be used to create select box, it is same as html <select> tag, it will display list of items to select either multiple or single depending on our option.
Syntax:
<form:select path="--" multiple="--" items="--"> ------
</form:select>
Where "path" attribute will take reference value to the Select box , it must be same as the respecitve property name in command class inorder to store the selected values.
Where "multiple" attribute will take either "true" or "false" value inorder make "multiple" selections or "single" selection in the select box.
Where "items" attribute will take a property name of Map type contains the Item values and itemLabels which is generated from referenceddata() method of the contoller class.
Note: If we want to provide items to the select box individually , not as a Map from controller class then we have to use <form:option> tag as chaild tag to <form:select> tag.
Syntax:
<form:option value="--"> Label </form:option> Where "value" attribute will take item Value which we want to send to Server side application. Where "Label" is item Label to display in select box.
Note: If we want to get all the Item Values and Item Labels from referencesData() method in Controller class then we have to use <form:options> tag.
Syntax:
<form:options items="---" itemValue="--" itemLabel="---"/>
Where "items" attribute will take a property representing Map object contains item values and item Labels from referencedData() method in Controiller class.
EX:
<form:select path=‖uskillSet‖ multiple=‖true‖> <form:option value=‖C‖>C</form:option> <form:option value=‖C++‖>C++</form:option> <form:option value=‖Java‖>Java</form:option> <form:option value=‖.Net‖>.Net</form:option> <form:option value=‖Oracle‖>Oracle</form:option> </form:select>
In Command Class
public class User{ private String[] uskillSet; ---- public void setUskillSet(String[] uskillSet){ this.uskillSet = uskillSet; } public String[] getUskillSet(){ return uskeillSet; } }
EX:
<form:select path="uhobbies" items="${uhobbies}" multiple="true"/>
In Command Class
public class User{ private String[] uhobbies; ---- public void setUhobbies(String[] uhobbies){ this.uhobbies = uhobbies; } public String[] getUhobbies(){ return uhobbies; } }
In Controller class
public class UserController extends SimpleFormController{ protected Map referenceData(HttpServletRequest request) throws Exception { Map<String, Object> map = new HashMap<>();
Map<String, String> uhobbies = new HashMap<>(); uhobbies.put(―Playing Cricket‖, ―Playing Cricket‖); uhobbies.put(―Listening Music‖, ―Listening Music‖); uhobbies.put(―Chatting with Friends‖, ―Chatting with Friends‖); map.put(―uhobbies‖, uhobbies);
map.put(―uhobbies‖, uhobbies);
return map; }
EX:
<form:select path="uprofession" multiple="false"> <form:options items="${uprofession}"/>
</form:select>
IN Command Class
public class User{ private String uprofession; ---- public void setUprofession(String uprofession){ this.uprofession = profession; } public String getUprofession(){ return uprofession; } }
- <form:textarea>
It will provide text area to take multiple lines of data from Users, it is same as <textarea> tag in Html.
Syntax:
<form:textarea path="--"/> Where "path" attribute will take reference name to the textarea which is same as a property in Command class.
EX:
<form:textarea path="uaddr"/>
In Command class
public class User{ private String uaddr; ---- public void setUaddr(String uaddr){ this.uaddr = uaddr;
} public String getUaddr(){ return uaddr; } }
- <form:hidden>
It will prepare Hidden field in user forms, it is same as <input type="hidden"/> in Html.
Syntax:
<form:hidden path="--" value="--"/> Where "path" attribute will take a reference name to the hidden field, it must be same as a property in Command class having setXXX() method and getXXX() method.
EX:
<form:hidden path="sid" value="S-111"/>
In Command Class
public class Student{ private String sid; public void setSid(String sid){ this.sid = sid; } public String getSid(){ return sid; } }
- A command class is a plain Java bean; Spring maps each request parameter onto the matching property
- The property names must match the form field names or the value silently stays null
- AbstractCommandController gives the controller the object, not the raw request