隨筆 - 25  文章 - 32  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

            創建工程后會生成一大堆代碼。基本上都是配置文件。而在做 Seam 開發的過程中是不需要整天修改配置文件的。最多寫寫pages.xml或者faces-config.xml。Seam生成的文件夾如下面的結構:

          其中。build文件夾存放的是Ant編譯后的東西。
                      resources文件夾里就一個文件。XXXX-ds.xml。是用于存放數據源(DataSource)配置文件的。
                      src:
                                  src下有兩個文件夾:action和modal。即存放頁面動作與領域模型。
                                  在modal中有個META-INF文件夾,JPA的配置文件persistence.xml就存放在這個文件夾里。
                                  在Modal文件夾里還有幾個值得注意的文件。
                                  比如messages_en.properties和security.drl。messages_en.properties是存放系統消息的。
                                  如果想讓Seam的系統消息顯示中文就需要翻譯這個文件。而security.drl則是定義安全規則的。
                                  在action文件夾中。有一個包:org\domain\SeamTest\session。其中Authenticator.java會自動生成。用于做登陸驗證的。在這里簡單介紹下,先看代碼:
                                  
          @Name("authenticator")
          public class Authenticator
          {
              @Logger Log log;
              
              @In Identity identity;
             
              
          public boolean authenticate()
              
          {
                  log.info(
          "authenticating #0", identity.getUsername());
                  
          //write your authentication logic here,
                  
          //return true if the authentication was
                  
          //successful, false otherwise
                  identity.addRole("admin");
                  
          return true;
              }

          }

                   @Name 是用于定義Seam組件。這樣定義的Seam組件可用于雙向注入和其他頁面操作。如果你沒有這個注釋。則表示這個并不是Seam組件,所以也起了一個標識Seam組件的目的。在這里Seam組件被命名為"authenticator",在頁面上要調用這個組件的authenticate方法需要這樣寫:#{authenticator.authenticate}
                  @Logger用于注入日志組件
                  @In        用于雙向注入。在注入時Seam會尋找當前容器中與該變量名相符的組件。當然了。也可以注入變量名不同的組件。需要指定組件名稱,例如:@In("ident")。這樣的意思就是在容器中尋找名為"ident"的組件將其注入到該類中。(Seam的作用域比較復雜,本篇暫不介紹)
                  接下來便是authenticate方法,這個方法中其實最重要的是后面兩句。log.info(....)的意思即保存日志信息,最后那句意思即通過驗證。如果驗證的用戶名密碼不符合則return false;來表示拒絕登陸。比較復雜的是identity.addRole。這個方法是為當前用戶添加一個角色。單如果該方法最終返回false。那么這些添加的角色將不保存。
                   那么。如何讓Seam知道當用戶登陸時調用這個方法驗證呢?在/WebContent/WEB-INF/components.xml中有這樣一段配置:
             <security:identity authenticate-method="#{authenticator.authenticate}"
                                     security-rules
          ="#{securityRules}"
                                        remember-me
          ="true"/>
          其中authenticate-method即驗證的方法。是以組件形式調用。還有一個security-rules屬性則是安全規則。在哪里配置的?就在上面。
             <drools:rule-base name="securityRules">
                 
          <drools:rule-files><value>/security.drl</value></drools:rule-files>
             
          </drools:rule-base>

          這就是剛剛說的那個文件。
          既然說到components.xml,那我們就來看下這個文件里還有些什么東西。
           <core:init debug="true" jndi-pattern="@jndiPattern@"/>
          這段代碼是定義jndi查找規則的。@jndiPattern@的定義是在/src/modal/components.properties里的這樣一段配置:
          #
          #Fri Dec 
          05 10:37:03 CST 2008
          jndiPattern
          =\#{ejbName}/local
          embeddedEjb
          =false
          這段配置的意思是開啟jndi查找規則是ejb組件名稱/local。即使用本地EJB組件。而非遠程EJB組件
             <core:manager concurrent-request-timeout="500" 
                           conversation-timeout
          ="120000" 
                           conversation-id-parameter
          ="cid"
                           parent-conversation-id-parameter
          ="pid"/>
          conversation-timeout  conversation(頁面流)過期時間。我將conversation稱為頁面流可能不太合適。可以根據你的理解去稱呼它。它是一段頁面流轉的過程定義。Seam中定義了一conversation作用域
          conversation-id-parameter用于定義conversation的reuqest parameter name。即因為用戶現在在哪個頁面流中需要瀏覽器回傳一個參數才知道。
          parent-conversation-id-parameter。conversation允許定義子頁面流。這個屬性定義瀏覽器回傳父conversation的參數名
             <persistence:managed-persistence-context name="entityManager"
                                               auto-create
          ="true"
                                    entity-manager-factory
          ="#{SeamTestEntityManagerFactory}"/>
          這個就是jpa的entityManager組件的定義了。在Seam組件中使用@In("entityManager")將會自動注入這個組件
             <event type="org.jboss.seam.security.notLoggedIn">
                 
          <action execute="#{redirect.captureCurrentView}"/>
             
          </event>
             
          <event type="org.jboss.seam.security.loginSuccessful">
                 
          <action execute="#{redirect.returnToCapturedView}"/>
             
          </event>
          這個是對于登陸用的。當客戶訪問一個頁面需要登陸。但用戶又沒有登陸。這時會轉到一個登陸頁面。登陸完畢后又轉會用戶要進入的頁面。如果有這樣的需求就要加上這兩句配置。不過Seam已經自動生成了
          其他:默認生成的 face-config.xml里沒有支持中文。可以加入
              <application>
                  
          <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
                  
          <locale-config>
                      
          <default-locale>en</default-locale>
                      
          <supported-locale>bg</supported-locale>
                      
          <supported-locale>de</supported-locale>
                      
          <supported-locale>en</supported-locale>
                      
          <supported-locale>fr</supported-locale>
                      
          <supported-locale>tr</supported-locale>
                  
          </locale-config>
              
          </application>
          代碼:
          <supported-locale>zh_CN</supported-locale>
          posted on 2008-12-09 12:03 phyeas 閱讀(1181) 評論(0)  編輯  收藏 所屬分類: Seam項目實戰
          主站蜘蛛池模板: 荔波县| 达州市| 安泽县| 闻喜县| 泗水县| 公安县| 民权县| 南宁市| 齐齐哈尔市| 南木林县| 黑山县| 平度市| 丹寨县| 瑞丽市| 商洛市| 克拉玛依市| 清流县| 蓬安县| 兴义市| 古浪县| 石嘴山市| 宁强县| 宁陕县| 莲花县| 永吉县| 丰原市| 任丘市| 榕江县| 全椒县| 新河县| 琼结县| 阳江市| 明水县| 财经| 江川县| 明星| 云和县| 临泉县| 岳阳县| 且末县| 怀安县|