当前页面: 开发资料首页 → JSP 专题 → Struts中用动态选择的元素创建复选框
摘要: Struts中用动态选择的元素创建复选框
package com.strutsrecipes; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; public final class CheckboxTestForm extends ActionForm { // Instance Variables /*Mountains "pre-selected"...*/ private String[] selectedMountains = {"Everest","K2","Lhotse"}; /*the ten tallest Mountains to iterate through*/ private String[] mountains = {"Everest","K2","Kangchenjunga","Lhotse", "Makalu","Kangchenjunga South", "Lhotse Middle","Kangchenjunga West", "Lhotse Shar","Cho Oyu"}; /*Getter for selectedMountains*/ public String[] getSelectedMountains() { return this.selectedMountains; } /*Setter for selectedMountains*/ public void setSelectedMountains(String[] selectedMountains) { this.selectedMountains = selectedMountains; } /*Getter for the mountains*/ public String[] getMountains() { return this.mountains; } /*Setter for the mountains*/ public void setMountains(String[] mountains) { this.mountains = mountains; } }
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> <%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> <%@taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%> <%-- html code, etc... --> <html:form action="/FormAction" name="testForm" type="com.strutsrecipes.CheckboxTestForm"> <h4><bean:message key="testForm.instruction"/></h4> <logic:iterate name="testForm" property="mountains" id="mountain"> <%-- create the checkbox and selected attribute --> <html:multibox property="selectedMountains"> <bean:write name="mountain"/> </html:multibox> <%-- create the label, note that "br" tag will format it vertically --> <bean:write name="mountain"/><br/> </logic:iterate> <br/> <html:submit/><html:reset/> </html:form> <%-- some more html code, etc... -->
注意,我用 Struts <bean:message/> 标记表示文本,用 <html:multibox/> 表示 HTML 复选框,用 <logic:iterate/> 标记在数组中迭代并创建相应内容。我的表单在 JSP 中通过 <html:form/> 标记被实例化。
下一步是对 <logic:iterate/> 标记中的 mountains 字段进行迭代。在这么做的时候,我创建了一个变量(mountain),用它来填充复选框,并用 <bean:write/> 标记给它一个标签。要在复选框中创建 selected 属性,我要再次使用 <logic:iterate/> 和 <html:multibox/> 标记。<html:multibox/> 标记中的 property 属性由 selectedMountains 字段填充。当 selectedMountains 等于 mountain 时,selectBox 就是选中的。
第 3 步. 编写 Action 类
最后一步是编写 Action 类。清单 3 比起其他清单,做的事并不多。我做的只是得到 selectedMountains 的 String[] 数组,并使它可以用于页面:
清单 3. 表单的 Action
import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * A simple Action for Checkbox test. * * @author Danilo Gurovich */ public final class CheckboxTestAction extends Action { // -------------------------- OTHER METHODS -------------------------- /** * The execute method * * @param mapping ActionMapping * @param form CheckboxTestForm * @param request HttpServletRequest * @param response HttpServletRespons * @return success to the confirmation page * @throws ServletException not thrown, but could be! * @throws Exception ditto. */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, Exception { // Extract attributes needed String[] selectedMountains = ((CheckboxTestForm) form).getSelectedMountains() ; System.out.println("htmlString RETURNED*\n" + selectedMountains.toString()); //Save the htmlString in the session for later... HttpSession session = request.getSession(); session.setAttribute(CheckboxConstants.MOUNTAINS, selectedMountains); return (mapping.findForward("success")); } }
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> <%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> <%@taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%> <%-- html code, etc... --> <logic:iterate id="mountain" property="mountains" name="testForm"> <bean:write name="mountain"/><br/> </logic:iterate> <hr size=5 color="black"/> <%-- some more html code, etc... -->
<html:form action="/FormAction" name="testForm" type=" com.strutsrecipes.CheckboxTestForm">
<logic:iterate id="mountain" property="mountains" name="testForm">
<html:multibox property="selectedMountains"> <bean:write name="mountain"/> </html:multibox> <bean:write name="mountain"/><br/>
<logic:iterate id="mountainlb" property="mountainslb" name="testForm"> <bean:define id="mountainbean" name="mountainlb "type="org.apache.struts.util.LabelValueBean"/> <html:multibox property="selectedMountains"> <bean:write name="mountainbean" property="value"/> </html:multibox> <bean:write name="mountainbean" property="label"/><br/> </logic:iterate>