简体   繁体   中英

Form backing bean is not retaining already set property values

i am using a form backing bean like this.

public class BeanData implements Serializable{

    private String param1;
    private String param2;
    private String param3;
    private String param4="india";

    getters setters
}

then sending bean object in model as below-

@RequestMapping(value=/formPage, method={RequestMethod.GET,RequestMethod.POST})
    public ModelAndView getPage(HttpSession session, ModelAndView modelAndView) {   

        BeanData formBean = new BeanData();
        formBean.setParam2("123456"); // this param2 doens't have any field in JSP
        modelAndView.addObject("formBean", formBean);
        modelAndView.setViewName(PAGE);

        return modelAndView;
    }
@RequestMapping(value=submitData, headers="Accept=*/*", method={RequestMethod.POST})
    public void submitData(@Valid @ModelAttribute("formBean") BeanData formBean, BindingResult result, HttpServletRequest request,HttpServletResponse response,ModelAndView modelAndView, HttpSession session) {


        LOGGER.info("param1:"+formBean.getParam1()); // Param1 has a path map in jsp field. So whatever user is puuting into form field, that is getting populated here
        LOGGER.info(" param2:"+formBean.getParam2()); // It has not been used in JSP. Though from controller it was populated before sending the bean to the jsp. but here the value is null . This is the concern
        LOGGER.info("param3:"+formBean.getParam3());// Param1 has a path map in jsp field. So whatever user is puuting into form field, that is getting populated here
        LOGGER.info("param4:"+formBean.getParam4());//thsi field also has not been used in JSP. But this property was set in bean instantiation. It is also getting retrieved successfully.


        modelAndView.setViewName(SUCCESS PAGE);

    }

My concern is, i want to set one bean property using setter method and want to pass the bean backing object to JSP. Then all the property values should be bind(what i explicitly bind using form filed path attribute and what i have already set while created the bean object) to the backing object and it should get received in controller. Please guide me where i am doing wrong.

If you want to just hold the value of param2 field in JSP and get it back on form submit, you can bind it using the hidden field just like below:

<form:hidden path="formBean.param2"/>

It will not be displayed in your JSP yet it will hold your value as is.

Another way would be to store your BeanData into session.

The model attributes are request scoped beans. Solutions you could use:

  • Map your properties to hidden fields at your html form. I do not recommend that as they can be easily update at the client side.
  • Use session attributes instead of model attributes, and I think it is the last option.
  • What you really should do is delegate the creation of your form bean to a method annotated with @ModelAttribute instead of doing that in your getPage method.

Spring executes method annotated with @ModelAttribute before handling the requests and then will update the object with the properties coming from your form.

@ModelAttribute("formBean")
public BeanData setFormBeanModel()
{
  BeanData formBean = new BeanData();
  formBean.setParam2("123456");
  return formBean;
}

@RequestMapping(value=/formPage, method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView getPage(HttpSession session, ModelAndView modelAndView) 
{ 
  modelAndView.setViewName(PAGE);

  return modelAndView;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM