从JSP检索嵌套列表的值,并将其发送回表单 [英] Retrieve the values of a nested list from JSP and send it back to form

查看:68
本文介绍了从JSP检索嵌套列表的值,并将其发送回表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑现在,我知道我的问题是由于 .该链接还提供了解决方案,但我似乎无法在第二个列表中弄清楚该怎么做.

EDIT Now I know my problem is due to this. The link also provided solutions but I can't seem to figure out how to do it in the 2nd list.

我将首先向您展示我正在研究的代码结构.

I will first show you the code structure I am working on.

这是MyForm类:

public class MyForm extends ValidatorForm {
    private List<ADTO> aDTOList;

    // getters and setters for aDTOList below

    public ADTO getADTO(int index) {
        if (aDTOList == null) {
            aDTOList = new ArrayList<ADTO>();
        }
        if (aDTOList.size() - 1 < index) {
            while (aDTOList.size() - 1 < index) {
                aDTOList.add(new ADTO());
            }
        }
        return aDTOList.get(index);
    }

    @Override
    protected ActionErrors execValidate(ActionMapping mapping, HttpServletRequest request) {
          // BODY NOT SHOWN FOR PRIVACY
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) { 
        super.reset(mapping, request);
        this.aDTOList = new ArrayList<ADTO>();
    }


}

这是ADTO类:

public class ADTO {
    private List<BDTO> bDTOList;
    // getters and setters for bDTOList below

}

这是BDTO类:

public class BDTO {
    private String sample1;
    private String sample2;
    // getters and setters for sample1 and sample2 below

}

我已经通过执行以下操作在JSP中成功显示了aDTOList的内容:

I have successfully displayed the contents of aDTOList in the JSP by doing this:

<logic:iterate id="ADTO" name="MyForm" property="aDTOList" indexId="idxRes">
    <logic:iterate id="BDTO" name="ADTO" property="bDTOList" indexId="idxLine">
        <html:hidden name="BDTO" property="sample1" indexed="true"/>
        <html:hidden name="BDTO" property="sample2" indexed="true"/>
    </logic:iterate>
</logic:iterate>

现在,我的问题是,每当我提交aDTOList内的表单bDTOList都将为空时.aDTOList具有与我显示的原始列表相同的大小,但唯一的区别是<aDTO中的c1>为空.如果aDTOList的大小为2,并且每个ADTO包含bDTOList,其大小也为2,则aDTOList的结构是这样的.

Now my problem is whenever I submit the form bDTOList that is inside aDTOList will become all null.aDTOList has the same size as the original list I have displayed but the only difference is that all elements of bDTOList in a aDTO is null. The structure of the aDTOList is like this if the size of aDTOList is 2 and each ADTO contains bDTOList which also has the size of 2.

[[null, null],[null, null]]

因此,我认为我的问题是我的表单中没有getBDTO,但是我不知道如何实现它.谁能帮我实现它?还是有其他方法用原始数据填充bDTOList?

Thus I think my problem is I do not have getBDTO in my form, but I don't know how to implement it. Can anyone help me on how to implement it? Or is there any other means of populating bDTOList with the original data?

注意:我无法更改代码的结构,并且代码只是示例代码

推荐答案

经过几天的研究和修改代码,我终于能够从JSP中检索值并将其发送回表单.我将发布答案以供将来参考.感谢此网站,我能够知道问题的原因并最终提出了解决方案解决这个问题.请参阅下面有关我如何解决问题的详细信息.

After days of research and tinkering my code I finally was able to retrieve back the values from the JSP and send it back to form. I will just post an answer for future reference. Thanks to this website I was able to know the cause of my problem and eventually made a solution to solve it. Refer below on the details on how I solved my problem.

我发现问题是由于如果您使用java.util.List而不是Arrays,则Commons BeanUtils中存在带有索引属性的问题,这是因为人们随后在请求范围内的ActionForms遇到了索引超出范围"错误.这就是为什么需要在调用get(int)方法时增加列表的原因.此外,每当调用reset方法时,您都需要重新初始化列表.为此,您需要将此代码粘贴到以下形式的reset方法中:

I found out that the problem is due to an issue in Commons BeanUtils with indexed properties if you use java.util.List rather than Arrays is that people then get "index out of range" errors with ActionForms that are in Request scope. That is why growing the list when the get(int) method is called is needed to be done. Also you need to reinitialize the list whenever the reset method is called. To do this you need to paste this code in the reset method of the form:

public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

    aDTOList = ListUtils.lazyList(new java.util.ArrayList(), new Factory() {
        public Object create() {
            return buildADTOList();
        }
    });
 }

private ADTO buildADTOList() {
    ADTO aDTO = new ADTO();
    List bDTOList = ListUtils.lazyList(new java.util.ArrayList(), new Factory() {
        public Object create() {
            return new BDTO();
        }
    });
    aDTO.setBDTOList(bDTOList);
    return aDTO;
}

现在,只要调用reset方法,您的列表就会恢复到其原始大小.现在的下一个问题是如何从JSP取回值并将它们放回到列表中.为此,您必须注意,JSP标记的结果html name属性的值必须采用aDTOList[0].bDTOList[0].sample1格式.但是,如果您使用标记(就像问题在使用一样),则生成的html的值将如下所示: 示例:

Now your whenever the reset method is called your list will regrow to its original size. The next problem is now how to retrieve back the values from the JSP and place them back in the list. To do this you have to take note of that the value of the resulting html name attribute of your JSP tag must be in this format aDTOList[0].bDTOList[0].sample1. But if you use tag (just as the question was using), the value of the resulting html will look like this: Example:

<logic:iterate id="ADTO" name="MyForm" property="aDTOList" indexId="idxRes">
    <logic:iterate id="BDTO" name="ADTO" property="bDTOList" indexId="idxLine">
        <html:hidden name="BDTO" property="sample1" indexed="true"/>
        <html:hidden name="BDTO" property="sample2" indexed="true"/>
    </logic:iterate>
</logic:iterate>

这将导致:

<input type="hidden" name="BDTO[0].sample1" value="..."/>
<input type="hidden" name="BDTO[0].sample2" value="..."/>
<input type="hidden" name="BDTO[1].sample1" value="..."/>
<input type="hidden" name="BDTO[1].sample2" value="..."/>
<input type="hidden" name="BDTO[0].sample1" value="..."/>
<input type="hidden" name="BDTO[0].sample2" value="..."/>
<input type="hidden" name="BDTO[1].sample1" value="..."/>
<input type="hidden" name="BDTO[1].sample2" value="..."/>

结果不是aDTOList[0].bDTOList[0].sample1格式,因此您需要使用<nested:iterate>.
转换后的代码将是:

The result is not in aDTOList[0].bDTOList[0].sample1 format thus you need to use <nested:iterate>.
The converted code will be:

<nested:iterate property="aDTOList" indexId="idxRes">
    <nested:iterate property="bDTOList" indexId="idxLine">
        <nested:hidden property="sample1"/>
        <nested:hidden property="sample2"/>
    </nested:iterate>
</nested:iterate>

这将导致:

<input type="hidden" name="aDTOList[0].bDTOList[0].sample1" value="..."/>
<input type="hidden" name="aDTOList[0].bDTOList[0].sample2" value="..."/>
<input type="hidden" name="aDTOList[0].bDTOList[1].sample1" value="..."/>
<input type="hidden" name="aDTOList[0].bDTOList[1].sample2" value="..."/>
<input type="hidden" name="aDTOList[1].bDTOList[0].sample1" value="..."/>
<input type="hidden" name="aDTOList[1].bDTOList[0].sample2" value="..."/>
<input type="hidden" name="aDTOList[1].bDTOList[1].sample1" value="..."/>
<input type="hidden" name="aDTOList[1].bDTOList[1].sample2" value="..."/>

如您所见,它是aDTOList[0].bDTOList[0].sample1格式.

然后,您可以从JSP中检索嵌套列表的值,并将其发送回表单.我希望这将为那些被困于解决此类问题几天的人们提供指导.

And from that you can retrieve the values of a nested list from JSP and send it back to form. I hope this will serve as a guide for those people that have been stuck for days solving this kind of problem.

这篇关于从JSP检索嵌套列表的值,并将其发送回表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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