views:

55

answers:

2

Hi,

I am new to Spring Framework and trying to understand the functionality of FormBackingObject and comparing it with referencedata object functions, both this objects confuse me when am trying to relate them to HTTP Request Cycle from Spring MVC point of view.

I would really appreciate if someone can explain this two objects with reference to an example.

Q: What is the difference between formbacking object and reference Data Object ?

A: 

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>
Arthur Ronald F D Garcia
Can you explain form backing object and referenceData in more simple language...let's say you have to explain it to someone who has just entered into Spring World or say someone who does not anything about Spring ?
Rachel
@Rachel **I really advice you** to follow Either Spring MVC Tutorial http://static.springsource.org/docs/Spring-MVC-step-by-step/index.html before. read carefully. One day for each chapter = six business day Or http://maestric.com/doc/java/spring Feel free. Good lucky!
Arthur Ronald F D Garcia
+1  A: 

In simple terms:

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.

JacobM
This is something that I was looking for...thank you Jacob for simple explanation.
Rachel