struts使用list提交多行表单(提供完整示例)

本文介绍如何使用Struts框架处理多行表单数据,包括创建包含多行人员信息的表单、预填充表单数据、保存及显示数据等步骤。提供了核心类代码示例,展示了如何通过ActionForm收集并处理多行表单数据。

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

在struts框架中常常会遇到多行表单的情况, 如何有效的利用struts框架提供的自动收集机制来处理呢? 这里提供一个示例. 1.示例功能:通过一个【提交】按钮保存多行人员信息,如附件中的图片所示。 2.核心类代码: TestForm.java Java代码 public class TestForm extends ActionForm{ private List voList = null; public List getVoList() { return voList; } public void setVoList(List voList) { this.voList=voList; } } public class TestForm extends ActionForm{ private List voList = null; public List getVoList() { return voList; } public void setVoList(List voList) { this.voList=voList; } } PreAction.java:准备初始数据 Java代码 public class PreAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { TestForm aform = (TestForm)form; TestVo vo1=new TestVo("1","vo1","11"); TestVo vo2=new TestVo("2","vo2","22"); TestVo vo3=new TestVo("3","vo3","33"); List voList = new ArrayList(); voList.add(vo1); voList.add(vo2); voList.add(vo3); aform.setVoList(voList); return mapping.findForward("jsp1"); } } public class PreAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { TestForm aform = (TestForm)form; TestVo vo1=new TestVo("1","vo1","11"); TestVo vo2=new TestVo("2","vo2","22"); TestVo vo3=new TestVo("3","vo3","33"); List voList = new ArrayList(); voList.add(vo1); voList.add(vo2); voList.add(vo3); aform.setVoList(voList); return mapping.findForward("jsp1"); } } jsp1.jsp:编辑保存的页面 Java代码 This is jsp1. Edit
idnameage
This is jsp1. Edit
idnameage
SaveAction.java:保存多行数据 Java代码 public class SaveAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { TestForm aform = (TestForm)form; List voList=aform.getVoList(); //TODO:you can save data to database here. request.setAttribute("voList", voList); return mapping.findForward("jsp2"); } } public class SaveAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { TestForm aform = (TestForm)form; List voList=aform.getVoList(); //TODO:you can save data to database here. request.setAttribute("voList", voList); return mapping.findForward("jsp2"); } } jsp2.jsp:展示保存结果 Java代码 This is jsp2. View
idnameage
This is jsp2. View
idnameage
3.几点说明: A.strut填充ActionForm的方法: 如果key是简单的'name',直接form.setName(map.get('name')); 如果key是'person.name', 执行的操作是 form.getPerson().setName(map.get('person.name'); 如果key是'voList[0].name', 它可以对应到数据或集合中,如对于数组 form.voList[0].name=map.get('voList[0].name'); 对于集合(List) form.getVoList().get(0).setName(map.get('voList[0].name')) B.如果同时需要删除、添加行的功能: 实现添加不难但实现删除较难,建议有这样需求的还是不要同时提交多行的较好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值