Struts標籤-BEAN標籤

                         Struts 標籤 ——  Bean 標籤

      JSP 開發之中的要求,程序中儘可能少的出現的Scriptlet 代碼。主要是因爲可維護性相對較好。
     提供了大量的自定義的標籤庫,通過標籤庫可以簡化JSP 的頁面代碼開發。

      Bean 標籤:主要與JavaBean 有關

     回顧:
          <jsp:useBean id="對象名稱" scope="保存範圍(四種屬性範圍)" class="包.類"/>
          <jsp:setProperty name="id   (jsp:useBean  中使用)" property="屬性"/>
          <jsp:getProperty name="id" property="屬性"/>

          只要是通過標籤設置的對象,則自動將對象保存在一個固定的範圍之中,由scope 指定
          一般對象都使用“id”進行表示
          如果要使用對象,在標籤通過“name ”屬性完成的

1、Bean 標籤
     在 Struts 中提供了一系列的與JavaBean 有關的操作標籤,稱爲Bean 標籤。
1.1、<bean:define>標籤
     定義或複製一個對象的
     定義對象一般爲 String 類型。
     複製對象

   <h1>${str}</h1>
   <h1><bean:write name="str"/></h1>
   </body>
</html:html>

     強調:
      · id :就表示一個存放在四種屬性範圍之中的名稱
      · name :表示使用一個存放在四種屬性範圍中的對象

1.2、<bean:size>標籤
      ·求出長度:數組、Collection、Map
     標籤肯定數據存放在四種屬性範圍之中。
輸出Map 的長度:
<%@ page language="java" pageEncoding="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>BeanDemo01.jsp</title>
   </head>

   <body>
   <%
      Map m = 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>

輸出 Collection 的長度:
<%@ page language="java" pageEncoding="GB2312"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

                                                                             E-Mail:[email protected]
<%@ 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>BeanDemo01.jsp</title>
   </head>

   <body>
   <%
      Collection col = new ArrayList() ;
      col.add("MLDN") ;
      col.add("lxh") ;
      col.add("mole") ;

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

     求長度的時候,即可以是Map 也可以是Collection,但是對於JSP 調用代碼是不變的。

1.3、<bean:write>標籤
     打印對象,也可以打印對象中的屬性。
     在JSP 2.0 中可以使用EL 代替 Struts 中的<bean:write>標籤

<%@ page language="java" pageEncoding="GB2312"%>
<%@ page import="java.util.*"%>
<%@ page import="org.lxh.tag.*"%>
<%@ 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>BeanDemo01.jsp</title>
   </head>

   <body>
   <%

                                                                              E-Mail:[email protected]
      SimpleBean simple = new SimpleBean() ;
      simple.setName("魔樂") ;
      simple.setPassword("www.mldn.cn") ;
      // 將此對象保存在屬性範圍之中
      request.setAttribute("sim",simple) ;
   %>
   <h1>使用EL:</h1>
   <h2>姓名:${sim.name}</h2>
   <h2>密碼:${sim.password}</h2>

   <hr>

   <h1>使用Bean 標籤:</h1>
   <h2>姓名:<bean:write name="sim" property="name" scope="request"/></h2>
   <h2>密碼:<bean:write name="sim" property="password" scope="request"/></h2>
   </body>
</html:html>

<%@ page language="java" pageEncoding="GB2312"%>
<%@ page import="java.util.*"%>
<%@ page import="org.lxh.tag.*"%>
<%@ 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>BeanDemo01.jsp</title>
   </head>

   <body>
   <jsp:useBean id="sim" class="org.lxh.tag.SimpleBean" scope="request"/>
   <jsp:setProperty name="sim" property="name" value="魔樂"/>
   <jsp:setProperty name="sim" property="password" value="www.mldn.cn"/>

   <h1>使用EL:</h1>
   <h2>姓名:${sim.name}</h2>
   <h2>密碼:${sim.password}</h2>

   <hr>

   <h1>使用Bean 標籤:</h1>
   <h2>姓名:<bean:write name="sim" property="name" scope="request"/></h2>
   <h2>密碼:<bean:write name="sim" property="password" scope="request"/></h2>
   </body>
</html:html>

                                                                             E-Mail:[email protected]
     結論:只要是標籤,在程序開發中就可以互相調用,所有的標籤基本上都是對四種屬性範圍的操作。

1.4、<bean:message>標籤
      Struts  國際化
      Struts  中存在一個資源文件(用於保存所有的錯誤消息)

     有些時候希望將所有的顯示內容保存在文件之中,
# Resources for parameter 'org.lxh.struts.ApplicationResources'
# Project TagDemo

welcome                                                                                                        =

<h1>/u6b22/u8fce/u5149/u4e34/u9b54/u4e50/u5728/u7ebf/uff01/uff01/uff01</h1>

<%@ page language="java" pageEncoding="GB2312"%>
<%@ page import="java.util.*"%>
<%@ page import="org.lxh.tag.*"%>
<%@ 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>BeanDemo01.jsp</title>
  </head>
  <body>
  <bean:message key="welcome"/>
  </body>
</html:html>

新的要求?
     希望顯示如下效果:

  歡迎 Xxx 光臨魔樂在線!!! 

在<bean:message>標籤中提供了一個佔位功能,在輸出的文件中佔着一位,這一位的數據等待標籤填寫。

welcome = <h1>/u6b22/u8fce {0} /u5149/u4e34/u9b54/u4e50/u5728/u7ebf/uff01/uff01/uff01</h1>

<%@ page language="java" pageEncoding="GB2312"%>
<%@ page import="java.util.*"%>
<%@ page import="org.lxh.tag.*"%>

                                                                          E-Mail:[email protected]
<%@ 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>BeanDemo01.jsp</title>
  </head>

  <body>
  <bean:message key="welcome" arg0="李興華"/>
  </body>
</html:html>

引發了一個問題?
     例如以下一種情況:
          有一套程序,此程序完成之後要同時供7 個國家的用戶訪問,那麼該怎麼做?
           ·針對每一個國家的語言完成一套代碼
           ·將所有國家的語言單獨保存在一個文件中,用戶使用程序會根據自己的國家自動選擇出語言,
而程序代碼只有一套。(推薦)

Struts 國際化?
     所有的內容通過<bean:message>輸出,之後所有的語言,單獨形成一個*.properties 文件。

根據瀏覽器的不同,顯示的語言也不同,因爲所有顯示的內容在*.properties 中已經完成的配置了。

ApplicationResources.properties:顯示中文
# Resources for parameter 'org.lxh.struts.ApplicationResources'
# Project TagDemo
welcome = <h1>/u6b22/u8fce {0}
/u5149/u4e34/u9b54/u4e50/u5728/u7ebf/uff01/uff01/uff01</h1>
name = /u7528/u6237/u540d/uff1a

ApplicationResources_en_US.properties:顯示英語
# Resources for parameter 'org.lxh.struts.ApplicationResources'
# Project TagDemo
welcome = <h1>/u6b22/u8fce {0}
/u5149/u4e34/u9b54/u4e50/u5728/u7ebf/uff01/uff01/uff01</h1>

name = User Name:

<h1><bean:message key="name"/></h1>

會根據瀏覽器的不同,自動進行語言的顯示,自動去調用相應的屬性文件

                                                                         E-Mail:[email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章