struts使用list提交多行表單(提供完整示例) -开发者知识库

struts使用list提交多行表單(提供完整示例) -开发者知识库,第1张

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
id name age
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
id name age
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.如果同時需要刪除、添加行的功能: 實現添加不難但實現刪除較難,建議有這樣需求的還是不要同時提交多行的較好。

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复