前段时间在做项目的时候,碰到这个问题

结果是相应的内容显示不出来,原来是忘了这句很关键的引入:<%@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" %> 的重要性的更多相关文章

  1. <%@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 ...

  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>会报错

    有些时候,<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>会报错,错 ...

  3. 解决<%@taglib prefix="s" uri="/struts-tags"%>显示找不到

    问题: jsp中使用<%@taglib prefix="s" uri="/struts-tags"%>显示找不到 解决方法: 在web.xml中插入 ...

  4. jsp中<%@ taglib prefix="s" uri="/struts-tags"%>标签意思

    @taglib表明引用标签.类似java中的import语句prefix="s" 引用的名称在页面可以使用,就像java中生成的一个对象名,以后调用的时候直接使用<s:xxx ...

  5. 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 ...

  6. struts tags

    HTTP ERROR 500 Problem accessing /showognl.jsp. Reason: Server Error Caused by: org.apache.jasper.Ja ...

  7. 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: ...

  8. 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 ...

  9. 配置struts tags 输出HTML

    <s:property escape="false" value="vaMsg"/> escape="false"则vaMsg内 ...

随机推荐

  1. 【C#正则基础】正则表达式

    1. 代表任意多个字符:(.*?)2. 代表网页里的<body>*</body>任意的标签内容,替换以后网页源码就只剩纯文本:<[^>]*>3. 代表网页中的 ...

  2. ACtivity实现欢迎界面并添加动画切换效果

    先看效果: 中间切换动画没来得及截图,凑合着看吧. 主要是java代码的实现: Welcom.java package kehr.activity.welcome; import android.ap ...

  3. 【浅墨Unity3D Shader编程】之二 雪山飞狐篇:Unity的基本Shader框架写法&amp;颜色、光照与材质

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40955607 作者:毛星云(浅墨)  ...

  4. openwrt time sycronize

    三行命令搞定这个. opkg update opkg install ntpclient ntpclient -s -c 0 -h ntp.sjtu.edu.cn 最后把这个 放到 rc.local ...

  5. mysql常用操作 mysql备份与恢复

    先登录mysql  ==>mysql -uroot -p  查看数据库的版本 select version(); 查看有哪些库 show datases; 查看当前处于哪个库 select da ...

  6. jquery之杂记

    //选中事件,放在初始化方法里面,toolbar下面 onSelect : function(rowIndex, rowData) { queryChannelFloor(rowIndex, rowD ...

  7. [转]iOS设备唯一标识探讨

    转自:http://www.jianshu.com/p/b83b0240bd0e iOS设备唯一标识探讨 为了统计和检测应用的使用数据,几乎每家公司都有获取唯一标识的业务需求,在iOS5以前获取唯一标 ...

  8. Android-操作栏之图标导航

    想实现图标向上导航功能,步子分两步走: 1.样式上要改变-->图标要变成可点击的按钮,并有一个向左的箭头. 2.功能上要实现-->实现向上导航 首先谈第一步: 对于拥有fragment的a ...

  9. iOS_SN_CocoaPods使用详细说明( 转)

    一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...

  10. iOS_SN_百度地图基本使用(1)

    上次用了一次百度地图,一直没有记笔记,今天记一笔. 以前没有用过百度地图的时候,听做这方面的朋友说百度地图有不少的坑,但是我做的时候没有遇到太大的坑,主要是要注意官方文档的注意事项,还有配置环境开发中 ...