My question is, Is it necessary to have a 'commandName' binding for forms and 'path' bindings for inputs?
I have a work flow which needs users to select data from a series of ajaxy select boxes. So the form doesn't exactly map to any model directly. It seems rather pointless to add another model just to accommodate this form, while everything on this form will be driven by ajax. Now I know I can always write a classic form and select tags, but I would like to take advantage of spring tags here for example:
<form:options items="${list}" itemLabel="name" itemValue="id"/>
which allows me to easily populate by form items. And plus I would like to maintain uniformity in the project and use spring tags through.
I would like to know thoughts and opinions on this topic.
Thanks!
PS: I am coming from ruby on rails background and am used to lot of syntactic sugar :P forgive me if the question sounds silly or answer obvious.
The commandName attribute is the most important attribute in the form tag, which specifies the model attribute name that contains a backing object and the properties of this object will be used to populate the generated form.
Data binding is useful for allowing user input to be dynamically bound to the domain model of an application (or whatever objects you use to process user input). Spring provides the so-called DataBinder to do exactly that.
Spring MVC is a Model-View-Controller framework so it handles form submission by the three key components: model, view and controller. Model: basically a POJO (Plain Old Java Object) class is created to bind form fields with properties of the object. This object will be put into the model (model object).
Form Backing Object/Command Object This is a POJO that is used to collect all information on a form. It contains data only. It is also called a Command Object in some Spring tutorials.
I don't know if it's useful, but here it goes:
If you don't set a 'commandName' in the form, the default value for this property will be set as 'command'. So, if you don't set it, the bound data will have the name 'command'.
If you want, you can set it with the name of your bound data.
==========================================================================
A solution without using data binding would be:
I would add a HttpServletRequest request parameter to the controller method and get the parameter like servlets do.
@Controller
public class SomeController {
@RequestMapping(value = "/formAction", method = RequestMethod.POST)
public String controllerMethod(HttpServletRequest request){
// this way you get value of the input you want
String pathValue1 = request.getParameter("path1");
String pathValue2 = request.getParameter("path2");
return "successfulView";
}
}
PS.: the 'path1' and 'path2' are the path names you set in the inputs. I know it seems do not use the Spring properly, but its a hack that Spring let us to use.
The form would be this way:
<form:form method="post" action="/formAction">
<form:input path="path1" />
<form:input path="path2" />
<input type="submit" value="Submit"/>
</form:form>
Hope its useful.
If we are not adding the commandName to spring form tags an exception willl be raised as
IllegalStateException: Neither BindingResult nor plain target object "command" available as request parameter...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With