论 <%@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内 ...
随机推荐
- hdu 1253 胜利大逃亡 (三维简单bfs+剪枝)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 開始学习swift开发
近期要開始学习swift开发了,接下来的日子,会记录学习swift的历程.
- Java基础知识强化54:经典排序之插入排序(InsertSort)
1. 插入排序原理图: 算法步骤: 1)将第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列. 2)从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位 ...
- TreeGrid( 树形表格)
本节课重点了解 EasyUI 中 TreeGrid(树形表格)组件的使用方法,这个组件依赖于DataGrid(数据表格)组件 一. 加载方式//建立一个 JSON 文件[{"id" ...
- SpringMVC学习简单HelloWorld实例
首先还是从一个简单的Hello World项目说起: 我机器的开发环境为: Ubuntu12.04(不同操作系统对本系列项目没有影响): 开发工具:Eclipse For JavaEE: 数据库:My ...
- Java 测试Hibernate+Mysql简单的数据存储
想使用Hibernate框架,在网上看了一个Hibernate学习视频,试着做了一个小小的Java连接数据库的操作,Java初学者一个,大家多多包涵 开发环境: 1.安装MySql, 2.安装了Ecl ...
- 64位Windows 7平台安装32位Timesten,配置ODBC数据源
问题: 由于系统版本原因,客户机只能安装32位的Timesten,但客户机的平台是64位的win 7,安装完成后按照常规的控制面板->管理工具->数据源(ODBC)打开的ODBC数据源管理 ...
- Swift--集合类型 数组 字典 集合
数组 1.创建一个数组 var someInts = [Int]()空数组 someInts = []清空 var threeDoubles = Array(repeating: 0.0, count ...
- C#高级知识点概要(3) - 特性、自动属性、对象集合初始化器、扩展方法、Lambda表达式和Linq查询
1.特性(Attributes) 特性(Attributes),MSDN的定义是:公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法 ...
- [c#]控制台进度条的示例
看到[vb.net]控制台进度条的示例 感觉很好玩,翻译成C#版. using System; using System.Collections.Generic; using System.Linq; ...