Logic標簽做邏輯判斷
所謂的標簽庫編程就是進行四種屬性的操作: page、request、session、application
1.<logic:present></logic:present>
判斷是否有指定屬性存在/不存在指定范圍之中
如果不指定范圍,則表示要進行全面的查找,依次按照屬性范圍進行查找
<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>StrutsLogic</title>
</head>
<body>
<%
request.setAttribute("name","tanm");
session.setAttribute("password","123");
%>
<logic:present name="name" scope="request">
<h1>指定屬性存在,內容為: <bean:write name="name"/></h1>
</logic:present>
<logic:notPresent name="name" scope="request">
<h1>指定屬性不存在</h1>
</logic:notPresent>
<logic:present name="password" scope="session">
<h1>指定屬性存在,內容為: <bean:write name="password"/></h1>
</logic:present>
<logic:notPresent name="password" scope="session">
<h1>指定屬性不存在</h1>
</logic:notPresent>
</body>
</html:html>
2.邏輯判斷 =、>=、<=、!=、>、<
=: <logic:equal>
!=: <logic:notEqual>
>=: <logic:greaterEqual>
<=: <logic:lessEqual>
>: <logic:greaterThan>
<: <logic:lessThan>
屬性: name:指定的屬性名稱 scope:指定的屬性范圍
使用此種邏輯標簽最大的好處是可以簡單的進行各種類型的判斷,字符串、數值、字符、布爾
<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>StrutsLogic</title>
</head>
<body>
<%
request.setAttribute("name","tanm");
request.setAttribute("in",new Integer(1));
// request.setAttribute("in",new Character('1')); //也可以用字符
%>
<logic:equal name="name" scope="request" value="tanm">
<h1>語句滿足條件,內容是: <bean:write name="name"/></h1>
</logic:equal>
<logic:notEqual name="name" scope="request" value="tanm">
<h1>語句不滿足條件,內容不是: <bean:write name="name"/></h1>
</logic:notEqual>
<logic:equal name="in" scope="request" value="1">
<h1>內容是: <bean:write name="in"/></h1>
</logic:equal>
<logic:notEqual name="in" scope="request" value="1">
<h1>內容不是: <bean:write name="in"/></h1>
</logic:notEqual>
<logic:greaterThan name="in" scope="request" value="0">
<h1>內容大于0</h1>
</logic:greaterThan>
<logic:greaterEqual name="in" scope="request" value="1">
<h1>內容大于等于1</h1>
</logic:greaterEqual>
<logic:lessThan name="in" scope="request" value="2">
<h1>內容小于2</h1>
</logic:lessThan>
<logic:lessEqual name="in" scope="request" value="1">
<h1>內容小于等于1</h1>
</logic:lessEqual>
</body>
</html:html>
3.迭代標簽
<logic:iterate id=”每個元素的實例化對象” name=”屬性范圍” scope=”查找范圍” property=”找到對象里的屬性”>
使用此標簽可以輸出三種類型的數據
.對象數組
.Collection集合(單值)
.Map集合(二元偶對象)
無論輸出Collection還是Map都使用同一種方式進行輸出。
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>StrutsLogic</title>
</head>
<body>
<%
//List是Collection的子類
List all = new ArrayList();
//帶排序功能
// Set all = new TreeSet();
all.add("1111");
all.add("5222");
all.add("3333");
//將對象保存在屬性當中
request.setAttribute("list",all);
/**
要通過迭代標簽輸出Map集合
回顧:Map標簽本身不直接支持迭代輸出
輸出步驟:
Map-->Set-->Iterator-->Map.Entry-->key、value
*/
Map m = new HashMap();
m.put("name","yourname");
m.put("pass","yourpass");
request.setAttribute("map",m);
%>
<logic:iterate id="src" name="list" scope="request">
<h2><bean:write name="src" /></h2>
</logic:iterate>
<logic:iterate id="str" name="map" scope="request">
<h2><bean:write name="str" property="key"/></h2>
<h2><bean:write name="str" property="value"/></h2>
</logic:iterate>
</body>
</html:html>
更高級的應用
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>StrutsLogic</title>
</head>
<body>
<%
/**
在Map中保存多個Collection集合,該如何進行了輸出?
*/
List all = null;
Map m = new HashMap();
all = new ArrayList();
all.add("msdn");
all.add("url");
m.put("name",all);
all = new ArrayList();
all.add("mole");
all.add("msistri");
m.put("info",all);
//在一個Map中保存了多個集合,每個集合又包含了多個內容
request.setAttribute("list",m);
%>
<logic:iterate id="src" name="list" scope="request" >
<h2><bean:write name="src" property="key"/></h2>
<logic:iterate id="ins" name="src" scope="page" property="value">
<bean:write name="ins"/>
</logic:iterate>
</logic:iterate>
</body>
</html:html>
posted on 2007-10-17 14:10
譚明 閱讀(1290)
評論(0) 編輯 收藏 所屬分類:
Struts