1、Struts标签的logic标签
Logic标签是逻辑标签,是Struts中比较重要的标签,完成各种逻辑运算操作,可以直接支持全局调转。
2、1<logic:present><logic:present>标签
<logic:request><logic:norequest>标签是用于在一个jsp页面中判断很多数据是否存在,例如数据是否存在,传递的参数是否存在等
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ 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"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<% // 设置request范围的属性
request.setAttribute("author","李兴华") ;
%>
<logic:present name="author" scope="request">
<h3>author属性存在,内容是:${author}</h3>
</logic:present>
<logic:notPresent name="url" scope="request">
<h3>url属性不存在!</h3>
</logic:notPresent>
</body>
</html>
2、2<logic:empty><logic:notempty>
<logic:empty><logic:notempty>来判断属性是否为null,或者判断一个集合的长度是否为0
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ 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"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%
List<String> all = new ArrayList<String>() ;
request.setAttribute("all",all) ; // 集合长度为0
%>
<logic:empty name="all" scope="request">
<h3>集合的内容为空(长度为0)!</h3>
</logic:empty>
<logic:empty name="author" scope="request">
<h3>没有发现author属性!</h3>
</logic:empty>
</body>
</html>
2、3 <logic:iterate>标签
<logic:iterate>能够输出对象数组,collection集合,Map集合
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ 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"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%
String allArr[] = {"www.MLDN.cn","www.MLDNJAVA.cn","www.JIANGKER.com"} ;
request.setAttribute("allArr",allArr) ;
%>
<h3>输出对象数组</h3><ol>
<logic:iterate id="arr" name="allArr" scope="request">
<li>网站:${arr}</li>
</logic:iterate></ol>
<%
List<String> allList = new ArrayList<String>() ;
allList.add("www.MLDN.cn") ;
allList.add("www.MLDNJAVA.cn") ;
allList.add("www.JIANGKER.com") ;
request.setAttribute("allList",allList) ;
%>
<h3>输出List集合</h3><ol>
<logic:iterate id="list" name="allList" scope="request">
<li>网站:${list}</li>
</logic:iterate></ol>
<%
Map<String,String> allMap = new HashMap<String,String>() ;
allMap.put("url1","www.MLDN.cn") ;
allMap.put("url2","www.MLDNJAVA.cn") ;
allMap.put("url3","www.JIANGKER.com") ;
request.setAttribute("allMap",allMap) ;
%>
<h3>输出Map集合</h3><ol>
<logic:iterate id="map" name="allMap" scope="request">
<li>网站:${map.key} --> ${map.value}</li>
</logic:iterate></ol>
</body>
</html>
2.4 logic其他标签,不常用
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ 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"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%
request.setAttribute("author","李兴华") ;
request.setAttribute("num",30) ;
%>
<logic:equal name="author" value="李兴华" scope="request">
equal条件满足!<br>
</logic:equal>
<logic:notEqual name="author" value="MLDN" scope="request">
notEqual条件满足!<br>
</logic:notEqual>
<logic:lessThan name="num" value="50" scope="request">
数字小于50!<br>
</logic:lessThan>
<logic:greaterThan name="num" value="20" scope="request">
数字大于20!<br>
</logic:greaterThan>
<logic:lessEqual name="num" value="30" scope="request">
数字小于等于30!<br>
</logic:lessEqual>
<logic:greaterEqual name="num" value="30" scope="request">
数字大于等于30!<br>
</logic:greaterEqual>
</body>
</html>