1.分支判断
首先要引入struts2的标签库
<%@ taglib uri="/struts-tags" prefix="s"%>
If elseif else使用 test内部是ognl表达式取值
public String execute() throws Exception {
System.out.println(username);
ActionContext ac = ServletActionContext.getContext();
ac.put("age", 18);
return super.execute();
}
<s:if test="#age < 16">
<s:property value="小孩"/>
</s:if>
<s:elseif test="#age >= 16 && #age < 18">
<s:property value="'未成年'"/>
</s:elseif>
<s:else>
<s:property value="'成年人'"/>
</s:else>
2.循环
使用<s:iterator>标签
属性:
Value是从ActionContext中获取的集合key,不需要#
Var:是每次从集合中取值赋值的变量
使用的时候需要加#,#str
public String execute() throws Exception {
System.out.println(username);
ActionContext ac = ServletActionContext.getContext();
String [] arrStr = {"aa","bb","cc"};
ac.put("arrStr", arrStr);
return super.execute();
}
<h3>循环数组</h3>
<s:iterator value="arrStr" var="str">
<s:property value="#str"/>
</s:iterator>
------------------------------
ActionContext ac = ServletActionContext.getContext();
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
ac.put("list", list);
<h3>循环list</h3>
<s:iterator value="list" var="str">
<s:property value="#str"/>
</s:iterator>
------------------------------
public String execute() throws Exception {
System.out.println(username);
ActionContext ac = ServletActionContext.getContext();
Map<String,Object> map = new HashMap<String,Object>();
map.put("key1", "value1");
map.put("key2", "value2");
ac.put("map", map);
return super.execute();
}
<h3>循环map</h3>
<s:iterator value="map" var="mapObj">
<s:property value="#mapObj.key"/>------><s:property value="#mapObj.value"/><br>
</s:iterator>
对于对象集合循环
Status属性可以给循环设置参数
变量:
Index:当前循环的索引号,从0开始
Count:当前循环的顺序号,从1开始
First:是否是第一行
List:是否是最后一行
odd:是否是奇数
even:是否是偶数
begin:从数字几开始
end:到数字几结束
step:步长
<table border="1">
<tr>
<th>id</th>
<th>姓名</th>
<th>性别</th>
<th>索引</th>
<th>序号</th>
<th>是否首行</th>
<th>是否尾行</th>
<th>奇数</th>
<th>偶数</th>
</tr>
<s:iterator value="personList" var="person" status="status">
<tr bgcolor='<s:property value="#status.odd?'#c3f3c3':'#f3c3f3'"/>'>
<td><s:property value="#person.personId"/></td>
<td><s:property value="#person.personName"/></td>
<td><s:property value="#person.gender == 1?'男':'女'"/></td>
<td><s:property value="#status.index"/></td>
<td><s:property value="#status.count"/></td>
<td><s:property value="#status.first"/></td>
<td><s:property value="#status.last"/></td>
<td><s:property value="#status.odd"/></td>
<td><s:property value="#status.even"/></td>
</tr>
</s:iterator>
</table>
3.输出标签
<S:property>
属性:
Value:用于通过ognl表达式来取值
Default:如果value值是空就给一个默认值
EscapeHtml:是否被浏览器解析,默认是true不解析,false 是解析
<s:property value="#name"/>
<s:property value="#name1" default="空值"/>
<s:property value="'<a href>张</a>'" escapeHtml="false"/>
4.日期输出标签
<s:date>
属性:
Name:取日期的ognl表达式的值
Format:要展示的日期的格式
<h3>日期输出标签</h3>
<s:date name="#ctime" format="yyyy-MM-dd HH:mm:ss"/>
ActionContext ac = ServletActionContext.getContext();
ac.put("ctime", new Date());
5.页面动态包含
<s:action>
属性:
Name:要请求的Action
ExecuteResult:是否展示Action的执行结果,true是展示,false不展示
<h3>页面包含</h3>
<s:action name="hello1" executeResult="true"></s:action>
hello1是新建的action和配置的action
6.超链接标签
<s:a>
属性:
Action:要链接的动作类的名称,标签会在Action的值后面自动的加上后缀
<s:param>是<s:a>内部的元素,param主要是给a链接赋予参数的,可以自动的对中文编码
<h3>超链接</h3>
<s:a action="hello">我是a链接
<s:param name="username" value="'aaaa'"></s:param>
<s:param name="job" value="'teacher'"></s:param>
</s:a>
在execute中打印即可
6.struts2对el表达式的支持
El表达式
${name}:调用pageContext.findAttribute(“name”),从页面,请求,会话,应用范围去查找key=name对应的值
<table border="1">
<tr>
<th>id</th>
<th>姓名</th>
<th>性别</th>
<th>索引</th>
<th>序号</th>
<th>是否首行</th>
<th>是否尾行</th>
<th>奇数</th>
<th>偶数</th>
</tr>
<s:iterator value="personList" var="p" status="status">
<tr bgcolor="${status.odd?'#c3f3c3':'#f3c3f3'}">
<td>${p.personId }</td>
<td>${p.personName }</td>
<td>${p.gender == 1?'男':'女' }</td>
<td>${status.index}</td>
<td>${status.count}</td>
<td>${status.first}</td>
<td>${status.last}</td>
<td>${status.odd}</td>
<td>${status.even }</td>
</tr>
</s:iterator>
</table>
Struts中el表达式${name}查找范围依次按着如下顺序查找: 请求, 值栈,contextMap, 会话,应用