Struts1 Logic标签

来源:互联网 发布:网站数据安全如何保证 编辑:程序博客网 时间:2024/05/16 15:08

logic:empty

该标签是用来判断是否为空的。如果为空,该标签体中嵌入的内容就会被处理。该标签用于以下情况:
1)当Java对象为null时;
2)当String对象为""时; 
3)当java.util.Collection对象中的isEmpty()返回true时;
4)当java.util.Map对象中的isEmpty()返回true时。
eg. 
< logic:empty   name="userList">   
...   
< /logic:empty> 
该句等同于:
if(userList.isEmpty())   {   
...   

}

logic:notEmpty

该标签的应用正好和logic:empty标签相反,略。

logic:equal

该标签为等于比较符。
eg1. 比较用户的状态属性是否1,若为1,输出"启用";
< logic:equal   name="user"  property="state"   value="1">
启用
< /logic:equal>
eg2. 如果上例中的value值是动态获得的,例如需要通过bean:write输出,因struts不支持标签嵌套,可采用EL来解决该问题。
<logic:equal   name="charge"  property="num"  value="${business.num}">   
......
< /logic:equal>

logic:notEqual

该标签意义与logic:equal相反,使用方法类似,略。

logic:forward

该标签用于实现页面导向,查找配置文件的全局forward。
eg. < logic:forward name="index"/>

logic:greaterEqual

为大于等于比较符。
eg. 当某学生的成绩大于等于90时,输出“优秀”:
< logic:greaterEqual name="student" property="score" value="90">
优秀
< /logic:greaterEqual>

logic:greaterThan

此为大于比较符,使用方法同logic:greaterEqual,略;

logic:lessEqual

此为小于等于比较符,使用方法同logic:greaterEqual,略;

logic:lessThan

此为小于比较符,使用方法同logic:greaterEqual,略;

logic:match

此标签比较对象是否相等;
eg1. 检查在request范围内的name属性是否包含"amigo"串: 
< logic:match name="name" scope="request" value="amigo">
< bean:write name="name"/>中有一个“amigo”串。
< /logic:match>
eg2. 检查在request范围内的name属性是否已“amigo”作为起始字符串:
< logic:match name="name" scope="request" value="amigo" location="start">
< bean:write name="name"/>以“amigo”作为起始字符串。
< /logic:match>
eg3. 
<logic:match header="user-agent" value="Windows">
你运行的是Windows系统
</logic:match>

logic:notMatch

此标签用于比较对象是否不相同,与logic:match意义相反,使用方法类似,略。

logic:messagePresent

该标签用于判断ActionMessages/ActionErrors对象是否存在;
eg. 如果存在error信息,将其全部输出:
< logic:messagePresent property="error"> 
< html:messages property="error" id="errMsg" > 
        <bean:write name="errMsg"/> 
< /html:messages>   
< /logic:messagePresent >

logic:messagesNotPresent

该标签用于判断ActionMessages/ActionErrors对象是否不存在,使用方法与logic:messagePresent类似,略

logic:present

此标签用于判断request对象传递参数是否存在。
eg1. user对象和它的name属性在request中都存在时,输出相应字符串:
<logic:present name="user" property="name">
   user对象和该对象的name属性都存在
< /logic:present> 
eg2. 若有一个名字为“user”的JavaBean,输出对应字符串:
< logic:present name="user" >
    有一个名字为“user”的JavaBean。
< /logic:present>
eg3. 
< logic:present header="user-agent">
    we got a user-agent header.
< /logic:present>

logic:notPresent

此标签用于判断request对象传递参数是否不存在,意义与了logic:present相反,使用方法类似,略。

logic:redirect

该标签用于实现页面转向,可传递参数。
eg1. < logic:redirect href="http://www.chinaitlab.com"/>

logic:iterator

用于显示列表为collection的值(List ,ArrayList,HashMap等)。
eg1. 逐一输出用户列表(userlList)中用户的姓名:
< logic:iterate  id="user" name="userList">
     < bean:write name="user" property="name"/>< br>
< /logic:iterate>
eg2. 从用户列表中输出从1开始的两个用户的姓名:
< logic:iterate  id="user" name="userList" indexId="index"  offset="1" length="2">
   < bean:write name="index"/>.
   < bean:write name="user" property="name"/>< br>
 < /logic:iterate>
eg3. logic:iterator标签的嵌套举例
< logic:iterate id="user" indexId="index" name="userList">
    < bean:write name="index"/>. 
    < bean:write name="user" property="name"/>< br>
    < logic:iterate id="address"name="user" property="addressList" length="3" offset="1">
< bean:write name="address"/><br>
    < /logic:iterate>
</logic:iterate>

a) 遍历数组


<%


String[] colors =


new String[]{"red","yellow","green"};


List list = new ArrayList();


list.add("a");


list.add("b")


Map map = new HashMap();


map.put("1","a");


map.put("2","b");


request.setAttribute("colors",colors);


request.setAttribute("list",list);


request.setAttribute("map",map);


%>


<logic:iterate id="color" name="colors">


<bean:write name="color"/>


</logic:iterate>


 


b) 遍历Collection


<logic:iterate id="element" name="list" scope="page|request|session|applicatoin">


<bean:write name="element"/>


</logic:iterate>


 


c) 遍历Map


<logic:iterate id="entry" indexId="index"


name="map" scope="page|request|session|applicatoin">


<bean:write name="entry" property="key"/>


<bean:write name="entry" property="value"/>


</logic:iterate>


属性scope省去不写,默认从page、request、session和application查找

<logic:forward> 进行请求转发



例如: <logic:forward name="index"/>


属性name指定的值为请求转发的全局目标资源,与Struts配置


文件中的<global-forward>元素中的<forward>子元素匹配。


<global-forwards>


<forward name="index" path="/index.jsp"/>


...


</global-forwards>


 

<logic:redirect> 进行请求重定向



它包括forward、href和page三个属性,这三个属性的使用


方法和<html:link>标签中forward、href和page属性的用法很相似。


例如: <logic:redirect href="http://localhost:8080/app/index.jsp"/>

0 0
原创粉丝点击