alartin's profileWindows Live 共享空间PhotosBlogListsMore Tools Help

Blog


    January 10

    JBoss Seam: Seam应用框架

    Seam Application Framework, 听起来挺吓人的,也很容易让人犯糊涂,Seam本身不就是个Web框架么,那里来的应用框架. 其实称之为Seam Component Template Framework更加合适. Seam 组件模板框架.

    原因很简单, 这个名词看起来吓人,其实就干一件事情, 把Seam组件开发再简化(尽管通过注释等方法, 开发Seam组件非常简单了). 怎么再简化呢? 大家可以想一想Struts的Dynamic Action历史就明白了. 也就是通过配置来创建基本的Seam组件, 你可以不用写类了! 在componets.xml文件中配置.例如下面的配置信息能够创建一个基本的实现CRUD功能的Seam组件:

    <framework:entity-home name="personHome" 
                           entity-class="eg.Person" 
                           entity-manager="#{personDatabase}">
        <framework:id>#{param.personId}</framework:id>
    </framework:entity-home>
    如果你讨厌通过XML配置来替代编程的话(反正我讨厌), 你也可以通过类继承来实现:
    @Stateful
    @Name("personHome")
    public class PersonHome extends EntityHome<Person> implements LocalPersonHome {
        @RequestParameter String personId;
        @In EntityManager personDatabase;
        
        public Object getId() { return personId; }
        public EntityManager getEntityManager() { return personDatabase; }
        
    }

    一头雾水吧?让我们看看Seam的代码.所有的Application Framework代码位于org.jboss.seam.framework下面.

    最重要的两个类为Home类和Query类.

    Home类: 提供了实体类的持久化操作CRUD. Seam提供两种实现:EntityHome和HibernateEntityHome. 一个是给JPA的,一个是给Hibernate的.

    Query类: 顾名思义,提供实体类查询的.同样,Seam提供两种实现: EntityQuery, HibernateEntityQuery.一个是给JPA的,一个是给Hibernate的.

    Home类和Query类天生就能和会话范围,事件范围和对话范围一起工作.

    需要注意的是:Seam Application Framework只能和Seam管理的持久化上下文一起工作,Seam-managed Persistence Context.

    默认的情况下,Application Framework的组件查找一个名为entityManager的持久化上下文.

    来点仔细的. 我们看一下Home, 其实Home就是给某个实体类干CRUD活的,类似于EJB3的Session Bean, 比如说我们有个实体类Person, 那么PersonHome就是创建,修改,删除Person的工具么.

    @Entity
    public class Person { // 这个是我们的实体类
        @Id private Long id;
        private String firstName;
        private String lastName;
        private Country nationality;
        
        //getters and setters...
    }
    1. 可以通过在components.xml配置相对应的Home类:
    <framework:entity-home name="personHome" entity-class="eg.Person" />
    2. 或者自己写类来继承
    @Name("personHome")
    public class PersonHome extends EntityHome<Person> {} // 参数是实体类啊

    反正我觉的第二种方法最合适,如果你是个程序员的话.

    然后你就自动获得PersonHome类了, 内置persist(), remove(), update()和getInstance()方法. 酷吧!当然,你要调用remove()或update()方法前,要先设置setId(Object id)方法.

    OK, 那么怎么使用呢, 我们看一个例子, 我们可以直接在页面上使用Home类:

    <h1>Create Person</h1>
    <h:form>
        <div>First name: <h:inputText value="#{personHome.instance.firstName}"/></div>
        <div>Last name: <h:inputText value="#{personHome.instance.lastName}"/></div>
        <div>
            <h:commandButton value="Create Person" action="#{personHome.persist}"/>
        </div>
    </h:form>

    用户在页面上的输入直接就能创建一个Person实体对象了,当然,通过PersonHome创建的.不过我们会觉得使用personHome.instance很不爽, 其实我们更加愿意使用person来代替它. 做到这一点也很容易:

    1. 在components.xml中配置

    <factory name="person" 
             value="#{personHome.instance}"/>
    
    <framework:entity-home name="personHome" 
                           entity-class="eg.Person" />
    2.在自己写的类中加一个方法,然后用@Factory注释
    @Name("personHome")
    public class PersonHome extends EntityHome<Person> {
        // 很简单,这个方法仅仅调用的getInstance()而已
        @Factory("person")
        public Person initPerson() { return getInstance(); }
        
    }
    OK, 现在我们的页面就可以变成:
    <h1>Create Person</h1>
    <h:form>
        <div>First name: <h:inputText value="#{person.firstName}"/></div>
        <div>Last name: <h:inputText value="#{person.lastName}"/></div>
        <div>                                              // 这里没变
            <h:commandButton value="Create Person" action="#{personHome.persist}"/>
        </div>
    </h:form>
    相信你已经很满意了吧.那么,也许你想搞个页面来编辑某个已经持久化的Person实体,那么你需要在pages.xml中配置页面参数:
    <pages>
        <page view-id="/editPerson.jsp"> // 编辑页面需要person id参数
            <param name="personId" value="#{personHome.id}"/>
        </page>
    </pages>
    让我们看看编辑页面:
    <h1> // 如果personHome还没有管理某个person实体的话, 页面显示为 创建Person, 否则显示 编辑Person
         // 此时,页面参数personId 为空
        <h:outputText rendered="#{!personHome.managed}" value="Create Person"/>
        <h:outputText rendered="#{personHome.managed}" value="Edit Person"/>
    </h1>
    <h:form>
        <div>First name: <h:inputText value="#{person.firstName}"/></div>
        <div>Last name: <h:inputText value="#{person.lastName}"/></div>
        <div>
            //如果personHome还没有管理某个person实体的话,按钮显示创建,否则的话显示两个按钮 更新和删除
            // 此时,页面参数personId 为空
            <h:commandButton value="Create Person" action="#{personHome.persist}" rendered="#{!personHome.managed}"/>
            <h:commandButton value="Update Person" action="#{personHome.update}" rendered="#{personHome.managed}"/>
            <h:commandButton value="Delete Person" action="#{personHome.remove}" rendered="#{personHome.managed}"/>
        </div>
    </h:form>
    关于Home类,我们先讲这么多,再来看看Query类.
    Query类通常用来查询一个实体对象的列表.比如获得所有Person:
    1.通过components.xml配置
    <framework:entity-query name="people" 
                            ejbql="select p from Person p"/>
    使用页面:
    <h1>List of people</h1>
    <h:dataTable value="#{people.resultList}" var="person">
        <h:column>
            <s:link view="/editPerson.jsp" value="#{person.firstName} #{person.lastName}">
                <f:param name="personId" value="#{person.id}"/>
            </s:link>
        </h:column>
    </h:dataTable>
    要是我们想分页看呢:
    <framework:entity-query name="people" 
                            ejbql="select p from Person p" 
                            order="lastName"  // 排序
                            max-results="20"/> // 分页最大结果
    在pages.xml配置:
    <pages>
        <page view-id="/searchPerson.jsp">
            <param name="firstResult" value="#{people.firstResult}"/>
        </page>
    </pages>
    更改后的页面:
    <h1>Search for people</h1>
    <h:dataTable value="#{people.resultList}" var="person">
        <h:column>
            <s:link view="/editPerson.jsp" value="#{person.firstName} #{person.lastName}">
                <f:param name="personId" value="#{person.id}"/>
            </s:link>
        </h:column>
    </h:dataTable>
    
    <s:link view="/search.xhtml" rendered="#{people.previousExists}" value="First Page">
        <f:param name="firstResult" value="0"/> // <<
    </s:link>
    
    <s:link view="/search.xhtml" rendered="#{people.previousExists}" value="Previous Page">
        <f:param name="firstResult" value="#{people.previousFirstResult}"/>
    </s:link> 
    <s:link view="/search.xhtml" rendered="#{people.nextExists}" value="Next Page">
        <f:param name="firstResult" value="#{people.nextFirstResult}"/>
    </s:link> 
    <s:link view="/search.xhtml" rendered="#{people.nextExists}" value="Last Page">
        <f:param name="firstResult" value="#{people.lastFirstResult}"/>
    </s:link>
    我们甚至可以监听一些事件,例如更新,删除等等然后根据这些事件,刷新查询:
    <event type="org.jboss.seam.afterTransactionSuccess">
        <action execute="#{people.refresh}" />
    </event>
    甚至可以限定是Person实体的事件:
    <event type="org.jboss.seam.afterTransactionSuccess.Person">
        <action execute="#{people.refresh}" />
    </event>

    需要注意的是:上面的Query例子都用的是配置形式,你也可以写自己的类来继承.Seam通常将Query的继承类命名为XXXList, 将Home类命名为XXXHome, 其中XXX是实体类.

    有趣的Controller类其实就是一些实体类的帮助类,让你更加容易使用内置组件和内置组件的方法.

            like   session bean                          |                           like session bean

    XXXList/Query(负责XXX的查询)  <----  你的实体类XXX  ----> XXXHome (负责XXX的CRUD工作)

                                                                |

                                                       Controller(XXX的帮助类)

    Comments (1)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    No namewrote:
    上海拉拉钢<a href="http://www.lalamo.net/Corporation_instruct.asp">膜结构公司</a>是一家专门主要从事建筑<a href="http://www.lalamo.net/">膜结构</a>上海上海宇栾钢<a href="http://www.yuluan.sh.cn/Corporation_instruct.asp">膜结构公司</a>。

    上海海洋泵阀制造有限公司是专业生产<a href="http://www.sea-pump.com/dj.html">多级泵</a>,<a href="http://www.sea-pump.com/gm.html">隔膜泵</a>,<a href="http://www.sea-pump.com/hg.html">化工泵</a>,<a href="http://www.sea-pump.com/pw.html">排污泵</a>,<a href="http://www.sea-pump.com/xf.html">消防泵</a>的大型股份企业,本公司生产的消防泵,排污泵,化工泵,隔膜泵,多级泵已广泛应用于城市给排水、城市污水处理以及国家大型环保处理工程;高层建筑增压送水,园林喷灌、消防增压、远距送水、农田排灌、纺织、造纸工业排水增压以及其他工业增压配套等。产品质量稳定可靠,远销西欧,东南亚,深受广大用户信赖和好评。

    上海上一泵业制造有限公司是中国最大的<a href="http://www.shangyi-pump.com/xf.html">消防泵</a>,<a href="http://www.shangyi-pump.com/pw.html">排污泵</a>,<a href="http://www.shangyi-pump.com/hg.html">化工泵</a>,<a href="http://www.shangyi-pump.com/gm.html">隔膜泵</a>,<a href="http://www.shangyi-pump.com/dj.html">多级泵</a>制造商之一,在离心历史背景制造领域,是专业生产消防泵,排污泵,化工泵,隔膜泵,多级泵、生活消防成套智能控制给水设备及水泵智能电气控制设备的大型股份制企业

    全采建筑工程设计有限公司专业从事<a href="http://www.qc-design.cn">上海室内设计</a>,<a href="http://www.qc-design.cn">上海装潢设计</a>,系专业建筑室内设计机构,着力为社会提供私人住宅上海室内设计、上海装潢设计和施工服务以及公共建筑室内设计和施工服务,“全采设计”吸纳志同道合的设计师加入设计团队,其各怀绝技各有所长,从室内设计之结构、造型、色彩、材质、灯光 、配饰等各个方面给予设计团队最专业的技术支持。

    E-LIKES(依莱特斯)<a href="http://www.e-likes.com/">干洗店</a>、<a href="http://www.e-likes.com/">干洗加盟</a>中心,是源自欧洲概念的先进洗涤服务终端。其清新的形象、专业的服务、专业的干洗加盟,正如其天使般的名字一样,依莱特斯干洗店深受都市人群的喜爱,欢迎加入依莱特斯干洗加盟中心<a href="http://www.shjy.sh.cn/">干洗</a>,<a href="http://www.shjy.sh.cn/ganxijiameng.html">干洗加盟

    上海青浦莲盛专业生产<a href="http://www.915p.com/nijiangbeng.html">泥浆泵</a>,<a href="http://www.915p.com/xiaofangbeng.html">消防泵</a>,严格经过检验的磁力泵产品广泛应用于市政建设、农田水利、火力发电、石油化工、冶金矿山、消防环保、医药等各个领域,公司本着用心制造泥浆泵,消防泵,用情服务为宗旨愿与各界新老朋友携手共进,竭诚合作,共同创造水泵业界新的辉煌,欢迎广大新老客户前来订购泥浆泵,消防泵。

    Welcome to http://www.mygamebuy.com <a href="http://www.mygamebuy.com/">Buy Wow Gold</a>, We will serve you with cheap wow gold , If you want to buy cheap wow gold, please come here , the best price and services are waiting for you Buy Wow Gold.

    上海递玛吉传动机械有限公司是专业开发减速器、销售减速器的厂家, 公司以现代化网络平台和呼叫中心为服务核心,为用户提供高品质的减速器产品与服务保障,
    <a href="http://www.dimaji.com/jsdj.asp">减速电机</a>,<a href="http://www.dimaji.com/cljsj.asp">齿轮减速机</a>,<a href="http://www.dimaji.com/bxzljsj.asp">摆线针轮减速机</a>,<a href="http://www.dimaji.com/wlwgjsj.asp">蜗轮蜗杆减速机</a>,<a href="http://www.dimaji.com/wljsj.asp">蜗轮减速机</a>,<a href="http://www.dimaji.com">减速器</a>,<a href="http://www.dimaji.com/jsdj.asp">sew减速机</a>

    上海岭秦数码科技有限公司成立于1998年,销售各类智能卡(会员卡,<a href="http://www.200100.net/">贵宾卡</a>,IC卡,<a href="http://www.200100.net/">PVC卡</a>,<a href="http://www.200100.net/">IC卡</a>)及其设备的高科技企业。

    14 May

    Trackbacks (1)

    The trackback URL for this entry is:
    http://alarnan.spaces.live.com/blog/cns!819CBC613DE169EF!170.trak
    Weblogs that reference this entry