隨筆-8  評(píng)論-8  文章-10  trackbacks-0
                  在早期jsp的開(kāi)發(fā)中會(huì)用到大量的Scriptlet代碼,這樣造成了jsp頁(yè)面維護(hù)性和可讀性的下降,而Struts所帶的標(biāo)簽庫(kù)在一定程度上解決了此問(wèn)題,所以我們提倡使用標(biāo)簽而不是用Scriptlet。
                  
          一、Bean標(biāo)簽
          1.<bean:define> 定義或復(fù)制一個(gè)對(duì)象
              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>求出長(zhǎng)度,數(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對(duì)象保存在四種屬性范圍之中
              request.setAttribute("coll",coll) ;
            %
          >
            
          <bean:size id="len" name="coll" scope="request"/>
            
          <h1>長(zhǎng)度是:${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對(duì)象保存在四種屬性范圍之中
              request.setAttribute("namemap",m) ;
            %
          >
            
          <bean:size id="len" name="namemap" scope="request"/>
            
          <h1>長(zhǎng)度是:${len}</h1>
            
          </body>
          </html:html>

          3.<bean:write>打印對(duì)象或?qū)ο笾械膶傩?類似于${}的功能
              eg:
              先編寫一個(gè)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頁(yè)面
          <%@ 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標(biāo)簽:</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 國(guó)際化 或調(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>

              



          每天進(jìn)步一點(diǎn)點(diǎn)

          posted on 2008-08-05 15:56 應(yīng)越 閱讀(259) 評(píng)論(0)  編輯  收藏 所屬分類: struts學(xué)習(xí)
          主站蜘蛛池模板: 庄浪县| 浦北县| 西昌市| 长汀县| 德令哈市| 志丹县| 营口市| 克拉玛依市| 泾源县| 博客| 承德市| 四川省| 扎囊县| 文山县| 南投市| 拉孜县| 铜梁县| 兴化市| 乌审旗| 汶上县| 延吉市| 繁峙县| 岚皋县| 黎平县| 哈尔滨市| 德惠市| 河北区| 漯河市| 北票市| 安岳县| 长岭县| 遂平县| 台北市| 蕲春县| 丹凤县| 紫阳县| 丰台区| 任丘市| 琼中| 静海县| 和田市|