使用复选框(html:multibox)动态删除多条记录

    技术2022-05-11  128

    大家好,这是我第一次发表文章,不足之处请指教。

    我们在使用struts进行web开发的时候会面临一个问题,如下:

    1. search,然后使用标签<logic:iterate>显示记录

    2. 每一条记录后面增加一个"修改"或者"删除"链接(按钮)

    3. 产生的问题:全部或者多条删除将怎么实现?

    解决的办法:

    使用复选框<html:multibox>

    使用方法:

    1.创建ActionForm:  AForm

    public class AForm extends ActionForm {

           private String str[];

           public String[] getStr() {  return str; }

          public void setStr(String[] strings) {  str = strings; }

    }

    2.创建Action:  GAction

    public class GAction extends Action {

      public ActionForward execute(ActionMapping mapping,  ActionForm form,                                         HttpServletRequest request, HttpServletResponse response)throws Exception {    

         ActionErrors errors = new ActionErrors();     ActionForward forward = new ActionForward(); // return value     AForm aForm = (AForm) form;

         try {             int j=ArrayUtils.getLength(aForm.getStr());//使用apache的lang包,注意加上commons-lang.jar          for (int i = 0; i < j; i++) {                System.out.println(aForm.getStr()[i]);          }     } catch (Exception e) {}

         return mapping.findForward("success");  }}

    3. 创建action-mappings和form-beans配置,在struts-config.xml中相应增加:

     <action name="aForm" path="/g" scope="session" type="strutstest.actions.GAction"></action> <form-bean name="aForm" type="strutstest.forms.AForm"></form-bean> 4. 创建JSP页面a.jsp

    <%@ taglib uri="/tags/struts-html" prefix="html" %><%@ taglib uri="/tags/struts-bean" prefix="bean" %><%@ taglib uri="/tags/struts-logic" prefix="logic" %><html:html><BODY><%java.util.ArrayList list = new java.util.ArrayList();list.add("First");list.add("Second");list.add("Third");list.add("Fourth");list.add("Fifth");pageContext.setAttribute("list", list, PageContext.PAGE_SCOPE);%><html:form action="/g"> <logic:iterate id="element" name="list">   <html:multibox property="str"><bean:write name="element" /></html:multibox>:<bean:write name="element" /><BR> </logic:iterate> <htmlx:submit>delete</htmlx:submit></html:form></BODY></html:html>

    5. 运行a.jsp

    6. 勾选前3个复选框,点击按钮delete,控制台显示如下信息:

       3   First   Second   Third

       接下来我们根据这3条记录id进行删除操作。     在当时的项目中我看到有同事使用JavaScript对<html:multibox>进行处理的,可惜我对JavaScript不熟,^_^


    最新回复(0)