Tapestry+spring+hibernate基本架构

本文档详细介绍了如何将Tapestry、Spring和Hibernate整合在一起,包括在Web.xml中配置Tapestry的servlet,设置Tapestry的application文件,创建ORM文件和JavaBean,配置Spring的SessionFactory,实现服务接口和实现类,以及添加Spring事务管理。通过这些步骤,你可以建立一个基于Tapestry、Spring和Hibernate的应用程序,并实现数据的CRUD操作及事务处理。
摘要由CSDN通过智能技术生成
  
Tapestry+spring+hibernate
 
1.     配置 tapestry
1)Web.xml 中配置 tapestry’s servlet
 
< servlet >
    < servlet-name > app </ servlet-name >
    < servlet-class > org.apache.tapestry.ApplicationServlet </ servlet-class >
</ servlet >
 
< servlet-mapping >
    < servlet-name > app </ servlet-name >
    < url-pattern > / </ url-pattern >
</ servlet-mapping >
 
Servlet mapping is associate with the file of hivemodule.xml
 
Note:first u couldn’t add the tapestry-spring.jar
 
2)config application file
 
<application >
       <meta key = "org.apache.tapestry.messages-encoding" value = "UTF-8" />
//encoding for tapestry
        <meta key = "org.apache.tapestry.page-class-packages" value = "tapestry" />
//html file is associate with java file in the “tapestry” package
</application>
 
And the application file name must be defined the same as the servlet-class name.if u dun do that,u must declare in the application file like this “<application name=’project name’>”
 
3)write your html file and java file
 
Home.html:
<input jwcid=”@insert” value=”ongl:name”>
Home.java:(must extends BasePage)
        Public absact String getName();
        Public abstact void setName(String name);
Note:absact is better for the project.
 
Now u can run and see!
2.    hibernate+spring configuration
1)write a orm file---User.hbm.xml whatever u like(include a PK and name/pwd property).and also do a javabean for it.named User.java in domain package.
 
2)First add tapestry-spring.jar,and add under context into web.xml:
 
< listener >
       < listener-class >
           org.springframework.web.context.ContextLoaderListener
       </ listener-class >
    </ listener >
    < context-param >
       < param-name > contextConfigLocation </ param-name >
       < param-value > /WEB-INF/application.xml </ param-value >
</ context-param >
 
3)Then create application.xml file:
 
< bean id = "sessionFactory"
class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
           < property name = "mappingResources" >
              < list >
                  < value > hibernate/User.hbm.xml </ value >
              </ list >
           </ property >
           < property name = "hibernateProperties" >
              < props >
                  < prop key = "hibernate.connection.driver_class" >
                     oracle.jdbc.driver.OracleDriver
                  </ prop >
//for oracle driver
                  < prop key = "hibernate.connection.url" >
                     jdbc:oracle:thin:@localhost:1521:jseps
                  </ prop >
                  < prop key = "hibernate.connection.username" > web </ prop >
                  < prop key = "hibernate.connection.password" > web </ prop >
                  < prop key = "hibernate.dialect" >
                     org.hibernate.dialect.Oracle9Dialect
                  </ prop >
                  < prop key = "hibernate.show_sql" > true </ prop >
                  < prop key = "hibernate.hbm2ddl.auto" > update </ prop >
//is the key word for auto create table in database by itself.
              </ props >
           </ property >
    </ bean >
 
4)and u can create a service interface and impl class files,one is in service.interface package and another in service.impl package.
 
public interface UserService {
    void create(User user);
 
    void update(User user);
 
    void delete(User user);
 
    Collection query();
 
    User load(Long id);
}
 
And UserServiceImpl.java must inject sessionFactory
 
public class UserServiceImpl implements UserService {
   
        private SessionFactory sessionFactory ;
   
        public void setSessionFactory(SessionFactory sessionFactory) {
           this . sessionFactory = sessionFactory;
        }
 
        public void create(User user) {
HibernateTemplate ht = new HibernateTemplate( this . sessionFactory );
           ht.save(user);
    }
……………………………………………………….
}
 
Now u must add these into application.xml
 
< bean id = "userService" class="service.impl.UserServiceImpl">
    < property name = "sessionFactory ref = "sessionFactory" >
</ property >
</ bean >
 
5) add a table on html
< table cellpadding = "0" cellspacing = "0" class = "grid" width = "100%"
    jwcid = "tableUser" >
        < tr jwcid = "@contrib:TableRows" >
           < td jwcid = "@contrib:TableValues" />
        </ tr >
</ table >
 
Then Home.java will change:
 
@InjectObject ( "spring:userService" )
    public abstract UserManager getUserManager();
//inject userService
 
@Component (id = "tableUser" , type = "contrib:Table" , bindings = { "columns=literal:!name,!pwd" , "row=currUser" , "source=userList" })
    public abstract IComponent getTableUser();
//for the tableUser component,see more on “http://tapestry.apache.org/tapestry4”
 
    public Collection getUserList(){
       return this .getUserManager().query();
    }
    //for source
 
    public abstract User getCurrUser();
public abstract void  setCurrUser(User user);
. . . . . . . . . . . . .
 
To make use of this library, you must include its JAR on the classpath (typically, by copying it into WEB-INF/lib), and extend your application specification, and add a <library> element :
<library id="contrib" specification-path="classpath:/org/apache/tapestry/contrib/
Contrib.library"/>
 
    Now u can add some data in database,and run . . .see the list loaded from database!
3.    spring 事务
define a form in new page---create.html
 
< form jwcid = "@Form" >
        NAME: < input jwcid = "@TextField" value = "ognl:user.name" />
    PWD: < input jwcid = "@TextField" value = "ognl:user.pwd" hidden = "true" />
        < input jwcid = "save" value = "save" />
</ form >
 
Add a method in create.java
 
public String save(){
       getUserManager().create(getUser());
       return "Home" ;
}
//can return a string type
 
And add these in application.xml:
 
< bean id = "txMgr" class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
         < property name = "sessionFactory" >
            < ref bean = "sessionFactory" />
        </ property >
    </ bean >
   
    <!-- the txInterceptor bean will get auto-wrapped around all beans matched with the combination of "txAdvisor"+"txAutoProxyCreator" -->
    < bean id = "txInterceptor" class = "org.springframework.transaction.interceptor.TransactionInterceptor" >
        < property name = "transactionManager" >
            < ref bean = "txMgr" />
        </ property >
        < property name = "transactionAttributes" >
             < props >
                < prop key = "*" > PROPAGATION_REQUIRED </ prop >
            </ props >
        </ property >
    </ bean >
 
    <!-- the txAdvisor advises all matched methods in a given bean against its patterns with its interceptor -->
    < bean id = "txAdvisor" class = "org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
        < property name = "advice" >
            < ref bean = "txInterceptor" />
        </ property >
        < property name = "patterns" >
            <!-- the patterns must match *fully qualified* method names, hence the .* -->
            < list >
                < value > .* </ value >
<!—- .* 的方法包括在 TRANSACTION -->
            </ list >
        </ property >
    </ bean >
 
    <!-- the txProxyAutoCreator automagically proxies all matched beans with its interceptors if they match any pointcuts -->
    < bean id = "txProxyAutoCreator" class = "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
        < property name = "beanNames" >
            < list >
                <!-- managers -->
                < value > *Manager </ value >
                < value > *Service </ value >
<!—- 与以上命名的类文件匹配 -->
             </ list >
        </ property >
        < property name = "interceptorNames" >
            < list >
                < value > txAdvisor </ value >
            </ list >
        </ property >
    </ bean >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值