正 文

Struts中用动态选择的元素创建复选框


www.7dspace.com  更新日期:2005-12-20 6:33:24  七度空间


  第 2 步. 编写 JSP 代码

  接下来,我要编写页面的 JSP 代码,把 TestForm.java 的信息传递给视图层。在编写这个代码时,关键是要把对应的 Struts 标记库导入 JSP。清单 2 的 JSP 代码表示的是一个简单的表单,显示复选框中相应的框已经选中:

  清单 2. 带有表单的 JSP

%@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 就是选中的。

5页,页码:[1] [2] [3] [4] [5] 

上一篇:网络安全基础 新木桶理论与信息安全
下一篇:Illustrator矢量绘制实例:美丽圣诞树
作者:Danilo Gurovich  来源:IBM ( 责任编辑:7dspace )
收藏此页】【打印】【关闭
站 内 搜 索
 

热 点 导 读
特 别 推 荐