技术开发 频道

Webwork+Hibrnate开发


【IT168技术文档】 
    三.配置文件 
    1.components.xml
<components> <component> <scope>request</scope> <class>com.sunbay.util.SessionService</class> <enabler>com.sunbay.util.SessionServiceInterface</enabler> </component> <component> <scope>application</scope> <class>com.sunbay.util.SessionFactoryService</class> <enabler>com.sunbay.util.SessionFactoryServiceInterface</enabler> </component> </components>
    只要components.xml放在WEB-INF目录下,并确认webwork-default.xml中有如下声明(当然你也可以定义自己的xml文件,并让xwork.xml包括进来):
<interceptor name="component" class="com.opensymphony.xwork.interceptor.
component.ComponentInterceptor
"/> <interceptor name="hibernate" class="你存放HibernateInterceptor文件的包名.
HibernateInterceptor
"/>
    用到了日志,所以在WEB-INF目录下还要加入log4j.properties文件,该文件在Demo中有,几乎不要改动. 

    3.在xwork.xml中加入对webwork-default.xml的引用. 
    
    到这里我们快速搭建基于Webwork+Hibernate的Web应用已经完成,看看我们编写Action是多么方便吧. 

    在web应用中,一般我们对一个用例建立一个对应的Action,从而是整个应用结构清晰.
public class myAction extends AbstractAction{ public myAction(){} public String go() throws HibernateException{ //取得Session实例 getSession().save(某个对象); rerurn SUCCESS; } }
    每一个Action都必须继承AbstractAction,并覆写go()方法即可.在go()方法中要取得session实例,只要用getSession()方法就可以了.试想我们要是在每一个Action中都载入Hibernate配置文件,生成Session Factory和Session实例是多么烦人啊! 

    要想保存用户信息,只要用set()方法保存,要用时用get()方法取出就可以 

    在wen应用一开始,在xwork.xml文件中由于加入了对webwork-default.xml的引用,所以webwork又找到webwork-default.xml,通过其中的 
    <interceptor name="component" class="com.opensymphony.xwork.interceptor.component.ComponentInterceptor"/>webwork又找到了
components.xml,于是,通用类的四个文件都被加入webwork的视线.拦截器自动调用HibernateSessionFactoryImp.java和HibernateSessionImp.java中的set方法,先将HibernateSessionFactory实例注入到HibernateSession中,然后当在调用Action的候,又将HibernateSession的实例注入到了AbstractAction中,从而使继承AbstractAction的每一个Action实例都有了HibernateSession实例,只要在go()中调用getSession()方法我们就有了session实例!
0
相关文章