更新struts2中textfield中的值列表 [英] update a list of value in textfield in struts2

查看:105
本文介绍了更新struts2中textfield中的值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Struts2中遇到问题,在我的示例应用程序中我有对象数组(说出人名),我需要将这些名称显示为可编辑的文本字段,我正在使用迭代器,我成功地进行了调整,但是当我能够提供相同的价值并提交表格时。我得到了整个数组,它保存了空值。

I am facing a problem in Struts2, In my sample application I have Array of objects (Say person name) , I need to display these names as a editable text fields , I am using Iterators for this , I am successful in diaplying, But When I cange the same value and submit the form. I am getting the entire array which holds null value.

说我的表格bean中的Exaple我有一个属性
名称[]名称;

Say for Exaple in my form bean I have a property Name [] names;

在我的JSP中,我将迭代器设为

In my JSP I have the iterator as

如果有3个名称,那么我可以在UI上获取这些名称可以用一些虚拟值初始化它们,但是当我编辑和汇总时,名称数组没有得到更新。请帮助我这方面

If there are 3 names then I can get these names on the UI if I can initialized them with some dummy value but when I edit and sumbit, then "names" array is not getting updated. Please help me in this regard

推荐答案

在Struts2中,当你需要重新填充bean项列表时,你需要引用它们通过指数。请参考以下示例:

In Struts2, when you need to repopulate the list of bean items, you need to refer them through indices. Please refer the example below:

Bean类:

public class Person {

    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

行动类:

public class PersonAction extends ActionSupport {
    private List<Person> persons;

    public List<Person> getPersons() {
        return persons;
    }

    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }

    //Initial Load method
    @Override
    public String execute() {
        persons = new ArrayList<Person>();

        int alpha = 65;
        for(int i = 0; i < 3 ; i++) {
            Person person = new Person();
            person.setId(i);
            person.setName(String.valueOf((char)alpha++));
            persons.add(person);
        }
        return SUCCESS;
    }

    //Function that handles the form submit
    public String updatePerson() {

        for(Person person : persons) {
            System.out.println(person.getId() + ":" + person.getName());
        }

        return SUCCESS;
    }
}

页面:

<s:form action="doUpdate">
         <s:iterator value="persons" status="stat" var="person">
              <s:textfield value="%{#person.name}" name="persons[%{#stat.count}].name"/><br/>
         </s:iterator>
         <s:submit value="Submit"/>
</s:form>

当您提交上述表格时,网址看起来像 doUpdate?人[0]。名称= A1&安培;人[1]。名称= B1&安培;人[2]。名称= C1 。同样,如果您需要更新第一个人对象的ID,您将使用表单将 persons [0] .id = 3 附加到网址。在< s:textfield value =%{#person.name}name =persons [%{#stat.count}]。name/> ,您告诉每个对象,预定义值是人名。 name 属性用于设置渲染的html输入元素;提交表单时将在URL中引用的名称。如果你查看生成的html,你会明白的想法。

When you submit the above form, the url would look like doUpdate?persons[0].name=A1&persons[1].name=B1&persons[2].name=C1. Similarly if you need to update id of the first person object, you will append persons[0].id=3 to the url using form. In the <s:textfield value="%{#person.name}" name="persons[%{#stat.count}].name"/>, you tell that the predefined value is person's name, for each object. The name attribute is to set the rendered html input element; the name which will be referenced in the url when the form is submitted. You will get a clear idea if you look into the generated html.

这篇关于更新struts2中textfield中的值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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