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

来源:互联网 发布:mac chrome 无法下载 编辑:程序博客网 时间:2024/04/27 16:16
在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代码 <%@ page language="java" contentType="text/html;charset=UTF-8"pageEncoding="GBK"%> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%> This is jsp1. Edit
id name age
<%@ page language="java" contentType="text/html;charset=UTF-8"pageEncoding="GBK"%><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%><%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>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代码 <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="GBK"%> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%> This is jsp2. View
id name age
<%@ page language="java" contentType="text/html;charset=UTF-8"pageEncoding="GBK"%><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%><%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>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.如果同时需要删除、添加行的功能: 实现添加不难但实现删除较难,建议有这样需求的还是不要同时提交多行的较好。
原创粉丝点击