Announcement Announcement Module
Collapse
No announcement yet.
Binding Error with multipe fields - parent/child - FormBackingObject Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding Error with multipe fields - parent/child - FormBackingObject

    I am having issues with SimpleFormController. On my JSP page, I have to show to two select boxes. One shows the filingYear. Based on the value selected here, second select box is populated with forms. Then for selected form, I have to display it's fields in grid(table) format.

    Here is my command class
    Code:
    public class ReportFormFields {
    	private Long filingYear;//filingyear id value
    	private Long form; //form id value
    	
    	//filing years - loaded from looup table
    	private List filingYears=
    	    LazyList.decorate(
    		  	      new ArrayList(),
    		  	      FactoryUtils.instantiateFactory(ReportFilingYear.class));
    
    	//forms loaded from database per filing year
    	private List forms=
    	    LazyList.decorate(
    		  	      new ArrayList(),
    		  	      FactoryUtils.instantiateFactory(ReportForm.class));
    
    	//form fields loaded from db per form	
    	private List formFields =
    	    LazyList.decorate(
    	  	      new ArrayList(),
    	  	      FactoryUtils.instantiateFactory(ReportField.class));
    }
    So, we have a master child relation like filingYear--->Form--->Fields.

    Here is my Controller Class that extend SimpleFormController.
    Code:
    protected Object formBackingObject(HttpServletRequest request) throws ServletException{
    	logger.info("*****formBackingObject() called***********"+isFormSubmission(request));
    	ReportFormFields reportFormFields = new ReportFormFields();
    	reportFormFields.setFilingYears(reportAdminManager.getReportFilingYears());
    
    	Long filingYearId = null;
    	try{
    		filingYearId = ServletRequestUtils.getLongParameter(request, "filingYear");
    	}catch(ServletRequestBindingException ex){
    		logger.info("filingYear is empty");
    	}
    	logger.info("filingYearId-->"+filingYearId);
    
    	if(filingYearId != null){				
    		reportFormFields.setForms(reportAdminManager.getReportForms(filingYearId));
    		Long reportFormId = null;
    		try{
    			reportFormId = ServletRequestUtils.getLongParameter(request, "form");
    		}catch(ServletRequestBindingException ex){
    			logger.info("reportFormId is empty");
    		}			
    		logger.info("reportFormId-->"+reportFormId);
    		if(reportFormId != null){
    			reportFormFields.setFormFields(reportAdminManager.getReportFields(reportFormId));							
    		}
    	}
    	logger.info("ReportFormFields command object created: "+reportFormFields);
    	return reportFormFields;
    }
    Now when I execute the program first time it works fine. Here is the flow on JSP.
    1. Shows the form with only one select box -- list of filing years. I select one filing year here
    2. Shows the form with 2 select boxes - one with list of filing years and second with list of forms. I select one form here.
    3. Now it shows the fields for that form in grid(table). Now I edit few fields on the table and save the data. It saves in the db.

    4. Now when I select a different value from the form (or the default value like 'Select One'), it throws an exception from the
    formBackingObject as formFields object is empty inside the command class.

    how can I come out of this problem. Do I need to add any custom property binding class. Or do I need to create the empty objects for formFields and add them to the command class.

    Also, filingyear list is a reference data to me. Can I add it in the referenceData()? Is this data is available for each request whenever the form is displayed.

    I have configured this form as sessionForm.

    Here is the trace of the exception:

    Code:
    org.springframework.beans.NullValueInNestedPathException: Invalid property 'formFields[0]' of bean class [gov.nyc.doitt.coib.reports.admin.ReportFormFields]: Cannot access indexed value of property referenced in indexed property path 'formFields[0]': returned null
    	at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:527)
    	at org.springframework.beans.BeanWrapperImpl.getNestedBeanWrapper(BeanWrapperImpl.java:417)
    	at org.springframework.beans.BeanWrapperImpl.getBeanWrapperForPropertyPath(BeanWrapperImpl.java:394)
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:601)
    	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValue(AbstractPropertyAccessor.java:49)
    	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:74)
    	at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:515)
    	at org.springframework.validation.DataBinder.doBind(DataBinder.java:417)
    	at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:146)
    	at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:108)
    	at org.springframework.web.servlet.mvc.BaseCommandController.bindAndValidate(BaseCommandController.java:369)
    	at org.springframework.web.servlet.mvc.AbstractFormController.handleInvalidSubmit(AbstractFormController.java:650)
    	at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:257)
    	at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
    	at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:806)
    Thanks
Working...
X