phyeas Seam新手实战(2):自动生成的代码

来源:互联网 发布:七喜cms 编辑:程序博客网 时间:2024/04/28 05:57
Seam新手实战(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>
原创粉丝点击