TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
1,if,elseif,else标签控制流程
<struts:if><struts:elseif><struts:else>,其中if标签与else标签都有test属性,返回true或者false。
[color=#990066,strength=3);]ifTag.jsp
<struts:form action="if">
<struts:textfield name="name" label="姓名:"></struts:textfield>
<struts:submit value=" 提交 "></struts:submit>
</struts:form>
<!-- 利用request参数判断 -->
<struts:if test="#parameters.name[0] == 'Kurt'"> <!-- 如果参数为Kurt -->
Hello,Kurt.
</struts:if>
<struts:elseif test="#parameters.name[0] == 'Mart'">
Hello,Mart.
</struts:elseif>
<struts:else> <!-- 否则 -->
Hello,Other Buddies.
</struts:else>
<br/>
<br/>
<!-- 利用Action属性判断 -->
<struts:if test="name == 'Kurt'"> <!-- 如果变量为Kurt -->
Hello,Kurt.
</struts:if>
<struts:elseif test="name == 'Mart'"> <!-- 如果name变量为Matt -->
Hello,Matt.
</struts:elseif>
<struts:else> <!-- 否则 -->
Hello,Other Buddies.
</struts:else>
[color=#990066,strength=3);]ifAction.java
package com.zhangjie.struts2.action;
import org.apache.struts2.config.Namespace;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import com.opensymphony.xwork2.Action;
@Namespace(value="/")
@Results([email=value=%7B@Result(name=%22success%22,value=%22/ifTag.jsp]value={@Result(name="success",value="/ifTag.jsp[/email]")})
public class ifAction {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute(){
return Action.SUCCESS;
}
}
2,append标签连接多个List
append标签用于将多个List连成一个List,相当于Java中的list1.addAll(list2),例如:
[color=#990066,strength=3);]appendTag.jsp
<h3><font color="#0000FF"><struts:append/> 标签</font></h3>
<struts:append id="myAppendList">
<struts:param value="%{maleList}"></struts:param> <!-- 追加maleList -->
<struts:param value="%{femaleList}"></struts:param> <!-- 追加femaleList -->
</struts:append>
<struts:iterator value="%{#myAppendList}"> <!-- 遍历 myAppendList -->
<struts:property/><br/> <!-- 输出里面的每一个值 -->
</struts:iterator>
[color=#990066,strength=3);]AppendAction.java
@Results([email=value=%7B@Result(name=%22success%22,value=%22/appendTag.jsp]value={@Result(name="success",value="/appendTag.jsp[/email]")})
public class AppendAction {
private List<String> maleList = new ArrayList<String>(){
{
add("Machael");
add("Scorfield");
}
};
private List<String> femaleList = new ArrayList<String>(){
{
add("Janice");
add("Marry");
}
};
public String execute(){
return Action.SUCCESS;
}
public List<String> getFemaleList() {
return femaleList;
}
public void setFemaleList(List<String> femaleList) {
this.femaleList = femaleList;
}
public List<String> getMaleList() {
return maleList;
}
public void setMaleList(List<String> maleList) {
this.maleList = maleList;
}
}
3,generator标签将字符转化为List
[color=#990066,strength=3);]generatorTag.jsp
<h3><font color="#0000FF"><struts:generator/>标签</font></h3>
<struts:generator val="%{'李宁,安踏,双星,阿达,耐克'}" separator=","><!-- 分割字符串 -->
<struts:iterator> <!-- 遍历List数据 -->
<struts:property/> <!-- 输出每一个值 -->
</struts:iterator>
</struts:generator>
4,merge标签取集合的并集
merge标签类似于append标签,能将两个List连接成一个List,他们的用法完全一致。不同的是,append标签先添加maleList的所有元素,后添加female的所有元素。而merge标签是先添加maleList的第一个元数,再添加femaleList的第一个元素;在添加maleList的第二个元素,。。。。
[color=#990066,strength=3);]mergeTag.jsp
<h3><font color="#0000FF"><struts:merge/>标签</font></h3>
<struts:merge id="myMergeList">
<struts:param value="%{maleList}"></struts:param>
<struts:param value="%{femaleList}"></struts:param>
</struts:merge>
<struts:iterator value="%{#myMergeList}">
<struts:property/>
</struts:iterator>
5,subset标签过滤集合元素
subset标签用于筛选集合里元素。他使用一个Filter,将不合格的元素过滤掉,剩下原集合的一个子集:
[color=#990066,strength=3);]subsetTag.jsp
<h3><font color="#0000FF"><struts:subset/>标签</font></h3>
<struts:subset source="maleList" decider="decider"> <!-- 筛选标签 -->
<struts:iterator> <!-- 遍历筛选后的结果 -->
<struts:property/> <br/><!-- 输出每个遍历的值 -->
</struts:iterator>
</struts:subset>
注:maleList是Action中的集合属性,decider是Action中的Filter属性。decider必须实现SubsetIteratorFilter.Decider接口以及decide()方法。为简单一些,Action中使用了匿名内部类,代码如下:
[color=#990066,strength=3);]SubsetAction.java
@Results([email=value=%7B@Result(name=%22success%22,value=%22/subsetTag.jsp]value={@Result(name="success",value="/subsetTag.jsp[/email]")})
public class SubsetAction {
private List<String> maleList = new ArrayList<String>(){
{
add("Machael");
add("Scorfield");
add("Other");
}
};
private SubsetIteratorFilter.Decider decider = new SubsetIteratorFilter.Decider(){ //匿名类
public boolean decide(Object obj) throws Exception{ //覆盖过滤方法
if(obj instanceof String){ //如果是String 类型
if(obj.equals("Other")) //如果是other
return false; //返回false,被过滤掉
else
return true;
}
return false;
}
};
public String execute(){ //主方法
return Action.SUCCESS; //返回成功页面
}
public SubsetIteratorFilter.Decider getDecider() {
return decider;
}
public void setDecider(SubsetIteratorFilter.Decider decider) {
this.decider = decider;
}
public List<String> getMaleList() {
return maleList;
}
public void setMaleList(List<String> maleList) {
this.maleList = maleList;
}
}
|
|