最近刚刚进入公司、就应用到了没学习多久的Struts2框架!Struts1跟XW在学校的时候也没有接触过!刚刚做项目的时候、有点乱、不知道什么时候该做什么、该怎么做!现在基本上熟悉了Struts2的简单应用!看来、学习编程、做项目才是王道啊!
欢迎技术交流。 QQ:138986722
先来看看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方法。
页面代码:
这样出来过后、现在每次在编辑页面上显示的选项太多了!
上网查了一下~~~!
修改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文件应用是在网络上找到。