Struts2标签(一)

Action:

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值