隨筆-8  評論-8  文章-10  trackbacks-0
                  在早期jsp的開發(fā)中會用到大量的Scriptlet代碼,這樣造成了jsp頁面維護性和可讀性的下降,而Struts所帶的標簽庫在一定程度上解決了此問題,所以我們提倡使用標簽而不是用Scriptlet。
                  
          一、Bean標簽
          1.<bean:define> 定義或復(fù)制一個對象
              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>求出長度,數(shù)組、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>打印對象或?qū)ο笾械膶傩?類似于${}的功能
              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 國際化 或調(diào)用Struts的消息資源配置文件(用于保存顯示信息)
              eg:
              首先在struts-config.xml中配置消息資源配置文件,加入一下代碼即可
          <message-resources parameter="illu.struts.ApplicationResources" />
              再在illu.struts下創(chuàng)建ApplicationResources.properties文件
              在文件中加入welcome = welcome {0} !
              {0}表示參數(shù)位置
              
          <%@ 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 應(yīng)越 閱讀(259) 評論(0)  編輯  收藏 所屬分類: struts學(xué)習(xí)

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 将乐县| 定襄县| 新宁县| 青岛市| 西丰县| 乾安县| 积石山| 亳州市| 太和县| 偏关县| 腾冲县| 林州市| 安徽省| 洛川县| 鹤岗市| 乌海市| 井研县| 特克斯县| 牡丹江市| 那坡县| 旬阳县| 房产| 伽师县| 贞丰县| 册亨县| 丹江口市| 阆中市| 巨野县| 海盐县| 化隆| 冷水江市| 云龙县| 上高县| 龙江县| 隆子县| 宁德市| 河北省| 南丰县| 易门县| 康保县| 宜丰县|