在struts(使用struts live)中进行多行更新的好方法是什么?

发布于 2024-07-18 12:07:40 字数 790 浏览 5 评论 0原文

不使用 DynaForm 及其同类。

我想使用 POJO 数据传输对象,例如 Person:

public class Person {
   private Long id;
   private String firstName;
   private String lastName;
   // ... getters / setters for the fields
}

在 struts 实时操作表单中,我们将拥有:

public class PersonUpdateForm extends SLActionForm {
   String organization;
   Person[] persons; // all the people will be changed to this organization; they're names and so forth can be updated at the same time (stupid, but a client might desire this)

   // getters / setters + index setters / getters for persons

}

JSP 中相应的 html:text 标记是什么样子才能允许这样做? 如果我切换到“列出人员”字段并使用延迟加载列表(在公共集合中),这会如何改变事情?

在 struts-1.2(.9?) 中似乎没有好的方法来做到这一点?

非常感谢所有帮助! 如果您需要更多背景信息,请告诉我,我可以提供一些。

Without using DynaForm and it's kin.

I would like to use a POJO data transfer object, e.g., Person:

public class Person {
   private Long id;
   private String firstName;
   private String lastName;
   // ... getters / setters for the fields
}

In the struts live action form we would have:

public class PersonUpdateForm extends SLActionForm {
   String organization;
   Person[] persons; // all the people will be changed to this organization; they're names and so forth can be updated at the same time (stupid, but a client might desire this)

   // getters / setters + index setters / getters for persons

}

What would the corresponding html:text tags look like in the JSP to allow this? If I switch to a List persons field and use a lazy-loading list (in commons-collections) how would that change thinsg?

There seems to be no good way to do this in struts-1.2(.9?)

All help is greatly appreciated!!! If you need more context let me know and I can provide some.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2024-07-25 12:07:40

好吧,我相信我已经弄清楚了! 诀窍是让索引 getter 在每次 BeanUtils 的 populate 方法调用 getPersons() 方法时创建一个元素。 代码尚未完成,但我得到了一个积极的结果。 现在是 3:30,我已经被这个问题困住了一段时间了。 似乎没有人知道答案,这让我想用鳟鱼打他们的头。 至于我自己的无知……只能怪他们了!

public List<Person> getPersons() {
   persons.add(new Person()); // BeanUtils needs to know the list is large enough
   return persons;
}

当然,还要添加索引的 getter 和 setter。

我记得我实际上是如何做到这一点的。 您必须将上面的人员列表预先初始化为您希望转移的最大大小。 这是因为 List 首先被转换为数组,然后在数组的每个元素上设置属性,最后使用 setPersons(...) 返回 List。 因此,使用延迟加载列表实现或类似方法(如上面所示)将不适用于 struts live。 以下是您需要做的更详细的事情:

private List<Person> persons = new ArrayList<Person>(MAX_PEOPLE);
public MyConstructor() { for(int i = 0; i < MAX_PEOPLE; i++) persons.add(new Person()); }

public List<Person> getPeopleSubmitted() {
    List<Person> copy = new ArrayList<Person>();
    for(Person p : persons) {
        if(p.getId() != null) copy.add(p); 
        // id will be set for the submitted elements;
        // the others will have a null id
    }
    return copy; // only the submitted persons returned - not the blank templates
}

这基本上就是您必须做的! 但真正的问题是 - 谁还在使用 struts live?!

Okay, I believe I've figured it out! The trick is to have your indexed getter create an element each time the getPersons() method is called by the populate method of BeanUtils. The code is completed yet, but I got a positive looking result. It's 3:30 and I've been stuck on this a while. Nobody seemded to know the answer, which makes me want to smack them in the head with a trout. As for my own ignorance ... I only have them to blame!

public List<Person> getPersons() {
   persons.add(new Person()); // BeanUtils needs to know the list is large enough
   return persons;
}

Add your indexed getters and setters too, of course.

I remember how I actually did this. You must pre-initialize the persons List above to the maximum size you expect to transfer. This is because the List is first converted to an array, the properties then set on each element of the array, and finally the List set back using setPersons(...). Therefore, using a lazy-loading List implementation or similar approach (such as that show above) will NOT work with struts live. Here's what you need to do in more detail:

private List<Person> persons = new ArrayList<Person>(MAX_PEOPLE);
public MyConstructor() { for(int i = 0; i < MAX_PEOPLE; i++) persons.add(new Person()); }

public List<Person> getPeopleSubmitted() {
    List<Person> copy = new ArrayList<Person>();
    for(Person p : persons) {
        if(p.getId() != null) copy.add(p); 
        // id will be set for the submitted elements;
        // the others will have a null id
    }
    return copy; // only the submitted persons returned - not the blank templates
}

That's basically what you have to do! But the real question is - who's using struts live anymore?!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文