自用logic标签例子

  • Post author:
  • Post category:其他




(一)


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>


该标签用于在页面中创建一个循环,以次来遍历数组、

Collection



Map

这样的对象。在

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

对象中,命名为

test1

。然后使用

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

内存在特定的

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”, 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

所指定的资源。