struts2标签使用小结
[code]
1 #号,能访问actioncontext的上下文资源,例子如下:

public class OgnlAction extends ActionSupport implements ServletContextAware, 

ServletRequestAware, SessionAware { 


private static final long serialVersionUID = -6175904602786022346L; 


private HttpServletRequest request; 

private ServletContext application; 

private Map<String, Object> session; 


@SuppressWarnings("all") 

private List<Magazine> magazineList = new ArrayList<Magazine>() { 

{ 

add(new Magazine("时代周刊", 10)); 

add(new Magazine("足球之夜", 19)); 

add(new Magazine("时尚", 20)); 

add(new Magazine("时代影视", 50)); 

} 

}; 


public void setServletRequest(HttpServletRequest request) { 

this.request = request; 

} 


public void setServletContext(ServletContext application) { 

this.application = application; 

} 


@SuppressWarnings("all") 

public void setSession(Map session) { 

this.session = session; 

} 


public String execute() { 


request.setAttribute("account", "account from request"); 

application.setAttribute("account", "account from response"); 

session.put("account", "account from session"); 


return Action.SUCCESS; 

} 


public List<Magazine> getMagazineList() { 

return magazineList; 

} 


public void setMagazineList(List<Magazine> bookList) { 

this.magazineList = bookList; 

}


JSP页面:
所有的ID:<struts:property value="#parameters.id"/> <br/>
第一个ID:<struts:property value="#parameters.id[0]"/> <br/>
request 中取属性:<struts:property value="#request.account"/> <br/>
session 中取属性:<struts:property value="#session.account"/> <br/>
application 中取属性:<struts:property value="#application.account"/> <br/>
attr 中取属性:<struts:property value="#attr.account"/> <br/>
<hr />
价格大于18元的杂志共有 <struts:property value="magazineList.{?#this.price>18}.size()" /> 本,分别是:

<br/> 

<struts:iterator value="magazineList.{?#this.price>18}"> 

<struts:property value="name" /> - ¥<struts:property value="price" />. <br/> 

</struts:iterator> 

<hr />


名称包含“时代”二字的杂志共有 <struts:property value="magazineList.{?#this.name.contains('时代')}.size()"/> 本,分别是:

<br/> 

<struts:iterator value="magazineList.{?#this.name.contains('时代')}"> 

<struts:property value="name" /> - ¥<struts:property value="price" />. <br/> 

</struts:iterator> 

<hr />


“足球之夜”的价格为 ¥<struts:property value="magazineList.{?#this.name.contains('足球之夜')}[0].price"/>。

其中注意OGNL的灵活用法

2 下面两个注意区别:

<struts:label label="#request.account"></struts:label> 

<struts:label label="%{#request.account}"></struts:label>



其中第一个会输出字符串#request.account",第二个会把request.account的结果输出.

3 autocomplete控件:

<s:url id="dataUrl" value="/autocompleteAjaxData.jsp" />



请选择国家:

<struts:autocompleter name="country" theme="ajax" href="%{dataUrl}" 

loadOnTextChange="true" loadMinimumCount="1" autoComplete="false" 

showDownArrow="true" indicator="indicator" /> 

<img id="indicator" src="images/loading.gif" />


其中href为获取数据的URL,indicator可以为一个属性或文字,获取数据的JSP如下:

out.clear(); 


request.setCharacterEncoding("UTF-8"); 


response.setHeader("Pragma", "no-cache"); 

response.setHeader("Cache-Control", "no-cache"); 

response.setDateHeader("Expires", 0); 


String[] countries = { "。。。。。“} 

String country = request.getParameter("country"); 


if (country == null) 

country = ""; 


StringBuffer buffer = new StringBuffer(); 


for (int i = 0; i < countries.length; i++) { 

if (countries[i].toLowerCase() 

.startsWith(country.toLowerCase())) { 

if (buffer.length() != 0) 

buffer.append(","); 

buffer.append("['" + countries[i] + "']"); 

} 

} 


Thread.sleep(500); 


out.print("[" + buffer + "]");


4 连动框doubleselect:

<struts:head theme="ajax" /> 

<% 

List<String> provinceList = new ArrayList<String>(); 

provinceList.add("北京"); 

provinceList.add("上海"); 

provinceList.add("天津"); 

provinceList.add("重庆"); 

provinceList.add("河北"); 

provinceList.add("河南"); 

provinceList.add("山东"); 

provinceList.add("山西"); 

provinceList.add("江苏"); 

provinceList.add("浙江"); 


request.setAttribute("provinceList", provinceList); 


Map<String, List<String>> cityMap = new HashMap<String, List<String>>(); 


{ 

// 北京市 

List<String> cityList = new ArrayList<String>(); 


cityList.add("东城区"); 

cityList.add("西城区"); 

cityList.add("海淀区"); 

cityList.add("朝阳区"); 


cityMap.put("北京", cityList); 

} 

{ 

// 山东省 

List<String> cityList = new ArrayList<String>(); 


cityList.add("青岛"); 

cityList.add("济南"); 

cityList.add("潍坊"); 

cityList.add("烟台"); 

cityList.add("淄博"); 


cityMap.put("山东", cityList); 

} 


request.setAttribute("cityMap", cityMap); 

%> 


<struts:form action="login"> 

<struts:doubleselect name="province" list="#request.provinceList" 

doubleName="city" doubleList="#request.cityMap.get(top)" label="请选择省份、市"/> 

</struts:form>


5 optiontransferselect标签

<% 

List<String> provinceSelected = new ArrayList<String>(); 


provinceSelected.add("北京"); 

provinceSelected.add("上海"); 

provinceSelected.add("天津"); 

provinceSelected.add("重庆"); 

provinceSelected.add("河北"); 

provinceSelected.add("河南"); 

provinceSelected.add("山东"); 

provinceSelected.add("山西"); 

provinceSelected.add("江苏"); 

provinceSelected.add("浙江"); 


request.setAttribute("provinceSelected", provinceSelected); 


List<String> provinceUnselected = new ArrayList<String>(); 


provinceUnselected.add("湖南"); 

provinceUnselected.add("湖北"); 

provinceUnselected.add("云南"); 

provinceUnselected.add("广东"); 

provinceUnselected.add("广西"); 


request.setAttribute("provinceUnselected", provinceUnselected); 

%> 

<struts:form action="login"> 

<struts:optiontransferselect name="province" 

doubleList="#request.provinceSelected" 

list="#request.provinceUnselected" doubleName="city" leftUpLabel="向上" 

leftDownLabel="向下" rightDownLabel="向下" rightUpLabel="向上" 

leftTitle="已经选中的省份" headerKey="" headerValue="---请选择省份---" 

rightTitle="剩余的省份" /> 

</struts:form>


6 TAB标签

<s:tabbedPanel id="test"> 


<s:div id="one" label="第一个 TabItem" theme="ajax" labelposition="top" 

cssStyle="padding: 10px; "> 

第一个 TabItem <br /> 

第一个 TabItem <br /> 

</s:div> 


<s:div id="two" label="第二个 TabItem" theme="ajax" 

cssStyle="padding: 10px; "> 

 第二个 TabItem <br /> 

 第二个 TabItem <br /> 

</s:div> 


<s:url action="divNews" id="divNewsUrl"></s:url> 

<s:div id="three" label="新闻列表" theme="ajax" href="%{#divNewsUrl}"> 

</s:div> 


</s:tabbedPanel> 


<br/> 


<s:tabbedPanel id="test2" closeButton="tab"> 


<s:div id="one2" label="第一个 TabItem" theme="ajax" labelposition="top" 

cssStyle="padding: 10px; "> 

第一个 TabItem <br /> 

第一个 TabItem <br /> 

</s:div> 


<s:div id="two2" label="第二个 TabItem" theme="ajax" 

cssStyle="padding: 10px; "> 

 第二个 TabItem <br /> 

 第二个 TabItem <br /> 

</s:div> 


<s:div id="three2" label="新闻列表" theme="ajax" href="%{#divNewsUrl}"> 

</s:div> 


</s:tabbedPanel>


[/code]