Announcement Announcement Module
Collapse
No announcement yet.
new to spring doubt regarding formBackingObject Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • new to spring doubt regarding formBackingObject

    hi

    iam new to spring and iam just learning it.

    i have doubt regarding formBackingObject() method

    The default implementation is to create an empty instance of command bean .

    I know that to make use of formBackingObject() method we have to set the sessionForm to true.

    Say there is member registration form.

    In this case when i request for form viewing(GET request) we will be provided with a form in that process the controller implementation will call formBackingObject() method to create an empty instance of command bean.
    This is default implementation.

    My doubt is(or what i understood is this may be correct or not please correct me)
    Here can we overeride the formBackingObject() method to provide initial values to command bean that could be displayed in formview or obtain the data from database and set data to command bean.

  • #2
    You should not override the method. Your controller class would not know which method to call. Instead, within a single formBackingObject method, you can have separate return cases depending on what you need.

    Consider this example from the AccountFormController class from jpetstore (distributed with Spring):
    Code:
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
      UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
      if (userSession != null) {
        return new AccountForm(this.petStore.getAccount(userSession.getAccount().getUsername()));
      }
      else {
        return new AccountForm();
      }
    }
    Download the jpetstore sample and take a closer look at how it works. This AccountFormController is used both in registering new members, and in altering the properties of users who are already registered. It sounds pretty close to what you are trying to do.

    Comment


    • #3
      Thank u uaine;But a small clarrification regarding formBackingObject()

      Thank u for ur reply uaine;

      But a small clarrification.Actually Form submission is a 2 stage process(form viewing and form submitting) in spring MVC.

      Is it the same formBackingObject() method that is called both times that is during form viewing and form submission.So in the first stage(form viewing) spring creates command bean object and puts the command bean object in session and again during form submission spring retrieves the command object from session and bind the request parameters on command bean.

      But it is given that (in Experts Spring MVC and WebFlow) that,we can use the formBackingObject()method to configure if necessary the form object class possibly setting any dependencies or properties.This is the time to manipulate the form object before it ends the workflow.

      What actually the author is trying to convey.Does it mean that if we want we can provide initial values to the attributes of command bean so that those values could be displayed when the form is shown to the user or get the data from database and assign it to attributes of command bean and show them as default values to the user when the form is shown first time to the user.

      And also what does author mean by saying any dependencies.Does it mean that say for example if we have Member Command Bean which cantains Name as attribute which is again a separate java type.So we have to explicitly set the Name into Member Command bean like getMember.setName("xcv") in formBackingObject() method.

      Thanks in Advance

      Comment


      • #4
        Consider the following pieces of code. The AccountForm Class:
        Code:
        public class AccountForm implements Serializable {
        	private Account account;
        	private boolean newAccount;
        	private String repeatedPassword;
        
        	public AccountForm(Account account) {
        		this.account = account;
        		this.newAccount = false;
        	}
        
        	public AccountForm() {
        		this.account = new Account();
        		this.newAccount = true;
        	}
        
        	public Account getAccount() {
        		return account;
        	}
        
        	public boolean isNewAccount() {
        		return newAccount;
        	}
        
        	public void setRepeatedPassword(String repeatedPassword) {
        		this.repeatedPassword = repeatedPassword;
        	}
        
        	public String getRepeatedPassword() {
        		return repeatedPassword;
        	}
        }
        A snippet from the AccountFormController:
        Code:
        protected ModelAndView onSubmit(
        		HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
        		throws Exception {
        	AccountForm accountForm = (AccountForm) command;
        	try {
        		if (accountForm.isNewAccount()) {
        		this.petStore.insertAccount(accountForm.getAccount());
        		}
        		else {
        		this.petStore.updateAccount(accountForm.getAccount());
        		}
        	}
        	catch (DataIntegrityViolationException ex) {
        		errors.rejectValue("account.username", "USER_ID_ALREADY_EXISTS", "User ID already exists: choose a different ID.");
        		return showForm(request, response, errors);
        	}
        ...
        }
        My previous post showed the configuration of the form backing object. In the AccountForm class, you can see what the actual form object looks like, and the way in which it is configured.

        You can see how the data contained in the the AccountForm is processed in the onSubmit method of the controller. It is processed differently depending on the state of the form.

        Searching google or this forum would answer some of your questions a lot better than I can - I'm still pretty new to Spring myself.

        I'd recommend the step by step tutorial on the Spring website if you want to pick up most of the basics quickly. It's very well-written, and leaves almost nothing out. The samples included with Spring are great too, with examples of everything from spring:form to concurrency via Hibernate, Ibatis (my favorite), as well as many other things that I'm still teaching myself.

        Comment

        Working...
        X