【问题标题】:How to do mapping for LIST of object in spring mvc framework如何在spring mvc框架中为对象列表做映射
【发布时间】:2014-03-21 15:14:14
【问题描述】:

我有以下 POJO 类 作为输入 -

public class Input implements java.io.Serializable {

    private String id;
    private List<Inputbenefit> Inputbenefits;
    //and getter and setter method 
}

现在在 controller 我有 initbinder -

@InitBinder
public void initBinder(Object target,WebDataBinder binder) {  
    binder.registerCustomEditor(ArrayList.class, new CustomCollectionEditor(ArrayList.class) {   
        @Override  
        protected Object convertElement(Object element) {
            Input  input= new Input ();     
            if (element != null) {
                ArrayList<Inputbenefit> id = (ArrayList<Inputbenefit>) element;
                input.setInputbenefits(id);
            }
        return input;
    } 
});   

控制器中的 Post 方法签名是 -

@RequestMapping(value = "/addDependentOutput.html", method = RequestMethod.POST)
public String OutputForm(@ModelAttribute("Input") Input input, BindingResult result, Model model) 

在 JSP 中 - 我有 5 个固定文本框来为 Input 类取值 -

<spring:bind path="inputbenefits.benefitId">                
    <form:input path="${status.expression}" size="10" value="Manisha"/>
</spring:bind>

我没有将值从 html 表单获取到 OutputForm,即无法读取控制器 post 方法 OutputForm 中提交的 inputbenefits.benefitId 的值。

简而言之 - 我的 List 对象值没有传递给控制器​​方法。

请帮忙。谢谢。

【问题讨论】:

  • 请以正确的格式发布您的问题,尤其是代码。此外,您还没有提到您遇到了什么错误。 :(
  • 嗨,Subhkriti,我编辑了我的帖子。谢谢。

标签: spring-mvc


【解决方案1】:

你不应该从 converElement 方法返回宿主对象,你应该返回你的集合对象

        @Override  
        protected Object convertElement(Object element) {
            int benefitId = Integer.parseInt(element.toString());
            return benefitService.getById(benefitId);
        } 

更新

我假设您尝试将福利集合绑定到您的输入对象,并且您有一个表单来创建新输入并选择控件来选择所需的好处。正确的?如果是真的,你需要这样的东西

使用表单弹簧标签

        <form:select path="inputBenefits" items="${benefits}" 
         multiple="multiple" size="5" itemLabel="additionalAmt" itemValue="benefitId"/>
        <form:errors path="inputBenefits"/>

其中 ${benefits} 集合的可用福利,您可以访问该页面

在你需要的控制器中

@InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
     binder.registerCustomEditor(List.class, "inputBenefits", new CustomCollectionEditor(List.class) {

            protected Object convertElement(Object element) {
                if (element != null) {
                    Integer benefitId = Integer.parseInt(element.toString());
                    Benefit benefit = benefitService.getById(benefitId); // something that able to get benefit object
                    return benefit;

                }
                return null;
            }

        });

    }

这样,将为页面选择控件上选择的每个值调用方法 convertElement。之后,福利集合将被推送到命令表单对象(我想是输入)

这里是简单的例子http://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm

【讨论】:

  • 嗨,Georgy,我想我们已经足够接近了。我有以下字段作为 Inputbenefit.class { private java.lang.String wqBtId; 的一部分。私有 java.lang.String benefitId;私人双附加量;私人布尔选举;我怎样才能为此编写 convertElement(Object element) 方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 1970-01-01
  • 2020-04-21
  • 1970-01-01
  • 2014-05-04
  • 1970-01-01
  • 2016-05-19
相关资源
最近更新 更多