struts2,jsp页面向action提交list对象

本文介绍如何在Struts2框架中使用List进行表格数据的传递。通过配置文件指定List类型,实现表单数据与Action之间的交互。文章还提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Struts2 中支持使用List在页面和Action之间直接传递表格数据。下面是一个示例:
public class Person {
int id;
String name;
int age;
float height;
}

这是一个POJO,getter和setting省略了。

action中可以这样使用:
public class MyAction {
public List getPeopleList() { … }
public void setPeopleList( List peopleList ) { … }

}
在我们使用Person类之前,需要添加一个配置文件,MyAction-conversion.properties,把这个文件和MyAction放在一起。
这个文件里只有一行内容:
Element_peopleList=Person
前缀Element_是一个常量,表明等号左边的表达式中跟在这个常量后面的是Action类中一个List类型的字段名。 等号右边的表达式是全类名(包含package)
下面是一个页面的代码片段:
<s:form action="update" method="post" >
       <s:iterator value="peopleList" status="stat">
       <s:hidden name="peopleList[%{#stat.index}].id" value="%{peopleList[#stat.index].id}"/>
       <s:textfield label="Name" name="peopleList[%{#stat.index}].name" value="%{peopleList[#stat.index].name}"/>
       <s:textfield label="Age" name="peopleList[%{#stat.index}].age" value="%{peopleList[#stat.index].age}" />
       <s:textfield label="Height" name="peopleList[%{#stat.index}].height" value="%{peopleList[#stat.index].height}"/>
       <br/>
       </s:iterator>
        <s:submit value="Update"/>
</s:form>

使用这段代码,Struts2 会创建一个Person类的ArrayList,并且用setPersonList这个方法把页面表格中的值传递回Action。
如果你是想从用户界面中动态创建列表值,需要允许Struts2 给列表中类的实例。那么在配置文件MyAction-conversion.properties中添加一行:
CreateIfNull_peopleList = true

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值