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

结果是相应的内容显示不出来,原来是忘了这句很关键的引入:<%@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. SEO学习之路

    一.入门建站篇 1.Wamp集成环境安装 2.Wamp集成环境配置多站点 3.DedeCMS安装及目录结构 4.DedeCMS源码安装 5.DedeCMS官方手册 未完待续...

  2. Android下如何理解onMeasure,onLayout的过程

    在Android中view如何完成绘制这个过程介绍了很多,但是很多理论化的东西,最近重新整理一下,通俗的讲解一下. View绘制过程就好比你向银行贷款, 在执行onMeasure的时候,好比银行告诉你 ...

  3. Apache+Subversion+TortoiseSVN

    Key words: dav_svn, apache, subversion, tortoisesvn # install apache2 sudo apt-get install libapache ...

  4. [ES6] Rest Parameter

    Problem with the ES5: function displayTags(){ for (let i in arguments) { let tag = arguments[i]; _ad ...

  5. CoDel Test Script

    This TCL script is retrieved from http://www.pollere.net/CoDel.html in November 2013 :) # Codel test ...

  6. pwn学习之dl_resolve学习篇

    一:首先来了解一下linux下常见的攻击缓解机制: CANARY:(金丝雀值,指的是矿工曾利用金丝雀来确认是否有气体泄漏,如果金丝雀因为气体泄漏而中毒死亡,可以给矿工预警),类似于windows GS ...

  7. 【iOS开发之静态库、动态库】

    什么是库? 库 就是程序代码的集合, 是共享程序代码的一种方式,库一般分两类:开源库和闭源库.github中共享一般是开源库:闭源库分为:静态库和动态库,闭源库不开放源代码,是经过编译的二进制文件,一 ...

  8. 1:环境安装与介绍:canopy

    <利用python进行数据分析>这本书推荐用的的环境为EPDFree版本,但实际现在大概已经抛弃它改用Canopy了,下面将介绍Canopy相关: 一:下载:https://store.e ...

  9. jquery获取checkbox被选中的值

    只用一个循环,就可以找出被选中的checkbox的值 var s; $("[name = b]:checkbox").each(function () {              ...

  10. 新Android学习计划

    最近,在学习Android Design Support Library提供的新控件过程中,我感受到了原来的学习方式的缺点: 学习内容过于随意,在工作过程中碰到的新问题都想去掌握,心血来潮就想写一篇相 ...