随笔-8  评论-8  文章-10  trackbacks-0
        在早期jsp的开发中会用到大量的Scriptlet代码,这样造成了jsp页面维护性和可读性的下降,而Struts所带的标签库在一定程度上解决了此问题,所以我们提倡使用标签而不是用Scriptlet。
        
二、logic标签
1.<logic:present>与<logic:notPresent>
    判断是否有指定属性存在/不存在指定锝范围之中
    如果不指定范围,则表示要进行全面锝查找
<%@ page language="java" pageEncoding="utf-8"%>

<%@ 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" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  
<head>    
    
<title>logic01.jsp</title>
  
</head>  
  
<body>    
<%
        request.setAttribute(
"name","illu");
        session.setAttribute(
"userId","5");
    
%>
    
<logic:present name="name" scope="request">
        
<h1>指定属性存在 ${name}</h1>
    
</logic:present>
    
<logic:notPresent name="name" scope="request">
        
<h1>指定属性不存在</h1>
    
</logic:notPresent>
    
<logic:present name="userId" scope="session">
        
<h1>指定属性存在 ${userId}</h1>
    
</logic:present>
    
<logic:notPresent name="userId" scope="session">
        
<h1>指定属性不存在</h1>
    
</logic:notPresent>
  
</body>
</html:html>

2.逻辑判断
a. =       <logic:equal>
b. !=      <logic:notEqual>
c. >=     <logic:greaterEqual>
d. <=    <logic:lessEqual>
e. >       <logic:greaterThan>
f. <       <logic:lessThan>

<%@ page language="java" pageEncoding="utf-8"%>

<%@ 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" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  
<head>
    
<title>logic02.jsp</title>
  
</head>  
  
<body>
   
<%
           request.setAttribute(
"num",new Character('1'));
       
%>
       
<logic:equal value="1" name="num" scope="request">
           
<h1>内容等于1</h1>
       
</logic:equal>
       
<logic:notEqual scope="request" name="num" value="1">
           
<h1>内容不为1</h1>
       
</logic:notEqual>
       
<logic:greaterEqual name="num" scope="request" value="1">
           
<h1>内容大于等于1</h1>
       
</logic:greaterEqual>
       
<logic:lessEqual name="num" scope="request" value="1">
           
<h1>内容小于等于1</h1>
       
</logic:lessEqual>
       
<logic:greaterThan name="num" scope="request" value="1">
           
<h1>内容大于1</h1>
       
</logic:greaterThan>
       
<logic:lessThan name="num" scope="request" value="1">
           
<h1>内容小于1</h1>
       
</logic:lessThan>
  
</body>
</html:html>

3.<logic:iterate>迭代标签
     <logic:iterate id="集合中对象的使用名称" name="属性名称" scope="存储范围" property="对象属性名称"></logic:iterate>
a.遍历Set
<%@ page language="java" pageEncoding="utf-8"%>
<%@ 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" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  
<head>
    
<title>logic03.jsp</title>
  
</head>
  
<body>
  
<%
        
Set set = new HashSet();
        
set.add("illu");
        
set.add("stephen");
        
set.add("why");
        request.setAttribute(
"names",set);
    
%>
    
<logic:iterate id="name" name="names" scope="request">
        
<h3><bean:write name="name"/>
    
</logic:iterate>
  
</body>
</html:html>

b.遍历List
<%@ page language="java" pageEncoding="utf-8"%>
<%@ 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" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  
<head>
    
<title>logic04.jsp</title>
  
</head>  
  
<body>
    
<%
        List list 
= new ArrayList();
        list.add(
"illu");
        list.add(
"stephen");
        list.add(
"why");
        request.setAttribute(
"names",list);
    
%>
    
<logic:iterate id="name" name="names" scope="request">
        
<h3><bean:write name="name"/></h3>
    
</logic:iterate>
  
</body>
</html:html>

c.遍历Map
<%@ page language="java" pageEncoding="utf-8"%>
<%@ 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" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  
<head>    
    
<title>logic05.jsp</title>
  
</head>  
  
<body>
    
<% 
        Map map 
= new HashMap();
        map.put(
"illu","4");
        map.put(
"why","3");
        map.put(
"stephen","7");
        request.setAttribute(
"names",map);
    
%>
    
<logic:iterate id="name" name="names" scope="request">
        
<h3><bean:write name="name" property="key"/>--><bean:write name="name" property="value"/></h3>
    
</logic:iterate>
  
</body>
</html:html>
d.遍历集合套集合
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ 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" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  
<head>
    
<title>logic07.jsp</title>
  
</head>  
  
<body>
      
<%
          Map map 
= new HashMap();
          List asia 
= new ArrayList();
          asia.add(
"China");
          asia.add(
"Japan");
          asia.add(
"Korea");
          map.put(
"asia",asia);
          
          List europe 
= new ArrayList();
          europe.add(
"Britain");
          europe.add(
"Italy");
          europe.add(
"France");
          map.put(
"europe",europe);
          request.setAttribute(
"names",map);
      
%>
      
<logic:iterate id="temp" name="names" scope="request">
          
<h2><bean:write name="temp" property="key"/></h2>
          
<logic:iterate id="name" name="temp" property="value" scope="page">
              
<h4><bean:write name="name"/></h4>
          
</logic:iterate>
      
</logic:iterate>
  
</body>
</html:html>



每天进步一点点

posted on 2008-08-07 23:22 应越 阅读(1406) 评论(0)  编辑  收藏 所属分类: struts学习