`
dada_fangfang
  • 浏览: 144664 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

struts2的checkboxlist学习 -转

阅读更多
struts2里增加了一个新的UT标签s:checkboxlist,下面介绍下使用方法。
s:checkboxlist用于画面上显示一组复选框,缺省是横排输出,后面将介绍如何修改ftl文件使得它能按任意方式输出。
标签格式:
    <s:checkboxlist name="" list="" listKey="" listValue="" value="" />
    name-定义标签名,用于接收画面上选中的复选框,故应与Action里定义的属性一致,且多为数组;
    list-定义集合变量,用于输出复选框到画面上,一般在Action里定义一个List或Map属性;
    listKey-如果在Action里定义的是一个List,则往往会在List里定义一个Bean,它只有两个属性,其中一个(比如id)就在这里设置;
                如果在Action里定义的是一个Map,则Map的key就在这里设置;
    listValue-如果在Action里定义的是一个List,则往往会在List里定义一个Bean,它只有两个属性,另外一个(比如name)就在这里设置;
                  如果在Action里定义的是一个Map,则Map的value就在这里设置;
    value-用于回显画面上被选中的复选框,假如画面有输入检查,如果有错则返回原画面并显示出错信息,这时候就需要使用它。
             一般把它设成和name一致就可以了。
注意点:
    为了能正确显示已被选中的复选框,一定要使得name的数组类型与listKey的类型一致。
    比如,name设成String[] users,则listKey就要设成String id;如果name设成Integer[] users,则listKey就要设成Integer id;
修改ftl文件改变输出方式:
    1.搜索struts2-core-xxx.jar,找到checkboxlist.ftl文件,拷贝出来;
    2.在自己的工程的src下新建template.simple包,放置上述文件;
    3.用文本编辑器打开该文件,修改成自己希望输出的格式,保存,OK;
例子:
    希望画面上每3个复选框输出为一行。
<#--
/*
* $Id: checkboxlist.ftl 804072 2009-08-14 03:16:35Z musachy $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
-->
<#assign itemCount = 0/>
<#if parameters.list?exists>
     <@s.iterator value="parameters.list">
         <#assign itemCount = itemCount + 1/>
         <#if parameters.listKey?exists>
             <#assign itemKey = stack.findValue(parameters.listKey)/>
         <#else>
             <#assign itemKey = stack.findValue('top')/>
         </#if>
         <#if parameters.listValue?exists>
             <#assign itemValue = stack.findString(parameters.listValue)/>
         <#else>
             <#assign itemValue = stack.findString('top')/>
         </#if>
<#assign itemKeyStr=itemKey.toString() />
<#if (itemCount-1)%3 == 0>
<tr>
</#if>
<td>
<input type="checkbox" name="${parameters.name?html}" value="${itemKeyStr?html}" id="${parameters.name?html}-${itemCount}"<#rt/>
         <#if tag.contains(parameters.nameValue, itemKey)>
  checked="checked"<#rt/>
         </#if>
         <#if parameters.disabled?default(false)>
  disabled="disabled"<#rt/>
         </#if>
         <#if parameters.title?exists>
  title="${parameters.title?html}"<#rt/>
         </#if>
         <#include "/${parameters.templateDir}/simple/scripting-events.ftl" />
         <#include "/${parameters.templateDir}/simple/common-attributes.ftl" />
/>
<label for="${parameters.name?html}-${itemCount}" class="checkboxLabel">${itemValue?html}</label>
</td>
<#if itemCount%3 == 0>
</tr>
</#if>
     </@s.iterator>
</#if>
<input type="hidden" id="__multiselect_${parameters.id?html}" name="__multiselect_${parameters.name?html}" value=""<#rt/>
<#if parameters.disabled?default(false)>
disabled="disabled"<#rt/>
</#if>
/>
分享到:
评论
1 楼 a85187685a 2011-11-04  

相关推荐

Global site tag (gtag.js) - Google Analytics