当前页面: 开发资料首页 → JSP 专题 → Struts中用动态选择的元素创建复选框
摘要: Struts 对于复选框的动态呈现和预选提供了优秀的支持。这个诀窍是我合著 Struts Recipes 的原因 —— 那时我已经发现许多与 Struts 框架相关的理论和服务器端信息,但是用户界面编程多数被忽略了,或者被掩盖了。
在用户界面设计中,复选框组不如它的同类 —— 多行选择框那样流行。它们基本上做的是同一件事,即选择映射到单一 name 属性的一组选项。当在组中使用时,复选框执行的功能实际与多行选择框一样,但是它们占据的屏幕空间更多。当希望用户在选择一个或多个选项之前能够看到所有选项的时候,这会很有好处。
虽然在选项不多的时候,多行选择框通常提供更好的观感,但是当选择框必须动态呈现而且包含预选功能时,对企业应用程序来说复选框组会是更好的选择。幸运的是,使用 Struts 框架可以很容易地创建动态复选框组。
在这篇文章中,我将介绍一个简单的诀窍:用 Struts 的 <html:multibox/> 和 <logic:iterate/> 标记在应用程序的视图层呈现大量条目,在本例中是 Java Server Page(JSP)。
我先从使用复选框元素显示简单的 String[] 数组开始,数组中包含喜玛拉雅山的顶峰高度。然后,我将创建另外一个 String[] 数组,包含 selectedMountains ,代表已经选中的复选框。复选框的预选情况会在两个数组的交叉中产生。如果 selectedMountains 的初始数组为空,那么所有复选框最初都会显示为未选中。
创建动态复选框
创建动态复选框的诀窍包含三个主要部分:
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>