Struts Logic标签库及常见用法

(一)Logic 比较标签

 1.<logic:equal>

判断变量是否与指定的常量相等。例如:

   <%

     pageContext.setAttribute("test",new Integer(1000));

   %>

   <logic:equal value="1000" name="test">

       test=1000

   </logic:equal>

2.<logic:greaterThan>

判断常量变量是否与指定的常量不相等。

<html:link page="/greaterThan.jsp?test=123456">添加参数</html:link> //传值

<logic:greaterThan value="12347" parameter="test">

     true

</logic:greaterThan>

下面类似

3.<logic:greaterEqual>

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

4.<logic:lessThan>

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

5.<logic:lessEqual>

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

()循环遍历标签<logic:iterate>

该标签用于在页面中创建一个循环,以次来遍历数组、CollectionMap这样的对象。在Struts中经常用到!

例如:

 <%

 String []testArray={"str0","str1","str2","str3","str4","str5"};

 pageContext.setAttribute("test",testArray);

 %>

 <logic:iterate id="array" name="test">

      <bean:write name="array"/>

 </logic:iterate>

首先定义了一个字符串数组,并为其初始化。接着,将该数组存入pageContext对象中,命名为test。然后使用logic:iterate标记的name属性指定了该数组,并使用id来引用它,同时使用bean;write标记来将其显示出来。

还可以通过length属性指定输出元素的个数,offset属性指定从第几个元素开始输出。例如:

<logic:iterate id="array1" name="test1" length="3" offset="2">

 <bean:write name="array1"/><br>

</logic:iterate>

()匹配标签<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>

()存在标签

<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内存在特定的ActionMessagesActionErrors对象。它们有几个常用的属性:

name属性:   指定了ActionMessagesrequest对象内存储时的key值。

message属性:message属性有两种取值。当其为true时,表示使用Globals.MESSAGE_KEY做为从request对象中获取ActionMessageskey值,此时无论name指定什么都无效;当其为false时,则表示需要根据name属性所指定的值做为从request对象中获取ActionMessageskey

值,倘若此时未设置name属性的值,则使用默认的Globals.ERROR_KEY

property属性:指定ActionMessages对象中某条特定消息的key值。

例如:

    <%

       ActionErrors errors = new ActionErrors();

       errors.add("totallylost", new ActionMessage("application.totally.lost"));

       request.setAttribute(Globals.ERROR_KEY, errors);

       request.setAttribute("myerrors", errors);

    %>

       <logic:messagesPresent name="myerrors">

         Yes

</logic:messagesPresent>

()判空标签

<logic:empty>

<logic:notEmpty>

该标签用于判断指定的字符是否为空。

例如:

    <%

      pageContext.setAttribute("test","");

    %>

   <logic:empty name="test">

       test 为空

   </logic:empty>

()转发和重定向标签

1.<logic:foward>转发标签

该标签用于进行全局转发,使用该标签的页面一般不再编写其他内容,因为随着转发,页面将跳转,原页面中的内容也没有意义了。例如:

    this is a test

 <logic:forward name="welcome"/>

   this is a new test

2.<logic:redirect>重定向标签

该标签用于进行重定向。具有属性:

href属性:   将页面重定向到href指定的完整外部链接。

page属性:   该属性指定一个本应用内的一个网页,标记将页面重定向到这个新的网页。

forward属性:该属性与struts-config.xml中的<global-forward>内的子项相对应。即将页面重定向到forward所指定的资源。

原文地址:https://www.cnblogs.com/winkey4986/p/2307713.html