系統整理Strutslogic標簽庫17個常用標簽用法與示例,涵蓋條件判斷、比較、迭代、轉向、消息檢查等場景。
<logic:empty name="userList">:List/Map為空或對象為null/String為""時輸出體內容。等同if(userList.isEmpty())。<logic:notEmpty>:相反邏輯。<logic:present name="user" property="name">:request中存在user且其name屬性非空。支持header檢查HTTP頭。<logic:notPresent>:相反邏輯。<logic:equal name="user" property="state" value="1">啟用</logic:equal>:屬性值等於指定值。動態值可用ELvalue="${business.num}"。<logic:notEqual>、<logic:greaterEqual>、<logic:greaterThan>、<logic:lessEqual>、<logic:lessThan>:同理。<logic:match name="name" value="amigo" location="start">:字符串包含/前綴/後綴匹配,支持header匹配User-Agent等。<logic:notMatch>:相反邏輯。<logic:messagePresent property="error">:存在ActionErrors時輸出體內容,常配合<html:messages>遍歷顯示。<logic:messagesNotPresent>:相反邏輯。<logic:forward name="index"/>:查找全局forward轉向。<logic:redirect href="https://example.com"/>:客戶端重定向,可攜帶參數。<logic:iterate id="user" name="userList" indexId="index" offset="1" length="2">遍歷Collection/Map,支持分頁參數。嵌套示例:外層遍歷用戶,內層遍歷用戶地址列表(property="addressList")。
value屬性為字符串,數值比較自動轉型。scope屬性可限定查找範圍(page/request/session/application)。