当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Dynamic Textfields in Struts2

问题描述:

All -

New to struts2 here.. I've been reading some tutorials, and even picked up a book so I understand some, but please forgive me if I use the wrong terms.

I'm looking for an example or an explanation on how to create a dynamic list of text fields based on a collection, have the user enter some input and then assign that input back to the object.

Example:

I have a dynamic set of products, I'd like to be able to display a table of product names and textfields where the user can enter a price for that product.

Product1 ....... [ textfield_price1 ]

Product2 ....... [ textfield_price2 ]

...

ProductN

[submit_button ]

How do I then map those textfield values back to each product, most of the examples I have seen are standard forms with static information.

Can anyone point me to the right place ?

Thanks in advance.

网友答案:

Typically you'd use map-based syntax, usually with an ID as the key, and a domain object as the value. This is covered in the type conversion docs.

Nutshell: the action has a map of { ID => Product } for the form data:

public Map<Long, Product> getProductMap() { ... }
// and setter, and private property

Use [] in the JSP with an ID as thename` attribute value:

<s:textfield name="productMap[%{id}]" ... />

You could iterate over a collection of products, a pre-filled map, etc.

分享给朋友:
您可能感兴趣的文章:
随机阅读: