gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:37:00 26 4
gpt4 key购买 nike

我正在使用这样的表单支持 bean。

public class BeanData implements Serializable{

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

getters setters
}

然后在模型中发送 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);

}

我担心的是,我想使用 setter 方法设置一个 bean 属性,并希望将 bean 支持对象传递给 JSP。然后,所有属性值都应该绑定(bind)到支持对象(我使用表单字段路径属性显式绑定(bind)的内容以及我在创建 bean 对象时已经设置的内容),并且应该在 Controller 中接收它。请指导我哪里做错了。

最佳答案

如果您只想保留 JSP 中 param2 字段的值并在表单提交时取回它,您可以使用隐藏字段绑定(bind)它,如下所示:

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

它不会显示在您的 JSP 中,但它会按原样保留您的值。

另一种方法是将您的 BeanData 存储到 session 中。

关于java - 表单支持 bean 不保留已设置的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43178569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com