Struts Logic标签库

Struts Logic标签库中的标签可以根据特定 逻辑条件 来控制输出网页内容,或者循环遍历集合中的所有元素,大致分为:
  • *进行比较运算的Logic标签
  • *进行字符串匹配的Logic标签
  • *判断指定内容是否存在的Logic标签
  • *进行循环遍历的Logic标签
  • *进行请求转发或重定向的Logic标签

*进行比较运算的Logic标签

  • <logic:equal>:比较变量是否等于指定的常量。
  • <logic:notEqual>;比较变量是否不等于指定的常量
  • <logic:greaterEqual>;比较变量是否大于或等于指定的常量
  • <logic:greaterThan>;比较变量是否大于指定的常量
  • <logic:lessEqual>;比较变量是否小于或等于指定常量
  • <logci:lessThan>;比较变量是否小于指定常量

所有的比较运算标签都比较一个变量和指定常量的大小,比较运算标签的value属性指定常量值,可以通过以下方式来设置变量:

  • 设置cookie属性,此时变量为cookie属性指定的Cookie值,例如:

       <%
         Cookie c = new Cookie("username", "Linda");
         c.setComment("A test cookie");
         c.setMaxAge(3600); //60 seconds times 60 minutes
         response.addCookie(c);
        %>

        <logic:equal cookie="username" value="Linda" >
                UserName in Cookie is Linda <p>
        </logic:equal>

  • 设置header属性,此时变量为header属性指定的HTTP请求中的Header信息,例如:
  • <logic:equal header="Accept-Language" value="zh-cn" >
            Client’s language is: zh-cn. <p>
    </logic:equal>
  • 设置parameter属性,此时变量为parameter属性指定的请求参数值,例如:
  • <logic:greaterThan parameter="arg1" value="100" >
            The first request parameter is greater than 100 <p>
    </logic:greaterThan >
  • 例如请求访问该jsp的URL为:http://localhost:8080/***.jsp>arg1=200就会输出标签主体的文本。
  • 设置name属性,此时name属性指定被比较的变量,比较运算标签调用变量的toString()方法,获得被比较的字符串值,例如;
  • <% 
            request.setAttribute("intBean",new Integer(100));
    %>
    <logic:equal name="intBean" value="100" >
            The value of intBean is "100".<p> 
    </logic:equal >
  • 在默认情况下,将依次在page,request,session和application范围内寻找name属性指定的变量,此外,也可以通过scope属性来指定变量的存在范围。
  • 同时设置name和property属性,此时name属性指定已经存在的JavaBean,property指定JavaBean的属性,被比较的变量为这个属性的值,例如:
  • <% 
            SomeBean bean=new SomeBean();
            bean.setName("Linda");
            request.setAttribute("someBean",bean);
    %>
    <logic:notEqual name="someBean" property="name" value="Tom" >
            The name of someBean is not "Tom" <p>
    </logic:notEqual >
  • 如果两个字符串都可以成功转化为数字,就比较数字的大小,否则就进行字符串比较,例如;
  • <% request.setAttribute("number","100"); %>
    <logic:equal name="number" value="/100.0" >
            "100" equals "100.0" <p>
    </logic:equal >
  • <logic:lessThan name="number" value="/100.0a" >
            "100" is less than "100.0a" <p>
    </logic:lessThan >

<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


进行循环遍历的Logic标签
<logic:iterate>是Logic标签库中最复杂的标签,它能够在一个循环中遍历数组,Collection,

Enumeration,Iterator或Map中的所有元素。
 1、遍历集合
    <logic:iterate>的name属性指定需要进行遍历的集合对象,它每次从集合中检索出一个元素,然后

把它存放在page范围内,并以id属性指定的字符串来命名这个元素,例如:
 <%
 Vector animals=new Vector();
 animals.addElement("Dog");
 animals.addElement("Cat");
 animals.addElement("Bird");
 animals.addElement("Chick");
 request.setAttribute("Animals", animals);
 %>
<logic:iterate id="element" name="Animals">
   <bean:write name="element"/><BR>
</logic:iterate><p>
以上代码的输出内容为:
      Dog 
      Cat
      Bird
      Chick
length属性指定需要遍历的元素的数目,如果没有设置length属性,就遍历集合中的所有元素,offset属

性指定开始遍历的起始位置,默认值“0”,表示从一个集合的第一个元素开始遍历,indexId属性定义一

个代表当前被遍历元素序号的变量,这个变量被存放在page范围内,可以被标签主体的<bean:write>标签

访问,例如:
  <logic:iterate id="element" indexId="index" name="Animals" offset="1" length="2">
   <bean:write name="index"/>.<bean:write name="element"/><BR>
  </logic:iterate><p>
  以上标签的length属性为2,表示只需要遍历Animals集合中的两个元素,offset为1,表示从第二个元

素开始遍历,输出内容为:
       1、Cat
       2、Bird
2、遍历Map
   <logic:iterate>标签还可以遍历HashMap中的元素,例如:
   <%
HashMap months = new HashMap();
months.put("Jan.", "January");
months.put("Feb.", "February");
months.put("Mar.", "March");
request.setAttribute("months", months);
%>
<%--
The following section shows iterate.
--%>
<logic:iterate id="element" indexId="ind" name="months">
  <bean:write name="ind"/>. 
  <bean:write name="element" property="key"/>:
  <bean:write name="element" property="value"/><BR>
</logic:iterate><P>
  标签遍历months对象的每一个元素,每一个元素都包含一对key/value,以上代码输出:
   0、Mar:March
   1、Feb:Feberuary
   2、Jan:January
如果HashMap中的每个元素的value是集合对象,则可以采用嵌套的<logic:iterate>标签遍历集合中的所

有对象,例如:
 <%
HashMap h = new HashMap();
String vegetables[] = {"pepper", "cucumber"};
String fruits[] = {"apple","orange","banana","cherry","watermelon"};
String flowers[] = {"chrysanthemum","rose"};
String trees[]={"willow"};
h.put("Vegetables", vegetables);
h.put("Fruits", fruits);
h.put("Flowers", flowers);
h.put("Trees",trees);
request.setAttribute("catalog", h);
%>
<%--
The following section shows iterate.
--%>

<logic:iterate id="element" indexId="ind" name="catalog">
  <bean:write name="ind"/>. <bean:write name="element" property="key"/><BR>
  <logic:iterate id="elementValue" name="element" property="value" length="3" offset="1">
      -----<bean:write name="elementValue"/><BR>
  </logic:iterate>
</logic:iterate><P>
以上代码下定义了一个名为'catalog'的HashMap,存放在request范围,它的每个元素的value为字符串数

组,接下来外层的标签遍历HashMap中的所有元素,内层的标签访问每个元素的value属性,遍历value属

性引用的字符串数组中的所有元素,输出内容为:
    0. Vegetables
    ----cucumber
    1、Flowers
    ----rose
    2.Fruits
    ----orange
    ----banana
    ----cherry
    3.Trees
3、设置被遍历的变量
可以通过以下方式来设置需要遍历的变量。
a、设置name属性,name属性指定需要遍历的集合或Map,例如:
  <logic:iterate id="element" name="Animals">
   <bean:write name="element"/><BR>
</logic:iterate><p>
b、设置name属性和property属性,name属性指定一个JavaBean,property属性指定JavaBean的一个属性

,这个属性为需要遍历的集合或Map,例如:
<logic:iterate id="element" indexId="ind" name="catalog">
  <bean:write name="ind"/>. <bean:write name="element" property="key"/><BR>
  <logic:iterate id="elementValue" name="element" property="value" length="3" offset="1">
      -----<bean:write name="elementValue"/><BR>
  </logic:iterate>
</logic:iterate><P>
c、设置collection属性,collection属性指定一个运行时表达式,表达式的运算结果为需要遍历的集合

或Map,例如:
  <logic:iterate id="header" collection="<%= request.getHeaderNames() %>">
   <bean:write name="header"/><BR>
</logic:iterate>
 

在logic:iterate嵌入html:link标签:

<logic:iterate id="display" name="result">

     <html:link paramId="id" paramProperty="wzxw" paramName="display">

          <bean:write name="display" property="wzxw" />

     </html:link>

</logic:iterate> 

paramId="id"  是指在 url 中的id http://localhost/test.jsp?id=xxx ,如果 paramId="name",那么 url 中应该是http://localhost/test.jsp?name=xxx

paramProperty="wzxw" 是你在 result 中的属性;

paramName="display" 是 logic:iterate 的 id

进行请求转发或重定向的Logic标签
1、<logic:forward>标签

该标签用于请求转发,它的name属性指定转发目标,於Struts配置文件中的<global-forwards>元素中的

子元素<forward>匹配,例如,在配置文件中:

      <global-forwards>
      <forward   name="index"   path="/index.jsp"/>
      </global-forwards>
这样jsp中通过<logic:forward>标签把请求转发给name属性为“index”的全局转发目标:
<logic:forward name ="index"/>
这样当浏览器访问标签所在jsp时,标签把请求转发给index.jsp,因此浏览器看到的是index.jsp

2、<logic:redirect>标签
该标签用于重定向,它的forward,href和page属性指定重定向目标,这几个属性的用法和<html:link>的

forward,href,page属性类似,例如:
  <logic:redirect href="http://www.apache.org"/>

Reference URL:http://blog.csdn.net/kucool/article/details/1563286
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSDN IT狂飙上传的代码均可运行,功能ok的情况下才上传的,直接替换数据即可使用,小白也能轻松上手 【资源说明】 基于MATLAB实现的有限差分法实验报告用MATLAB中的有限差分法计算槽内电位;对比解析法和数值法的异同点;选取一点,绘制收敛曲线;总的三维电位图+使用说明文档 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2020b;若运行有误,根据提示GPT修改;若不会,私信博主(问题描述要详细); 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可后台私信博主; 4.1 期刊或参考文献复现 4.2 Matlab程序定制 4.3 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 5、欢迎下载,沟通交流,互相学习,共同进步!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值