如何在Spring MVC中绑定命令对象的列表集合中的对象 [英] How to bind an object inside the list collection of the command object in Spring MVC

查看:110
本文介绍了如何在Spring MVC中绑定命令对象的列表集合中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的命令对象有一个对象列表。我想将文本字段绑定到该列表中对象的属性。可以在Spring MVC中做吗?

My command object have a list of objects. I want to bind a text field to the attribute of the object inside that list. Is it possible to do in Spring MVC?

命令对象类

public class SubDevisonDto {

private String devId;
private List subDevisions;

列表中提到的Subdevision对象类

Subdevision object class mentioned in the list

public class SubDivison implements Serializable{

private String subDivisonName;
private String createdBy;
private String createdDate;
private String developerID;
private List users;

我想要文本框设置subDivisonName字段的值。

I want text box to set the value for subDivisonName field.

我已经编写了这样的Spring MVC标签。

I have written the Spring MVC tags like this.

<spring:bind path="subdivisondto.subDevisions[0].subDivisonName">
    <span class="formw">
        <input name="subDivisonName" type="text" style="width:350px;" />
    </span>
</spring:bind>

仅出于测试目的,我将其作为0.如果它正常工作我可以将其变为变量。我的要求是,我应该让用户动态添加subdevision对象。因此,最初在加载页面时,我只会显示一个文本框。如果他想添加更多,我会给他一个按钮添加。单击添加按钮时,我将动态生成文本框。之后我必须提交带有列表的表单。

Just for test purpose I have given it as 0. If it's working I can make it to a variable. my requirement is, I should let the user to dynamically add subdevision objects. So, initially when page is loading I will just show one text box. I will give a button for him to add if he want to add more. I will dynamically generate text boxes when he clicks the add button. After that I have to submit the form with the list.

这个jsp代码给了我一个错误。它说 org.springframework.beans.NullValueInNestedPathException:

This jsp code gives me an error. It says org.springframework.beans.NullValueInNestedPathException:

无论如何我在jsp代码中执行此操作?如果您对此有任何疑问,请帮助我。在此先感谢。

Is there anyway for me to do this in jsp code ? Please help me if you have any idea regarding this. Thanks in advance.

推荐答案

我找到了问题的答案。但是,这不是我的要求的解决方案,因为我需要实现动态列表。但我找到了这个问题的解决方案。

I found the answer for my question. But, it's not the solution for my requirement as I need to implement a dynamic list. but I found a solution for this question.

据我所知,我们第一次必须从后端发送数据到绑定输入元素。我没有找到一种方法来绑定表单元素,这些元素接收输入而不从beck端发送列表数据。但是当我们发送数据并绑定元素时,我们可以从这些元素中获取输入。所以,我认为在这种情况下绑定元素我们需要第一次发送数据。如果这个陈述是错误的,请纠正我。因为,这对我来说是一个更好的解决方案。

As I understood, first time we have to send data from back end to bind input elements. I didn't find a way to bind form elements which takes input without sending a list data from beck end. But when we send data and bind the elements, we can take input from those elements. So, I think to bind the element in a situation like this we need to send data first time. Correct me if this statement is wrong. Because, that would be a more good solution for me.

我们需要使用惰性列表并对jsp代码进行位修改。

We need to use the lazy list and jsp code is bit modified.

您的命令类对象应该是如下所述创建。

Your command class object should be created as below mentioned.

import org.apache.commons.collections.list.LazyList;
import org.apache.commons.collections.FactoryUtils;



public class SubDevisonDto {

    private String devId;

    private List subDevisions = 
        LazyList.decorate(
          new ArrayList(),
          FactoryUtils.instantiateFactory(SubDivison.class));

JSP代码应如下所示。

JSP code should look like below.

<c:forEach items="${subs.subDevisions}" var="obj" varStatus="gridRow"> 

绑定输入元素文本框

<spring:bind path="subdivisondto.subDevisions[${gridRow.index}].subDivisonName">
      <span class="formw"><input name="<c:out value="${status.expression}"/>" type="text"  style="width:350px;" />

绑定输入元素复选框。此输入元素生成一个列表。

binding an input element check box. This input element makes a list.

<spring:bind path="subs.subDevisions[${gridRow.index}].users">
              <c:forEach items="${obj.users}" var="dependenttwo" varStatus="dependentRowtwo">
                <li>
                  <input name="<c:out value="${status.expression}"/>" type="checkbox" class="users" value="<c:out value="${dependenttwo}"/>"/>
                  <c:out value="${dependenttwo}"/>
                </li>
                </c:forEach>
                </spring:bind>

`subs` is a map key name. the value for this key `subs` is a list of my DTO objects which named as `SubDevisonDto `

这段代码对我来说很好。

This code works fine for me.

感谢给予的支持。

这篇关于如何在Spring MVC中绑定命令对象的列表集合中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆