Struts2中checkboxlist标签——应用、实现换行

先来看看checkboxlist标签、都有些什么属性:


       <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一致就可以了。但是在编辑页面的时候、我想的是显示出页面的时候、就让它把所有的选项全部示、          但是当前记录并没有选择所有的选项、我需要把选择了的选项在加载编辑页面的时候选中。那么就需要在后台设置一个数组、


 


在后台我用的是Map集合跟Long数组。


Map集合跟Long数组都需要在对应的Action设置get/set方法。




java代码:


 


 public Map getSex() {
  sex = new TreeMap();
  sex.put("woman", "女2");
  sex.put("man", "男");
  return sex;
 }




 public void setSex(Map sex) {
  this.sex = sex;
 }


 


 public List getSex1() {
  sex1 = new ArrayList();
  User o = new User();
  o.setName("man");
  o.setValue("男!");
  User o1 = new User();
  o1.setName("woman");
  o1.setValue("女!");
  sex1.add(o1);
  sex1.add(o);
  return sex1;
 }




 public void setSex1(List sex1) {
  this.sex1 = sex1;
 }




 public String[] getSexkey() {
  sexkey = new String[2];
  sexkey[0]="man";
  sexkey[1]="woman";
  return sexkey;
 }




 public void setSexkey(String[] sexkey) {
  this.sexkey = sexkey;
 }


 


 public String getSexkey1() {
  sexkey1="man";
  return sexkey1;
 }




 public void setSexkey1(String sexkey1) {
  this.sexkey1 = sexkey1;
 }




 private Map sex;
 private List sex1;
 private String[] sexkey;
 private String sexkey1;
 
 class User{
  public User() {
   // TODO Auto-generated constructor stub
  }
  public String getName() {
   return name;
  }
  public void setName(String name) {
   this.name = name;
  }
  public String getValue() {
   return value;
  }
  public void setValue(String value) {
   this.value = value;
  }
  private String name;
  private String value;
 }
 


jsp代码:


<%@ taglib prefix="s" uri="/struts-tags"%>


<div style="background-color: #eee;">//用map无法按指定顺序排列
   <s:checkboxlist name="sex1" list="sex" value="sexkey"></s:checkboxlist>
  <br>//用list可以按指定顺序排列显示
  <s:checkboxlist name="sex1" list="sex1" listKey="name" listValue="value" value="sexkey"></s:checkboxlist>
  <br>
  <s:radio name="sex2" list="sex" value="sexkey1"></s:radio>
</div>


上网查了一下~~~!


修改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>
 />


关于checkboxlist.ftl文件应用是在网络上找到。


?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值