简体   繁体   English

表单支持bean不保留已设置的属性值

[英]Form backing bean is not retaining already set property values

i am using a form backing bean like this. 我正在使用像这样的表单支持bean。

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- 然后在模型中发送bean对象,如下所示 -

@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. 我关心的是,我想使用setter方法设置一个bean属性,并希望将bean支持对象传递给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. 然后所有属性值应该绑定(我使用表单字段路径属性显式绑定的内容以及我在创建bean对象时已经设置的内容)到后备对象,它应该在控制器中接收。 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: 如果你想在JSP中保存param2字段的值并将其恢复到表单提交,你可以使用隐藏字段绑定它,如下所示:

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

It will not be displayed in your JSP yet it will hold your value as is. 它不会显示在您的JSP中,但它将按原样保存您的值。

Another way would be to store your BeanData into session. 另一种方法是将BeanData存储到会话中。

The model attributes are request scoped beans. 模型属性是请求范围的bean。 Solutions you could use: 你可以用的解决方案:

  • Map your properties to hidden fields at your html form. 将您的属性映射到html表单中的隐藏字段。 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. 您真正应该做的是将表单bean的创建委托给使用@ModelAttribute注释的方法,而不是在getPage方法中执行。

Spring executes method annotated with @ModelAttribute before handling the requests and then will update the object with the properties coming from your form. Spring在处理请求之前执行使用@ModelAttribute注释的方法,然后使用来自表单的属性更新对象。

@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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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