struts技术的logic标签

                         

    开源项目最好的是可以让我们从项目的源码本身角度看项目,通过对源码的了解更多的是对设计思想融会贯通达提升整体能力的目的。
 
一、定义标签文件(web.xml)
<taglib>
  <taglib-uri>/tags/struts-logic</taglib-uri>
  <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

二、引用标签文件(jsp文件)
<%@ taglib uri="/tags/struts-logic"prefix="logic"   %>
 
三、标签文件说明(struts-logic.tld)
(1)empty标签
类名:org.apache.struts.taglib.logic.EmptyTag
标签体:bodycontent=JSP
引用logic:empty
属性   attribute:name,property,scope
功能:判断对象的值是否为空
 
(2)equal
类名:org.apache.struts.taglib.logic.EqualTag
标签体:bodycontent=JSP
引用logic:equal
属性  attribute:cookie,header,name,parameter,property,scope,value
功能:等于比较符
 
(3) forward
org.apache.struts.taglib.logic.ForwardTag   
标签体:bodycontent=empty
引用logic:forward
属性   attribute:name
功能:页面导向,查找配置文件的全局forward
 
(4) greaterEqual
类名:org.apache.struts.taglib.logic.GreaterEqualTag
标签体:bodycontent=JSP
引用logic:greaterEqual
属性  attribute:cookie,header,name,parameter,property,scope,value
功能:大于等于比较符
 
(5)greaterThan
类名:org.apache.struts.taglib.logic.GreaterThanTag
标签体:bodycontent=JSP
引用logic:greaterThan
属性  attribute:cookie,header,name,parameter,property,scope,value
功能:大于比较符
 
(6) iterator
类名:org.apache.struts.taglib.logic.IterateTag
标签体:bodycontent=JSP
引用logic:iterator
属性  attribute:collection,id,indexId,length,name,offset,property,scope,type
功能:显示列表为collection的值(List ,ArrayList,HashMap等)
 
(7)lessEqual
类名org.apache.struts.taglib.logic.LessEqualTag
标签体:bodycontent=JSP
logic:lessEqual
属性  attribute:cookie,header,name,parameter,property,scope,value
功能:小于等于比较符
 
(8)lessThan
类名:org.apache.struts.taglib.logic.LessThanTag
标签体:bodycontent=JSP
logic:lessThan
属性  attribute:cookie,header,name,parameter,property,scope,value
功能:小于比较符
 
(9)match
类名:org.apache.struts.taglib.logic.MatchTag
标签体:bodycontent=JSP
引用logic:match
属性  attribute:cookie,header,location,name,parameter,property,scope,value
功能:比较对象
 
(10)messagesNotPresent
类名:org.apache.struts.taglib.logic.MessagesNotPresentTag
标签:bodycontent=JSP
引用logic:messagesNotPresent
属性   attribute:name,property,message
功能:ActionMessages/ActionErrors对象是否不存在
 
(11)messagePresent
类名:org.apache.struts.taglib.logic.MessagesPresentTag
标签:bodycontent=JSP
引用logic:messagePresent
属性   attribute:name,property,message
功能:ActionMessages/ActionErrors对象是否不存在
 
(12)notEmpty
类名:org.apache.struts.taglib.logic.NotEmptyTag
标签:bodycontent=JSP
引用logic:notEmpty
属性   attribute:name,property,scope
功能:比较对象是否不为空

(13)notEqual
类名:org.apache.struts.taglib.logic.NotEqualTag
标签:bodycontent=JSP
引用logic:notEqual
属性  attribute:cookie,header,name,parameter,property,scope,value
 
(14)notMatch
类名:org.apache.struts.taglib.logic.NotMatchTag
标签:bodycontent=JSP
引用logic:notMatch
属性  attribute:cookie,header,location,name,parameter,property,scope,value
功能:比较对象是否不相等
 
(15)notPresent
类名:org.apache.struts.taglib.logic.NotPresentTag
标签:bodycontent=JSP
引用logic:notPresent
属性attribute:cookie,header,name,parameter,property,role,scope,user
功能:request对象传递参数是否不存在
 
(16)present
类名:org.apache.struts.taglib.logic.PresentTag
标签:bodycontent=JSP
引用logic:present
属性  attribute:cookie,header,name,parameter,property,role,scope,user
功能:request对象传递参数是否存在
 
(17)redirect
类名:org.apache.struts.taglib.logic.RedirectTag
标签:bodycontent=JSP
引用logic:redirect
属性  attribute:anchor,forward,href,name,page,paramId,paramName,paramProperty,paramScope,property,scope,transaction
功能;页面转向,可传递参数
 

(一)Struts Logic比较标签

1.<logic:equal>
判断变量是否与指定的常量相等。例如:
    <%
        pageContext.setAttribute("test",newInteger(1000));%>
    <logic:equal value="1000"name="test">
        test=1000
    </logic:equal>

2.<logic:greaterThan>
判断常量变量是否与指定的常量不相等。
<html:linkpage="/greaterThan.jsp?test=123456">添加参数</html:link>
//传值<logic:greaterThan value="12347"parameter="test">
      true
</logic:greaterThan>下面类似

3.<logic:greaterEqual>
判断变量大小是否等于指定的常量。

4.<logic:lessThan>
判断变量是否小于指定的常量。

5.<logic:lessEqual>

判断变量是否小于等于指定的常量。

(二)Struts Logic循环标签

    循环遍历标签<logic:iterate>该标签用于在页面中创建一个循环,以次来遍历数组、Collection、Map这样的对象。在Struts中经常用到!
例如:
<%
    String[]testArray={"str0","str1","str2","str3","str4","str5"};
    pageContext.setAttribute("test",testArray);
%>
    <logic:iterate id="array"name="test">
        <bean:writename="array"/>
    </logic:iterate>
首先定义了一个字符串数组,并为其初始化。接着,将该数组存入pageContext对象中,命名为test。然后使用logic:iterate标记的name属性指定了该数组,并使

用id来引用它,同时使用bean;write标记来将其显示出来。还可以通过length属性指定输出元素的个数,offset属性指定从第几个元素开始输出。例如:
    <logic:iterate id="array1" name="test" length="3"offset="2">
        <bean:writename="array1"/><br>
    </logic:iterate>

  (3)Struts Logic 匹配标签

<logic:match>
<logic:notmatch>
该标签用于判断变量中是否或者是否不包含指定的常量字符串。例如:
    <%
      pageContext.setAttribute("test","helloWord");
    %>
    <logic:match value="hello"name="test">
        hello 在helloWord中
    </logic:match>
Match标记还有一个重要属性,就是location属性。location属性所能取的值只有两个,一个是"start",另一个是"end"。
例如:
    <logic:match value="hello" name="test"location="start">
        helloWord以 hello开头
    </logic:match>

(4)Struts Logic存在标签

<logic:present>
<logic:notpresent>
<logic:messagePresent>
<logic:messageNotPresent>
 
<logic:present>和<logic:notpresent>标签主要用于判断所指定的对象是否存在;
例如:
    <%
        pageContext.setAttribute("test","testString");
    %>
    <logic:present name="test">
        true 
    </logic:present>
<logic:present>和<logic:notpresent>标记有以下几个常用属性:
header属性:   判断是否存在header属性所指定的header信息。
parameter属性:判断是否存在parameter属性指定的请求参数。
cookie属性:   判断cookie属性所指定的同名cookie对象是否存在。
name属性:     判断name属性所指定的变量是否存在。
property属性:和name属性同时使用,当name属性所指定的变量是一个JavaBean时,判断property属性所指定的对象属性是否存在。
    <%
        Cookie cookie=new Cookie("name","value");
        response.addCookie(cookie);
    %>
    <logic:present cookie="name">
        true
    </logic:present>

<logic:messagePresent>和<logic:messageNotPresent>这两个标记是来判断是否在request内存在特定的ActionMessages或ActionErrors对象。
它们有几个常用的属性:
name属性:   指定了ActionMessages在request对象内存储时的key值。
message属性:message属性有两种取值。当其为true时,表示使用Globals.MESSAGE_KEY做为从request对象中获取ActionMessages的key值,此时无论name指定什么

都无效;当其为false时,则表示需要根据name属性所指定的值做为从request对象中获取ActionMessages的key
值,倘若此时未设置name属性的值,则使用默认的Globals.ERROR_KEY。
property属性:指定ActionMessages对象中某条特定消息的key值。
例如:
    <%
        ActionErrors errors = new ActionErrors();
        errors.add("totallylost", newActionMessage("application.totally.lost"));
        request.setAttribute(Globals.ERROR_KEY,errors);
        request.setAttribute("myerrors", errors);
    %>
        <logic:messagesPresentname="myerrors">
          Yes
        </logic:messagesPresent>

(5)Struts Logic判空标签

<logic:empty>
<logic:notEmpty>
该标签用于判断指定的字符是否为空。
例如:
    <%
        pageContext.setAttribute("test","");
    %>
 
    <logic:emptyname="test">
        test 为空
    </logic:empty>

(6)Struts Logic转发和重定向标签

1.<logic:foward>转发标签
该标签用于进行全局转发,使用该标签的页面一般不再编写其他内容,因为随着转发,页面将跳转,原页面中的内容也没有意义了。
例如:
this is a test<logic:forwardname="welcome"/>
this is a new test

2.<logic:redirect>重定向标签
该标签用于进行重定向。具有属性:href属性:   将页面重定向到href指定的完整外部链接。
page属性:   该属性指定一个本应用内的一个网页,标记将页面重定向到这个新的网页。
forward属性:该属性与struts-config.xml中的<global-forward>内的子项相对应。即将页面重定向到forward所指定的资源。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值