注意:
1.某些低版本的struts2,tagAction.action?pid=%{id}是会报错的,正确的应该写成tagAction?pid=%{id}
2.ognl表达式和el表达式是可以嵌套使用的,限于标签外部。不能用于标签里。<s:a action="tagAction?pid=${pid}">是错误的。
3.在html标签中,只能使用el表达式。<a href="tagAction.action?pid=%{pid}">是错误的。
1.<s:select>回显的值必须在对象栈中
*用map集合测试
public class TestTagAction extends ActionSupport {
public String execute() throws Exception {
Map<String,Person> map = new HashMap<String,Person>();
map.put("p1", new Person("1","xxc1"));
map.put("p2", new Person("2","xxc2"));
map.put("p3", new Person("3","xxc3"));
ActionContext.getContext().put("pMap", map);//将map存到map栈中
ActionContext.getContext().getValueStack().set("id","p3");//将id和p3以map形式存放在对象栈中,key为id,value为p3
return SUCCESS;
}
}
<s:select class="hight_newText" name="user.userBasic.enterSchoolYear" list='#{"2008":"2008","2009":"2009","2010":"2010","2011":"2011","2012":"2012","2013":"2013"}'/>
页面:
*list:表示map栈中的key
*listKey表示map集合的key,翻译成html标签就是option标签中的value
*listValue表示map集合的value,翻译成html标签就是option标签中显示的内容
*headerKey表示第一个option的value
*headerValue表示第一个option标签中显示的值
*name表示从对象栈中获取哪个属性,用于回显
<s:select list="#pMap" listKey="key" listValue="value.name" headerKey="" headerValue="请选择" name="id"></s:select>
*用List集合测试
public String listSelect(){
List<Person> list = new ArrayList<Person>();
list.add(new Person("1","xxc1"));
list.add(new Person("2","xxc2"));
list.add(new Person("3","xxc3"));
ActionContext.getContext().put("pList", list);//将map存到map栈中
ActionContext.getContext().getValueStack().push(new Person("2","xxc2"));//将对象放入对象栈中
return "list";
}
页面:
*list:表示map栈中的key
*listKey:表示list集合中bean的属性,将其作为option的value属性
*listValue:表示list集合中bean的属性,将其作为option标签中显示的内容
*headerKey表示第一个option的value
*headerValue表示第一个option标签中显示的值
*name表示从对象栈中获取哪个属性,用于回显,如果是list则表示这个list集合中bean中的某个属性<s:select list="#pList" listKey="id" listValue="name" headerKey="" headerValue="请选择(litst)" name="id"></s:select>
2.<s:checkboxlist>
public String checkBoxSelect(){
List<Person> list = new ArrayList<Person>();
list.add(new Person("1","xxc1"));
list.add(new Person("2","xxc2"));
list.add(new Person("3","xxc3"));
ActionContext.getContext().put("pList", list);//将map存到map栈中
ActionContext.getContext().getValueStack().push(new Person("2","xxc2"));
return "checkBox";
}
页面:
*list:表示map栈中的key
*listKey:表示checkbox的value属性
*listValue:表示label标签中显示的内容
*name表示从对象栈中获取哪个属性,用于回显
页面:
<s:checkboxlist list="#pList" listKey="id" listValue="name" name="id"></s:checkboxlist>
翻译成:
<input type="checkbox" name="id" value="1" id="id-1" />
<label for="id-1" class="checkboxLabel">xxc1</label>
<input type="checkbox" name="id" value="2" id="id-2" checked="checked" />
<label for="id-2" class="checkboxLabel">xxc2</label>
<input type="checkbox" name="id" value="3" id="id-3" />
<label for="id-3" class="checkboxLabel">xxc3</label>
<input type="hidden" id="__multiselect_id" name="__multiselect_id" value="" />
3.<s:iterator>
(1)List<Person>形式迭代:
public String iteratorTag(){
List<Person> list = new ArrayList<Person>();
list.add(new Person("1","xxc1"));
list.add(new Person("2","xxc2"));
list.add(new Person("3","xxc3"));
list.add(new Person("4","xxc4"));
list.add(new Person("5","xxc5"));
ActionContext.getContext().put("pList", list);//将map存到map栈中
return "iterator";
}
页面迭代:
<html>
<head>
<style type="text/css">
.odd{
background-color: red;
}
.even{
background-color: blue;
}
</style>
</head>
<body>
<s:debug/>
<table>
<tr>
<td>ID</td>
<td>NAME</td>
<td>count</td>
<td>index</td>
<td>odd</td>
<td>even</td>
</tr>
<!--
value:表示后台传递过来的数据,如果value不写的话,是取值桟中的栈顶集合
var:表示当前遍历对象的别名,存在于map栈中
status:org.apache.struts2.views.jsp.IteratorStatus对象,以status后的属性作为key存在于mao栈中
其中的属性有:
*count:当前元素个数
*index:当前元素的索引
*odd:是否为奇数
*even:是否为偶数
-->
<s:iterator value="#pList" var="xxx" status="count">
<tr class="<s:property value="#count.index%3==0?'odd':'even'"/>">
<td><s:property value="#xxx.id"/></td>
<td><s:property value="#xxx.name"/></td>
<td><s:property value="#count.count"/></td>
<td><s:property value="#count.index"/></td>
<td><s:property value="#count.odd"/></td>
<td><s:property value="#count.even"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
(2)Map<Person>形式迭代:
public String mapIterator(){
Map<String,Person> personMap = new HashMap<String,Person>();
personMap.put("1",new Person("p1","pname1"));
personMap.put("2",new Person("p2","pname2"));
personMap.put("3",new Person("p3","pname3"));
ActionContext.getContext().put("personMap", personMap);
return "personMap";
}
页面迭代:
<body>
<table>
<tr>
<td>ID</td>
<td>PID</td>
<td>PNAME</td>
</tr>
<s:iterator value="#personMap"><!-- map集合在迭代的时候,以entrySet形式存在于值桟的栈顶 -->
<tr>
<td><s:property value="key"/></td>
<td><s:property value="value.id"/></td>
<td><s:property value="value.name"/></td>
</tr>
</s:iterator>
</table>
</body>
(3)Person[ ]形式迭代:
public String persons(){
Person[] persons = new Person[3];
persons[0] = new Person("p1","pname1");
persons[1] = new Person("p2","pname2");
persons[2] = new Person("p3","pname3");
ActionContext.getContext().put("persons", persons);
return "persons";
}
页面迭代:
<body>
<table>
<tr>
<td>PID</td>
<td>PNAME</td>
</tr>
<!--
如果传递过来的数组,数组里是bean,那么每次迭代的时候,当前元素都存在于值桟的栈顶,只要写相应的属性就可以了。
-->
<s:iterator value="#persons">
<tr>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
</tr>
</s:iterator>
</table>
</body>
(4)List<Map<String,Person>>形式迭代:
public String listMap(){
List<Map<String,Person>> listMap = new ArrayList<Map<String,Person>>();
Map<String,Person> personMap = new HashMap<String,Person>();
personMap.put("4",new Person("p4","pname4"));
personMap.put("5",new Person("p5","pname5"));
personMap.put("6",new Person("p6","pname6"));
listMap.add(personMap);
ActionContext.getContext().put("listMap", listMap);
return "listMap";
}
页面迭代:
<body>
<table>
<tr>
<td>ID</td>
<td>PID</td>
<td>PNAME</td>
</tr>
<!-- list -->
<s:iterator value="#listMap">
<!-- map,由于iterator遍历的时候,当前元素会处于值桟栈顶,这里的value已经存在于栈顶,可以省略,也可以写value="top" top为关键字,表示栈顶元素-->
<s:iterator value="top">
<tr>
<td><s:property value="key"/></td>
<td><s:property value="value.id"/></td>
<td><s:property value="value.name"/></td>
</tr>
</s:iterator>
</s:iterator>
</table>
</body>
(5)Map<String,List<Person>>形式迭代:
public String mapList(){
Map<String,List<Person>> mapList = new HashMap<String,List<Person>>();
List<Person> list = new ArrayList<Person>();
list.add(new Person("pid7","pname7"));
list.add(new Person("pid8","pname8"));
list.add(new Person("pid9","pname9"));
mapList.put("77", list);
mapList.put("88", list);
ActionContext.getContext().put("mapList", mapList);
return "mapList";
}
页面迭代:
<body>
<table>
<tr>
<td>ID</td>
<td>PID</td>
<td>PNAME</td>
</tr>
<!--
迭代map
-->
<s:iterator value="mapList">
<!--
由于此时值桟的栈顶是entryMap,所以直接取value(List集合)
-->
<s:iterator value="value">
<tr>
<td>
<!--
取外层map集合的key,由于现在已经在迭代list集合的person类的了,所以栈顶对象变成了person,外层map集合在值桟的第二个位置了
但是这并不影响取值,因为struts2会依次向下取,直到取到值桟里有key为key的value
-->
<s:property value="key"/>
</td>
<td>
<s:property value="id"/>
</td>
<td>
<s:property value="name"/>
</td>
</tr>
</s:iterator>
</s:iterator>
</table>
</body>
(6)Map<String,Map<String,Person>>形式迭代:
public String mapMap(){
Map<String,Map<String,Person>> mapMap = new HashMap<String,Map<String,Person>>();
Map<String,Person> personMap = new HashMap<String,Person>();
personMap.put("innerMap1", new Person("pid1","pname1"));
personMap.put("innerMap2", new Person("pid2","pname2"));
personMap.put("innerMap3", new Person("pid3","pname3"));
mapMap.put("outMap1", personMap);
ActionContext.getContext().put("mapMap", mapMap);
return "mapMap";
}
页面迭代:
<body>
<table>
<tr>
<td>ID</td>
<td>PID</td>
<td>PNAME</td>
</tr>
<!--
迭代map
-->
<s:iterator value="mapMap">
<!--
此时栈顶是entrySet
-->
<s:iterator value="value">
<tr>
<td>
<!--
这里的key只能取到内层map的key了
外层map和内侧map都有key,而外层map存在于栈中而非栈顶
struts2取值的时候,是从栈顶往栈底取,只要有一个满足就立即输出
内层map此时在被迭代,所以内层map的entrySet在栈顶,所以key输出的是内层map的
-->
<s:property value="key"/>
</td>
<td>
<s:property value="value.id"/>
</td>
<td>
<s:property value="value.name"/>
</td>
</tr>
</s:iterator>
</s:iterator>
</table>
</body>
(7)List<List<Map<String,Person>>>形式迭代:
public String listListMap(){
List<List<Map<String,Person>>> listListMap = new ArrayList<List<Map<String,Person>>>();
List<Map<String,Person>> listMap1 = new ArrayList<Map<String,Person>>();
Map<String,Person> mapPerson1 = new HashMap<String,Person>();
mapPerson1.put("1", new Person("pid1","pname1"));
mapPerson1.put("2", new Person("pid1","pname2"));
mapPerson1.put("3", new Person("pid1","pname3"));
listMap1.add(mapPerson1);
List<Map<String,Person>> listMap2 = new ArrayList<Map<String,Person>>();
Map<String,Person> mapPerson2 = new HashMap<String,Person>();
mapPerson2.put("4", new Person("pid4","pname4"));
mapPerson2.put("5", new Person("pid5","pname5"));
mapPerson2.put("6", new Person("pid6","pname6"));
listMap2.add(mapPerson2);
listListMap.add(listMap1);
listListMap.add(listMap2);
ActionContext.getContext().put("listListMap", listListMap);
return "listListMap";
}
页面迭代:
<body>
<table>
<tr>
<td>ID</td>
<td>PID</td>
<td>PNAME</td>
</tr>
<!--
迭代list
-->
<s:iterator value="#listListMap">
<!--
此时栈顶是第二层list<Map<String,Person>>
-->
<s:iterator>
<!--
此时栈顶是Map<String,Person>的entrySet,map中有几个键值对就有几个entrySet,所以要循环
-->
<s:iterator>
<tr>
<td>
<s:property value="key"/>
</td>
<td>
<s:property value="value.id"/>
</td>
<td>
<s:property value="value.name"/>
</td>
</tr>
</s:iterator>
</s:iterator>
</s:iterator>
</table>
</body>
3.<s:url>生成一个url地址,可以通过url标签制定的<s:param>子元素向URL地址发送请求参数
<!--action是action的名字,为url添加参数使用param标签-->
<s:url var="url" action="mainPage">
<s:param name="id" value="123"/>
</s:url>
<!--使用上面定义的url-->
<s:a href="%{url}">测试连接</s:a>
该方法在生成的页面源码中生成如下代码
<a href="/NetBookShop_081029/mainPage?id=123">测试连接</a>