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
谭明 阅读(1279)
评论(0) 编辑 收藏 所属分类:
Struts