大家對<s:checkboxlist/>的最大的疑問可能是:“如何在默認情況下,选中某些checkbox?”
答案其實很簡單,只需要將其“value”屬性設為你的要选中的值,如以代碼所示:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Struts 2 Cool Tags - <s:checkboxlist/ ></title>
<s:head />
</head>
<body>
<h2><s:checkboxlist/></h2>
<s:form action="Store" >
<s:checkboxlist name="skills1"
label="Skills 1"
list="{ 'Java', '.Net', 'RoR', 'PHP' }"
value="{ 'Java', '.Net' }" />
<s:checkboxlist name="skills2"
label="Skills 2"
list="#{ 1:'Java', 2: '.Net', 3: 'RoR', 4: 'PHP' }"
listKey="key"
listValue="value"
value="{ 1, 2, 3 }"/>
</s:form>
</body>
</html>
当自己通过Action把值传入 value时确总是选不中?
解决办法。把要传入Value的字符传转换为 数组 传入到Value里就好了
<s:bean name="com.action.IndexMerchant"
id="f">
<s:param name="id" value="${merchant.merchantId}"></s:param>
</s:bean>
<s:checkboxlist name="m" list="#f.tasteClasses" listKey="id"
listValue="name" value="#f.tasteClassesByMerchantId"></s:checkboxlist>
这个value里有值要对应listKey的值(listValue是显示出来的值)。
<s:doubleselect />
大家看Struts 2的showcase的例子,<s:doubleselect />的用法如下所示:
<s:doubleselect
tooltip="Choose Your State"
label="State"
name="region" list="{'North', 'South'}"
value="'South'"
doublue="'Florida'"
doubleList="top == 'North' ? {'Oregon', 'Washington'} : {'Texas', 'Florida'}"
doubleName="state"
headerKey="-1"
headerValue="---------- Please Select ----------"
emptyOption="true" />
很多朋友問:“上面的‘list’屬性只有兩個值,如果我有三個或更多的值,‘doublelist’屬性應該如何設定呢?”
我建議的做法是先定義一個Map類型的對象,鍵為“list”的集合,值則為“doubleList”的集合,然後“doubleList”的OGNL寫成“#myMap[top]”,如以下代碼所示:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Struts 2 Cool Tags - <s:doubeselect/ ></title>
<s:head />
</head>
<body>
<h2><s:doubleselect/></h2>
<s:form action="Store" >
<s:set name="foobar"
value="#{'Java': {'Spring', 'Hibernate', 'Struts 2'}, '.Net': {'Linq', ' ASP.NET 2.0'}, 'Database': {'Oracle', 'SQL Server', 'DB2', 'MySQL'}}" />
<s:doubleselect list="#foobar.keySet()"
doubleName="technology"
doubleList="#foobar[top]"
label="Technology" />
</s:form>
</body>
</html>