开发者_如何学PythonI am new to Spring Framework and trying to understand the functionality of formBackingObject and comparing it with referenceData methods, both these objects confuse me when am trying to relate them to HTTP Request Cycle from a Spring MVC point of view.
I would really appreciate if someone can explain these two objects with reference to an example.
Q: What is the difference between formbacking object
and reference Data
Object ?
When you load a web page, you will want to pass data to it so that it can render.
Some of this data will be purely informational, read-only; data that the page needs in order to render, but that isn't part of the current transaction. Examples: a list of countries to populate a drop-down, a list of possible products the user can buy.
Other data will be used for reading and writing: the contents of a form, say, must be populated with the current data, but can also be updated by the user. This set of data will be bound to the form; data sent to the page will render, data sent from the page (by the user) will cause an update. Examples: the user's name and address; the current order.
All of this data will typically be stored in one or more objects that the page needs access to.
Objects containing informational data should be placed in the map provided by the referenceData()
method. There can be as many such objects as you like.
The data to be bound to the form, the read/write data, must be contained in a single object. This object should be returned by the formBackingObject()
method.
I'll add that in more recent versions of Spring, annotations are used instead of these "built-in" methods.
Here goes formBackingObject API
retrieve a backing object for the current form from the given request
Some scenarios
- Avoids NullPointerException when traversing nested path
...
public class Command {
private NestedClass nestedPath;
// getter's and setter's
}
Notice above nestedPath field has not been initialized. So if you try to retrieve its value on the form such as
<spring:form path="nestedPath.someProperty"/>
Because nestedPath has not been initialized, You will get NullPointerException when traversing some nestedPath property. To avoid NullPointException, overrides formBackingObject
public Object formBackingObject(HttpServletRequest request) throws Exception {
Command command = new Command();
command.setNestedPath(new NestedClass());
return command;
}
- Find-To-Update scenario
You submit the identifier of some command (usually by using GET method) to allow users to update it later
public Object formBackingObject(HttpServletRequest request) throws Exception {
if(request.getMethod().equalsIgnoreCase("GET")) {
return commandRepository.findById(Integer.valueOf(request.getParameter("id")));
}
}
And referenceData API
create a reference data map for the given request
You use referenceData to create data used by your form, for instance, a list of categories
protected Map referenceData(HttpServletRequest request) throws Exception {
return new ModelMap().addAttribute(categoryRepository.findAll());
}
On the form
<label>Select category</label>
<form:select path="category">
<form:option label="Select category" value=""/>
<form:options items="${categoryList}"
itemLabel="WHICH_PROPERTY_OF_CATEGORY_SHOULD_BE_USED_AS_LABEL"
itemValue="WHICH_PROPERTY_OF_CATEGORY_SHOULD_BE_USED_AS_VALUE"/>
</form:select>
When you load a web page, you will want to pass data to it so that it can render.
Some of this data will be purely informational, read-only; data that the page needs in order to render, but that isn't part of the current transaction. Examples: a list of countries to populate a drop-down, a list of possible products the user can buy.
Other data will be used for reading and writing: the contents of a form, say, must be populated with the current data, but can also be updated by the user. This set of data will be bound to the form; data sent to the page will render, data sent from the page (by the user) will cause an update. Examples: the user's name and address; the current order.
All of this data will typically be stored in one or more objects that the page needs access to.
Objects containing informational data should be placed in the map provided by the referenceData() method. There can be as many such objects as you like.
The data to be bound to the form, the read/write data, must be contained in a single object. This object should be returned by the formBackingObject() method.
精彩评论