论 <%@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内 ...
随机推荐
- [转载]SQL Server查找包含某关键字的存储过程3种方法
存储过程都写在一个指定的表中了,我们只要使用like查询就可以实现查询当前这台SQL Server中所有存储过程中包括了指定关键字的存储过程并显示出来,下面一起来看看我总结了几条命令. 例子1 代码如 ...
- lesson3.1:java公平锁和非公平锁及读写锁
关于这四种锁的各自情况,网上有很多文章做了介绍,本不想单独开章节介绍,本章只介绍这四种锁的一些源码特点及注意事项. demo 源码:https://github.com/mantuliu/javaAd ...
- String 和 InputStream 互转方式
/** * 利用BufferedReader实现Inputstream转换成String <功能详细描述> * * @param in * @return String */ public ...
- rpm软件包类型
rpm软件包 在linux世界里有两种流行的包管理方式,分别是redhat系的rpm和debian系的deb.其中rpm是RedHat Package Manager(RedHat软件包管理工具)的简 ...
- js 全国城市3级联动
js /* * 全国三级城市联动 js版 */ function Dsy(){ this.Items = {}; } Dsy.prototype.add = function(id,iArray){ ...
- 第一个Delphi小程序
第一次应工作需呀,接触这个语言,今晚在自己的电脑搭建好环境,写的第一个超简单的Delphi小程序! var temp:Integer; //求个位数 procedure TForm1.BitBtn1C ...
- self和super之间的区别
关于 self 和 super 之间的区别, 首先要了解 1, self 是什么 :super 是什么.2,[ super init] 做什么.3,为什么要 self = [super init ...
- nodejs+chromium 创建桌面应用程序
直接用nodejs+javascript+html+css也可以创建桌面应用程序啦,前端开发的同学应该都比较感兴趣. 生成的应用程序自带nodejs环境和chrome浏览器环境. github的ato ...
- rdf
更多内容请看这里:http://www.w3school.com.cn/rdf/index.asp 资源描述框架 (RDF) 是描述网络中资源的 W3C 标准. RDF 是一个框架,用来描述网络资源, ...
- Linux中yum手动安装、手动建立仓库文件夹关联实现关联包自动安装、yum相关命令使用
yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指 ...