Struts Logic标签库(二)

<logic:match>判断变量中是否包含指定的常量字符串
  • <logic:notMatch>判断变量中是否不包含指定的常量字符串
  • 字符串匹配标签的value属性指定常量值,可以通过cookie,header,parameter,name和property属性来设置变量,用法和比较运算标签相应属性的用法类似。例如

    <%
      request.setAttribute("authorName", "LindaSun");
    %>

    <logic:match name="authorName" scope="request" value="Linda">
       <bean:write name="authorName"/> has the string 'Sun' in it.
    </logic:match>
    <logic:notMatch name="authorName" scope="request" value="Linda">
       <bean:write name="authorName"/> doesn't have the string 'Linda' in it.
    </logic:notMatch>

    字符串匹配标签的location属性指定子字符串的位置,可选值包括

    • start:子字符串位于母字符串的起始位置
    • end:子字符串位于母字符串的结尾

    例如,以下<logic:notMatch>标签判断在authorName变量的起始位置是否不包含"Linda"子字符串,如果比较结果为true,就执行标签主体的内容,此次为false,

    <logic:notMatch name="authorName" scope="request" value="Linda" location="start">
       <bean:write name="authorName"/> doesn't start with the string 'Linda'.
    </logic:notMatch>

    如果没有指定location属性,子字符串可以位于母字符串的任何位置。

     

    判断指定内容是否存在的Logic标签

    • <logic:empty>判断指定的变量是否为null,或者为空字符串“”
    • <logic:notEmpty>判断指定的变量是否不为null,或者不为空字符串“”
    • <logic:present>判断指定的安全角色,用户,Cookie,HTTP请求Header或JavaBean是否存在。
    • <logic:notPresent>判断指定的安全角色,用户,Cookie,HTTP请求Header或JavaBean是否不存在。
    • <logic:messagesPresent>;判断指定的消息是否存在
    • <logic:messagesNotPresent>;判断指定的消息是否不存在

    1、<logic:empty>和<logic:notEmpty>标签

    这一对标签判断指定的变量是否为空字符串“”,可以设置name属性,或同时设置name属性和property属性,来指定变量,例如:

    <%
    request.setAttribute("emptyString", "");
    %>

    <logic:empty name="emptyString">
       The variable named emptyString is empty!<P>
    </logic:empty>
    <logic:notEmpty name="emptyString">
        The variable named emptyString is not empty!<P>
    </logic:notEmpty>

    2、<logci:present>和<logic:notPresent>标签

    这一对标签判断指定的对象是否存在,有以下属性,分别用于判断某种类型的对象是否存在:

    • cookie属性,判断指定的cookie是否存在
    • header属性,判断指定的HTTP请求Header是否存在
    • role属性,判断当前通过权限验证的用户是否具有指定的安全角色,多个安全角色之间用逗号分开,例如:<logic:present role="role1,role2">...</logic:present>
    • user属性,判断当前通过权限验证的用户是否拥有指定的用户名
    • parameter属性,判断指定的请求参数是否存在
    • name属性,判断指定的JavaBean是否存在
    • 同时指定name和property属性,name属性指定JavaBean,property属性指定JavaBean的某个属性,判断这个属性是否存在并且是否为null

    例如:

    • 设置name属性,
    • <%
    • request.setAttribute("emptyString", "");
    • %>
    • <logic:present name="emptyString" >
         There is a JavaBean named "emptyString". <p>
      </logic:present>
    • 同时设置name属性和property属性,例如
    • <logic:notPresent name="emptyString" property="noSuchProperty">
         EmptyString doesn't have such a property named "noSuchProperty".
      </logic:notPresent><logic:notPresent name="noSuchBean" property="noSuchProperty">
         Either noSuchBean or noSuchProperty does not exist!
      </logic:notPresent>
    • 设置header属性,例如
    • <logic:present header="user-agent">
         Yep, we got a user-agent header.
      </logic:present>
      <logic:notPresent header="user-agent">
         No, user-agent header does not exist.
      </logic:notPresent>

    3、<logic:messagesPresent>和<logic:messagesNotPresent>标签

    这一对标签用来判断是否在request范围内存在指定的ActionMessages或其子类ActionErrors对象,以及在ActionMessages对象中是否存在特定的消息。
       例如:首先在JSP中定义一个ActionErrors对象,它包含一条消息,然后把它分别以Globals.ERROR_KEY和“myerrors”做为属性key,保存在request范围
    <%
    ActionErrors errors = new ActionErrors();
    errors.add("totallylost", new ActionMessage("application.totally.lost"));
    request.setAttribute(Globals.ERROR_KEY, errors);
    request.setAttribute("myerrors", errors);
    %>
    下面分几种情况介绍这两个标签的用法:
      a、未设置name,message和property属性,例如:
      <logic:messagesPresent>
       Yes, there are errors.
      </logic:messagesPresent><P>
      默认情况下,标签从request范围内检索属性key为Globals.ERROR_KEY的ActionMessage对象,其判断结果为true
       b、设置name属性,例如:
       <logic:messagesPresent name="myerrors">
       Yes, there are errors in myerrors collection.
       </logic:messagesPresent><P>
       标签将从request范围内检索key为“myerrors”的ActionMessages对象,其判断结果为true
       c、设置message属性,例如:
       <logic:messagesNotPresent message="true" >
      There are no normal messages.
       </logic:messagesNotPresent><P>
       标签从request范围内检索属性key为Globals.MESSAGE_KEY的ActionMessages对象,由于不存在这种的
       ActionMessages对象,判断结果为true
       d、设置property属性,单它指定的消息key不存在,例如:
       <logic:messagesNotPresent property="noSuchError">
       There is no error named "SuchError".
       </logic:messagesNotPresent><P>
       标签从request范围内检索属性key为Globals.ERROR_KEY的ActionMessages对象,然后再从该对象中检   索消息key为“noSuchError”的消息,由于不存在这样的消息,其判断结果为true
       e、设置property属性,并且它的指定消息key存在,例如:
       <logic:messagesPresent property="totallylost">
       There is an error named "totallylost".
       </logic:messagesPresent>
       标签从request范围内检索属性key为Globals.ERROR_KEY的ActionMessages对象,然后再从ActionMessages对像中检索key为“totallylost”的消息,判断结果为true
     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值