填充列表< String>在struts2中从表单数据 [英] Populate List<String> in struts2 from form data

查看:73
本文介绍了填充列表< String>在struts2中从表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这应该非常明显,但是到目前为止,我还没有找到答案.

I feel this should be exceedingly obvious, but so far I've failed to find an answer.

我想让一个字符串列表(或一个字符串数组,我真的不在乎)被Struts2中的表单数据填充.

I want to have a list of strings (or an array of strings, I really don't care) get populated by form data in Struts2.

我已经看到了几个如何使用bean进行索引属性的示例, >,但是将单个字符串包装在对象中似乎很愚蠢.

I've seen several examples of how to do indexed properties with beans, but wrapping a single string inside an object seems fairly silly.

所以我有类似的东西

    public class Controller extends ActionSupport {

        private List<String> strings = new ArrayList<String>();
        public Controller() {
            strings.add("1");
            strings.add("2");
            strings.add("3");
            strings.add("4");
            strings.add("5");
        }
        public String execute() throws Exception {
            return ActionSupport.SUCCESS;
        }
        public List<String> getStrings() {
            return strings;
        }

        public void setStrings(List<String> s) {
            strings = s;
        }
    }    

...

<s:iterator value="strings" status="stringStatus">
   <s:textfield name="strings[%{#stringStatus.index}]" style="width: 5em" />
</s:iterator>

表单字段填充了其初始值(例如1、2等),但结果未正确回发.永远不会调用setStrings,但会将值设置为空字符串.

The form fields get populated with their initial values (e.g. 1, 2, etc), but the results are not properly posted back. setStrings is never called, but the values get set to empty strings.

任何人都知道发生了什么事吗?预先感谢!

Anybody have any idea what's going on? Thanks in advance!

推荐答案

我相信,有了它,您的jsp代码将呈现出类似以下内容:

I believe as you have it, your jsp code would render something like:

<input type="text" name="strings[0]" style="width: 5em" value="1"/>
<input type="text" name="strings[1]" style="width: 5em" value="2"/>
<input type="text" name="strings[2]" style="width: 5em" value="3"/>
...

请注意,字段引用的名称为"strings [x]",因此您需要将其名称仅作为"strings".我会建议类似的东西:

Notice that the name of the field references are "strings[x]" where as you need the name to be just "strings". I would suggest something like:

<s:iterator value="strings" status="stringStatus">
   <s:textfield name="strings" value="%{[0].toString()}" style="width: 5em" />
</s:iterator>

不确定上面的value属性是否正确,但是我认为类似的方法可以为您带来理想的结果.

Not sure if the value attribute above may is correct, but I think something like this will get you the desired result.

这篇关于填充列表&lt; String&gt;在struts2中从表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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