隨筆-8  評論-8  文章-10  trackbacks-0
                  在早期jsp的開發中會用到大量的Scriptlet代碼,這樣造成了jsp頁面維護性和可讀性的下降,而Struts所帶的標簽庫在一定程度上解決了此問題,所以我們提倡使用標簽而不是用Scriptlet。
                  
          一、Bean標簽
          1.<bean:define> 定義或復制一個對象
              eg:
          <%@ page language="java" pageEncoding="ISO-8859-1"%>
          <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
          <html:html lang="true">
            
          <head>
              
          <title>bean_define</title>
            
          </head>
            
            
          <body>
            
          <bean:define id="str" value="Hello">
            
          <h1>${str}</h1>
            
          </body>
          </html:html>

          2.<bean:size>求出長度,數組、Collection、Map
              Collection eg:
          <%@ page language="java" pageEncoding="GB2312"%>
          <%@ page import="java.util.*"%>
          <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
          <html:html lang="true">
            
          <head>
              
          <title>bean_size</title>
            
          </head>
            
            
          <body>
            
          <%
              Collection 
          coll= new ArrayList() ;
              coll.add("北京") ;
              coll.add("上海") ;
              coll.add("西安") ;

              // 將Collection對象保存在四種屬性范圍之中
              request.setAttribute("coll",coll) ;
            %
          >
            
          <bean:size id="len" name="coll" scope="request"/>
            
          <h1>長度是:${len}</h1>
            
          </body>
          </html:html>

              map eg:
          <%@ page language="java" pageEncoding="GB2312"%>
          <%@ page import="java.util.*"%>
          <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
          <html:html lang="true">
            
          <head>
              
          <title>bean_size</title>
            
          </head>
            
            
          <body>
            
          <%
              Map 
          = new HashMap() ;
              m.put("one","1") ;
              m.put("two","2") ;
              m.put("three","3") ;

              // 將Map對象保存在四種屬性范圍之中
              request.setAttribute("namemap",m) ;
            %
          >
            
          <bean:size id="len" name="namemap" scope="request"/>
            
          <h1>長度是:${len}</h1>
            
          </body>
          </html:html>

          3.<bean:write>打印對象或對象中的屬性 類似于${}的功能
              eg:
              先編寫一個Person類(JavaBean)
          package com.illu;

          public class Person {
              
          private String name;
              
          private String password;
              
          public Person(){}
              
          public String getName() {
                  
          return name;
              }

              
          public void setName(String name) {
                  
          this.name = name;
              }

              
          public String getPassword() {
                  
          return password;
              }

              
          public void setPassword(String password) {
                  
          this.password = password;
              }

          }
              然后在編寫jsp頁面
          <%@ page language="java" pageEncoding="GB2312"%>
          <%@ page import="java.util.*"%>
          <%@ page import="com.illu.Person"%>
          <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
          <html:html lang="true">
            
          <head>
              
          <title>bean_write</title>
            
          </head>
            
            
          <body>
            
             <jsp:useBean id="person" class="com.illu.Person" scope="request"/>
                    <jsp:setProperty name="person" property="name" value="illu"/>
                    <jsp:setProperty name="person" property="password" value="123456789"/>

            
          <h1>使用EL:</h1>
            
          <h2>姓名:${person.name}</h2>
            
          <h2>密碼:${person.password}</h2>
            
          <hr>
            
          <h1>使用Bean標簽:</h1>
            
          <h2>姓名:<bean:write name="person" property="name" scope="request"/></h2>
            
          <h2>密碼:<bean:write name="person" property="password" scope="request"/></h2>
            
          </body>
          </html:html>

              4.<bean:message>Struts 國際化 或調用Struts的消息資源配置文件(用于保存顯示信息)
              eg:
              首先在struts-config.xml中配置消息資源配置文件,加入一下代碼即可
          <message-resources parameter="illu.struts.ApplicationResources" />
              再在illu.struts下創建ApplicationResources.properties文件
              在文件中加入welcome = welcome {0} !
              {0}表示參數位置
              
          <%@ page language="java" pageEncoding="GB2312"%>
          <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
            
          <head>
              
          <title>bean_message</title>
            
          </head>
            
            
          <body>
            
          <bean:message key="welcome" arg0="illu"/>
            
          </body>
          </html:html>

              



          每天進步一點點

          posted on 2008-08-05 15:56 應越 閱讀(263) 評論(0)  編輯  收藏 所屬分類: struts學習
          主站蜘蛛池模板: 象山县| 防城港市| 临泉县| 和林格尔县| 广灵县| 陵水| 库车县| 衡阳市| 关岭| 柳江县| 忻州市| 武汉市| 梅河口市| 定安县| 怀化市| 白水县| 日土县| 东城区| 方山县| 安泽县| 扎鲁特旗| 阳泉市| 德昌县| 洛扎县| 三台县| 京山县| 阳江市| 和林格尔县| 门源| 秦皇岛市| 内江市| 通州区| 秭归县| 永平县| 蕉岭县| 清河县| 开鲁县| 阳东县| 炉霍县| 镇安县| 临城县|