Logic标签介绍:
A、compare标签
<%
request.setAttribute("intBean",new Integer(100));
%>
<logic:equal name="intBean" value="100" >
The value of intBean is "100".<p>
</logic:equal >
intBean的值是100则显示执行标签体
<%
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 >
someBean(javabean)的属性name值是Tom则显示标签体
<logic:greaterThan parameter="arg1" value="100" >
The first request parameter is greater than 100 <p>
</logic:greaterThan >
在URL请求参数中,参数名为arg1的值为100,则显示标签体。
B、match标签
<%
request.setAttribute("authorName", "LindaSun");
%>
<logic:match name="authorName" scope="request" value="Linda">
<bean:write name="authorName"/> has the string 'Sun' in it.
</logic:match>
在request范围内,名为authorName对应的值若跟Linda匹配则执行方法体
<logic:match name="authorName" scope="request" value="Linda" location="start">
<bean:write name="authorName"/> starts with the string 'Linda'.
</logic:match>
比上面的标签多了一个从哪里匹配的说明
C、forward 标签
<logic:forward name="index"/>
需要在struts-config.xml文件中配置
<global-forwards>
<name="index" path="/index.jsp"/>
</global-forwards>
D、redirect标签
<logic:redirect href="http://www.apache.org"/>
重定向到其他应用
E、presence 标签
<% request.setAttribute("emptyString", ""); %>
<logic:empty name="emptyString">
The variable named emptyString is empty!<P>
</logic:empty>
名字为emptyString对应的值为" ",则显示标签体
<logic:present name="noSuchBean" property="noSuchProperty">
Both noSuchBean and noSuchProperty exist!
</logic:present>
存在名字为noSuchBean,并且它存在属性noSuchProperty。则显示标签体
<%
ActionErrors errors = new ActionErrors();
errors.add("totallylost", new ActionMessage("application.totally.lost"));
request.setAttribute("myerrors", errors);
%>
<logic:messagesPresent name="myerrors">
Yes, there are errors in myerrors collection.
</logic:messagesPresent>
存在名字为myerrors的ActionErrors,则显示标签体
<logic:messagesPresent property="totallylost">
There is an error named "totallylost".
</logic:messagesPresent>
存在名字为totallylost的ActionMessage,则显示标签体
F、logic标签
<%
HashMap months = new HashMap();
months.put("Jan.", "January");
months.put("Feb.", "February");
months.put("Mar.", "March");
request.setAttribute("months", months);
%>
<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>
迭代输出map中的数据,格式为:索引.键:值
<%
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);
%>
<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>
双层迭代:
格式为: 索引.键
-------值 (每个键对应的值,从下标1开始,一共输出3个)
<%
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>
迭代输出集合中的元素
<logic:iterate id="element" indexId="index" name="Animals" offset="1" length="2">
<bean:write name="index"/>.<bean:write name="element"/><BR>
</logic:iterate><p>
迭代输出结合,格式为: 索引.集合中的元素