Action:
struts.xml:
index.jsp:
append.jsp:
generator.jsp:
merge.jsp
package com.struts2.tag.exer.web.actions;
import com.opensymphony.xwork2.ActionSupport;
public class TagAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1096330019952553956L;
public String append(){
return SUCCESS;
}
public String generator(){
return SUCCESS;
}
public String merge(){
return SUCCESS;
}
}
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<package name="tag" extends="struts-default" >
<action name="*" class="com.struts2.tag.exer.web.actions.TagAction" method="{1}">
<result name="success">/pages/{1}.jsp</result>
</action>
</package>
</struts>
index.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Tag Navigation Page</title>
</head>
<body>
<form action="#">
<table>
<s:generator separator="," val="'append,generator,merge'">
<s:iterator>
<tr><td><li><a href="<s:property/>.action"><s:property/></a></li></td></tr>
</s:iterator>
</s:generator>
</table>
</form>
</body>
</html>
append.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>append</title></head>
<body>
<!-- map append map -->
<s:append id="newMap">
<s:param value="#{'Oracle reference':'Jackey','Spring in Action':'Tommy','Hibernate':'John'}"/>
<s:param value="#{'Struts in Action':'Mike','JavaScrpit':'Marry'}"/>
</s:append>
<!-- map append list -->
<s:append id="newList">
<!-- map -->
<s:param value="#{'Oracle reference':'Jackey','Spring in Action':'Tommy','Hibernate':'John'}"/>
<!-- list -->
<s:param value="#{'AJAX in Action','MyBatis'}"/>
</s:append>
<table border="1" width="200">
<tr>
<td>Book Name</td>
<td>Author</td>
</tr>
<s:iterator value="#newMap" status="st">
<tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>
<td><s:property value="key"/></td>
<td><s:property value="value"/></td>
</tr>
</s:iterator>
</table>
<table border="1" width="200">
<tr>
<td>Book Name</td>
<td>Author</td>
</tr>
<s:iterator value="#newList" status="st">
<tr <s:if test="#st.odd">style="background-color:#cccccc"</s:if>>
<td><s:property value="key"/></td>
<td><s:property value="value"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
generator.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page import="java.util.*,com.opensymphony.xwork2.util.*" %>
<html>
<head><title>generator</title></head>
<body>
<table border="1" width="200">
<tr>
<td>Book Name</td>
<td>Author</td>
</tr>
<s:generator separator="," val="'Oracle reference,Spring in Action,Hibernate,MyBatis'">
<s:iterator status="st">
<tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>
<td><s:property/></td>
</tr>
</s:iterator>
</s:generator>
</table>
<%-- count属性限定了元素的数量 id属性将生成的集合放入了valueStack中--%>
<s:generator separator="," val="'Oracle reference,Spring in Action,Hibernate,MyBatis'" id="books" count="4"/>
<table border="1" width="200">
<tr>
<td>Book Name</td>
<td>Author</td>
</tr>
<%--
ValueStack vs = (ValueStack)request.getAttribute("struts.valueStack");
java.util.Iterator<String> iterator= (java.util.Iterator<String>)vs.findValue("books");
if(iterator!=null){
while(iterator.hasNext()){
String book = (String)iterator.next();
--%>
<!--
<tr>
<td>
-->
<%--=book--%>
<!--
</td>
</tr>
-->
<%--
}
}
--%>
<s:iterator value="#books">
<tr>
<td><s:property/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
merge.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>merge</title></head>
<body>
<!-- map merge map -->
<s:merge id="mergeMap">
<s:param value="#{'Oracle reference':'Jackey','Spring in Action':'Tommy','Hibernate':'John'}"/>
<s:param value="#{'Struts in Action':'Mike','JavaScrpit':'Marry'}"/>
</s:merge>
<!-- map append map -->
<s:append id="appendMap">
<s:param value="#{'Oracle reference':'Jackey','Spring in Action':'Tommy','Hibernate':'John'}"/>
<s:param value="#{'Struts in Action':'Mike','JavaScrpit':'Marry'}"/>
</s:append>
<table border="1" width="200">
<tr>
<td colspan="2">Merge</td>
</tr>
<tr>
<td>Book Name</td>
<td>Author</td>
</tr>
<s:iterator value="#mergeMap" status="st">
<tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>
<td><s:property value="key"/></td>
<td><s:property value="value"/></td>
</tr>
</s:iterator>
</table>
<table border="1" width="200">
<tr>
<td colspan="2">Append</td>
</tr>
<tr>
<td>Book Name</td>
<td>Author</td>
</tr>
<s:iterator value="#appendMap" status="st">
<tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>
<td><s:property value="key"/></td>
<td><s:property value="value"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>