(一) 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所指定的资源。