论 <%@taglib prefix="s" uri="/struts-tags" %> 的重要性
前段时间在做项目的时候,碰到这个问题

结果是相应的内容显示不出来,原来是忘了这句很关键的引入:<%@taglib prefix="s" uri="/struts-tags" %>
1,Struts2只有一个标签库s,
引入它的方式为:<%@taglib prefix="s" uri="/struts-tags"%>
Struts2的标签不依赖于任何表现层技术,也就是说,Struts2提供的大部分标签,可以在各种表现层技术中使用,包括最常用的JSP页面,也可以在Velocity和FreeMarker等模块技术中使用。
2、Struts2的控制标签用法介绍
控制标签可以完成输出流程控制,例如分支,循环等操作,也可以完成对集合的合并,排序等操作。
1,if/elseif/else标签
只有if标签可以单独使用,其它两个要与if组合才能使用。
contentType="text/html; charset=GBK" language="java"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>s:if标签测试</title>
</head>
<body>
<s:set name="age" value=""/>
<s:if test="${age > 60}">
老年人
</s:if>
<s:elseif test="${age > 35}">
中年人
</s:elseif>
<s:elseif test="${age > 15}" id="wawa">
青年人
</s:elseif>
<s:else>
少年
</s:else>
</body>
</html>
2,iterator标签
iterator迭代List代码如下:
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>使用s:itertor标签迭代List</title>
</head>
<body>
<table border="" width="">
<s:iterator value="{'Spring2.0','J2EE','Ajax'}" id="name">
<tr>
<td><s:property value="#st.count"/><s:property value="name"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
还可以在迭代时根据迭代元素的属性来进行更多的控制。看如下代码:
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>使用s:itertor标签迭代List</title>
</head>
<body>
<table border="" width="">
<s:iterator value="{'Spring2.0','J2EE','Ajax'}" id="name" status="st">
<tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>
<td><s:property value="name"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
iterator迭代Map代码如下:
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>使用s:itertor标签迭代Map</title>
</head>
<body>
<table border="" width="">
<tr>
<th>书名</th>
<th>作者</th>
</tr>
<s:iterator value="#{'Spring2.0:'李' , 'J2EE':'李','Ajax':'李'}" id="score" 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>
append标签
append标签可以将多个集合对象拼接起来,组成一个新的集合。拼接后,从而允许通过一个iterator标签就可以完成对多个集合的迭代。
append对多个List对象的拼接
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>使用s:append标签拼接两个集合</title>
</head>
<body>
<s:append id="newList">
<s:param value="{'Spring2.0','J2EE','Ajax'}" />
<s:param value="{'培训', '职业教育'}" />
</s:append> <table border="" width="">
<s:iterator value="#newList" status="st">
<tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>
<td><s:property/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
append还可以拼接多个Map对象,还可以将Map和List混合拼接,见如下代码:
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>使用s:append标签拼接集合和Map</title>
</head>
<body>
<s:append id="newList">
<s:param value="#{'Spring2.0':'李','J2EE':'李','Ajax':'李'}" />
<s:param value="#{'培训', '职业教育'}" />
</s:append> <table border="" width="">
<s:iterator value="#newList" 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>
generator标签
使用generator标签可以将指定的字符串按指定分隔符隔成多个子串,临时生成的多个子串可以使用iterator标签来进行迭代输出。临时生成的集合将在此标签内部有效,出了标签就消亡。该标签有几个有用的属性,介绍如下:
id:这是一个可选属性,指定id后,则生成的标签在pageContext属性中
count:这是一个可选属性,该属性指定生成集合中元素的总数,多余的丢弃
separator:这是一个必填属性,指定用于解析的分隔符
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>使用s:generator生成集合</title>
</head>
<body>
<table border="" width="">
<s:generator val="'Spring2.0,J2EE,Ajax'" separator=",">
<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>
</body>
</html>
指定id和count后使用方式如下:
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>使用s:generator生成集合</title>
</head>
<body>
<s:generator val="'Spring2.0,J2EE,Ajax'"
separator="," id="books" count=""/>
<table border="" width="">
<%
java.util.Iterator i = (java.util.Iterator) pageContext.getAttribute("books");
while(i.hasNext())
{
String s = (String) i.next(); %>
<tr>
<td><%=s%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
merge标签
merge标签同样用于将多个集合元素拼接成一个集合元素。它的用法和功能同append很相似,只是生成的元素内容的顺序不同。
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>使用s:merge标签迭代Map</title>
</head>
<body>
<s:merge id="newList">
<s:param value="#{'Spring2.0':'李','J2EE':'李','Ajax':'李'}" />
<s:param value="#{'培训', '职业教育'}" />
</s:merge> <table border="" width="">
<s:iterator value="#newList" 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>
subset标签
subset标签用于取得集合的子集,该标签的底层通过org.apache.Struts2.util.Subset.IteratorFilter类提供实现。使用subset标签可以指定如下几个属性:
count:可选属性,指定子集中元素的个数,默认取得源集合的所有元素
source:可选属性,指定源集合,如果不指定,默认取得valueStack栈顶的集合,一般都会指定
start:可选属性,指定从源集合的第几个元素开始截取,,默认从第一个元素(即start=0)开始
decider:可选属性,由开发者自己决定是否选中该元素
一般用法如下:
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>使用s:subset标签截取集合元素</title>
</head>
<body>
<table border="" width="">
<s:subset source="{'Java','Spring2.0','J2EE','Ajax','WebWork'}"
start="" count="">
<s:iterator status="st">
<tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>
<td><s:property/></td>
</tr>
</s:iterator>
</s:subset>
</table>
</body>
</html>
上面的代码的source属性指定的集合包含了5个元素,通过subset从第2个元素开始截取,只取出其中3个元素。
此外,Struts2还允许开发者决定截取标准,开发者只需要实现一个Decider类,实现SubsetIteratorFilter.Decider接口中的boolean decide(Object element)方法,如果该方法返回真,则表明该元素将被选入子集中。看如下代码:
package lee; import org.apache.struts2.util.SubsetIteratorFilter;
public class MyDecider implements SubsetIteratorFilter.Decider
{
public boolean decide(Object element) throws Exception
{
String str = (String)element;
return str.indexOf("J2EE") > ;
}
}
这里要求过滤不包含“J2EE”的元素,JSP页面代码如下:
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>使用s:subset标签截取集合元素</title>
</head>
<body>
<s:bean id="mydecider" name="lee.MyDecider"/>
<table border="" width="">
<s:subset
source="{'Java','Spring2.0','J2EE','Ajax','WebWork'}"
decider="#mydecider">
<s:iterator status="st">
<tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>
<td><s:property/></td>
</tr>
</s:iterator>
</s:subset>
</table>
</body>
</html>
Sort标签
sort标签用于对指定的集合元素进行排序,进行排序时,必须提供自己的排序规则,即实现自己的Comparator,需要实现java.util.Comparator接口。使用sort标签时可指定如下几个属性:
comparator:必填属性,指定排序的Comparator实例
source:可选属性,指定被排序的集合,如果不指定则对valueStack栈顶的集合进行排序
JAVA代码如下:
package lee; import java.util.Comparator;
public class MyComparator implements Comparator
{
public int compare(Object element1, Object element2)
{
return element1.toString().length() - element2.toString().length();
}
}
上面的方法,如果返回一个大于0的数,则第一个元素大于第二个元素;返回0则表示两个元素相等,返回小于0的数,则第一个元素小于第二个元素。
JSP页面如下:
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>使用s:sort对集合元素进行排序</title>
</head>
<body>
<s:bean id="mycomparator" name="lee.MyComparator"/>
<table border="" width="">
<s:sort
source="{'J2EE','Ajax','Spring2.0'}"
comparator="#mycomparator">
<s:iterator status="st">
<tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>
<td><s:property/></td>
</tr>
</s:iterator>
</s:sort>
</table>
</body>
</html>
论 <%@taglib prefix="s" uri="/struts-tags" %> 的重要性的更多相关文章
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jst1/core"%>报错
查了一晚上 刚开始觉得最靠谱的还是这个说法: 1.下载jakarta-taglibs-standard-1.1.2.zip(在Weblogic中必须下载1.0版 http://jakarta.apa ...
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>会报错
有些时候,<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>会报错,错 ...
- 解决<%@taglib prefix="s" uri="/struts-tags"%>显示找不到
问题: jsp中使用<%@taglib prefix="s" uri="/struts-tags"%>显示找不到 解决方法: 在web.xml中插入 ...
- jsp中<%@ taglib prefix="s" uri="/struts-tags"%>标签意思
@taglib表明引用标签.类似java中的import语句prefix="s" 引用的名称在页面可以使用,就像java中生成的一个对象名,以后调用的时候直接使用<s:xxx ...
- The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter.
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the assoc ...
- struts tags
HTTP ERROR 500 Problem accessing /showognl.jsp. Reason: Server Error Caused by: org.apache.jasper.Ja ...
- This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has p
2014-09-16 15:47:51.590:WARN:oejs.ErrorPageErrorHandler:EXCEPTION org.apache.jasper.JasperException: ...
- The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. 异常
异常信息如下: The Struts dispatcher cannot be found. This is usually caused by using Struts tags without t ...
- 配置struts tags 输出HTML
<s:property escape="false" value="vaMsg"/> escape="false"则vaMsg内 ...
随机推荐
- C#。2.1 运算符
运算符: 一.算术运算符: + - * / % ——取余运算 取余运算的应用场景: 1.奇偶数的区分. 2.把数变化到某个范围之内.——彩票生成. 3.判断能否整除.——闰年.平年. int a = ...
- Js 插件修改及优化总结
1. ajaxfileupload 上传插件版本问题以及数据处理问题 参考链接: http://liwx2000.iteye.com/blog/1540321 现在大家至少也在用jquery1.9以上 ...
- 《第一行代码》学习笔记37-服务Service(4)
一个比较完整的自定义AsyncTask写成如下: class DownloadTask extends AsyncTask<Void, Integer, Boolean> { @Overr ...
- MS SQL到Oracle的数据迁移笔记
MS SQL到Oracle的数据迁移笔记 一.任务背景 旧系统使用MS SQL Server数据库,新系统使用Oracle数据库,现在需要将旧系统中的数据迁移到新系统中,旧数据按照约定的规则转换后,能 ...
- oracle数据库exp/imp命令详解
转自http://wenku.baidu.com/link?url=uD_egkkh7JtUYJaRV8YM6K8CLBT6gPJS4UlSy5WKhz46D9bnychTPdgJGd7y6UxYtB ...
- Swift--集合类型 数组 字典 集合
数组 1.创建一个数组 var someInts = [Int]()空数组 someInts = []清空 var threeDoubles = Array(repeating: 0.0, count ...
- map的使用方法
package cn.stat.p8.map.demo; import java.util.Collection; import java.util.HashMap; import java.util ...
- 读书笔记_Effective_C++_条款二十五: 考虑写出一个不抛出异常的swap函数
在之前的理论上调用对象的operator=是这样做的 void swap(A& x) { std::swap(a, x.a); } A& operator=(const A& ...
- 第1章 网络编程基础(2)——Socket编程原理
Socket编程原理 Socket是网络通信端点的一种抽象,它提供了一种发送和接收数据的机制. 流socket(SOCK_STREAM):双向.有序.无重复.并且无记录边界 数据报Socket(SOC ...
- POJ 3045 Cow Acrobats (贪心)
POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...