隨筆-28  評論-15  文章-81  trackbacks-0
           

                   Logic標(biāo)簽做邏輯判斷
              所謂的標(biāo)簽庫編程就是進(jìn)行四種屬性的操作: page、request、session、application
          1.<logic:present></logic:present>
             判斷是否有指定屬性存在/不存在指定范圍之中
             如果不指定范圍,則表示要進(jìn)行全面的查找,依次按照屬性范圍進(jìn)行查找
           

          <%@ 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>指定屬性存在,內(nèi)容為: <bean:write name="name"/></h1>

                </logic:present>

                <logic:notPresent name="name" scope="request">

                   <h1>指定屬性不存在</h1>

                </logic:notPresent>

               

                <logic:present name="password" scope="session">

                   <h1>指定屬性存在,內(nèi)容為: <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:指定的屬性范圍

             使用此種邏輯標(biāo)簽最大的好處是可以簡單的進(jìn)行各種類型的判斷,字符串、數(shù)值、字符、布爾

          <%@ 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>語句滿足條件,內(nèi)容是: <bean:write name="name"/></h1>

                </logic:equal>

                <logic:notEqual name="name" scope="request" value="tanm">

                    <h1>語句不滿足條件,內(nèi)容不是: <bean:write name="name"/></h1>

                </logic:notEqual>

                <logic:equal name="in" scope="request" value="1">

                   <h1>內(nèi)容是: <bean:write name="in"/></h1>

                </logic:equal>

                <logic:notEqual name="in" scope="request" value="1">

                    <h1>內(nèi)容不是: <bean:write name="in"/></h1>

                </logic:notEqual>

                <logic:greaterThan name="in" scope="request" value="0">

                    <h1>內(nèi)容大于0</h1>

                </logic:greaterThan>

                <logic:greaterEqual name="in" scope="request" value="1">

                    <h1>內(nèi)容大于等于1</h1>

                </logic:greaterEqual>

                <logic:lessThan name="in" scope="request" value="2">

                    <h1>內(nèi)容小于2</h1>

                </logic:lessThan>

                <logic:lessEqual name="in" scope="request" value="1">

                    <h1>內(nèi)容小于等于1</h1>

                </logic:lessEqual>

               

           </body>

          </html:html>

          3.迭代標(biāo)簽

          <logic:iterate id=”每個元素的實例化對象” name=”屬性范圍” scope=”查找范圍” property=”找到對象里的屬性”>

          使用此標(biāo)簽可以輸出三種類型的數(shù)據(jù)

          .對象數(shù)組

          .Collection集合(單值)

          .Map集合(二元偶對象)

          無論輸出Collection還是Map都使用同一種方式進(jìn)行輸出。

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

              <%

                  //ListCollection的子類

                  List all = new ArrayList();

                  //帶排序功能

                 // Set all = new TreeSet();

                  all.add("1111");

                  all.add("5222");

                  all.add("3333");

                  //將對象保存在屬性當(dāng)中

                  request.setAttribute("list",all);

                  /**

                    要通過迭代標(biāo)簽輸出Map集合

                    回顧:Map標(biāo)簽本身不直接支持迭代輸出

                    輸出步驟:

                    Map-->Set-->Iterator-->Map.Entry-->keyvalue

                  */

                  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>

          更高級的應(yīng)用

          <%@ 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集合,該如何進(jìn)行了輸出?

                  */

                  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中保存了多個集合,每個集合又包含了多個內(nèi)容

                  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 譚明 閱讀(1291) 評論(0)  編輯  收藏 所屬分類: Struts

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 惠来县| 鄂伦春自治旗| 麻阳| 金湖县| 汽车| 怀安县| 迁西县| 公主岭市| 武强县| 门头沟区| 塘沽区| 玛纳斯县| 盘锦市| 康保县| 沾益县| 阜阳市| 吉安县| 洛扎县| 台州市| 独山县| 杨浦区| 资阳市| 运城市| 集贤县| 三台县| 盈江县| 南京市| 登封市| 岳阳市| 凤庆县| 吐鲁番市| 阜平县| 泾川县| 台中市| 财经| 华阴市| 海丰县| 阆中市| 定陶县| 多伦县| 宣武区|