Differences between revisions 3 and 4
Revision 3 as of 2007-10-11 14:54:41
Size: 15752
Editor: DanielJue
Comment:
Revision 4 as of 2009-09-20 23:20:07
Size: 15752
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

A Sample Application In Tapestry 5.0.5 with Hibernate 3.2.2 ga Spring 2.0

This is a sample Application of Tapestry 5.0.5

Create Vehicle

VehicleId:_ Vehicle Name:_

  • Login

When ever we click on the login button data will be inserted on the Table VECHILE_MASTER

Project Structure:

 src 
     | 
      _  com.btp.vms 
              | 
               _  dao 
                       FMGDatabase.Java 
                       FMGService.Java 
               _  data 
                       Vehicle.java(BO Business Object) 

               _ Pages 
                      AddVehicle.Java 
               _ resources 
                     hibernate.cfg.xml 
               _ services 
                     VehicleModule.java 

Context 
   | 
    _Web-Inf 
          | 
           _ classes 
           _ lib 
           _AddVehicle.html 
           _web.xml 
           _springContext.xml 

we will start with AddVehicle.html form(Simple Html form with TextFeild,TextArea Tags)

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd"> 
    <h1>Create New Vehicle</h1> 
    <style> 
      DIV.t-beaneditor LABEL { 
        width: 200px; 
      } 
    </style> 
                <t:form t:id="addVehicle"> 
                        <div class="t-beaneditor"> 
                      <t:label for="modelName">Model</t:label> 
                      <input t:type="TextField" t:id="modelName" size="20"/> 
                      <br/> 
                      <t:label for="vehicleId">Vehicle Id</t:label> 
                      <input t:type="TextField" t:id="vehicleId" size="20"/> 
                      <br/> 
                      <br/> 
                      <input type="submit" value="AddVehicle"/> 
                    </div> 
            </t:form> 
</html> 

AddVehicle.Java

package com.btp.vms.pages; 

import java.sql.Date; 

import org.apache.tapestry.annotations.Component; 
import org.apache.tapestry.annotations.Inject; 
import org.apache.tapestry.annotations.OnEvent; 
import org.apache.tapestry.annotations.Persist; 
import org.apache.tapestry.annotations.Service; 
import org.apache.tapestry.corelib.components.Form; 

import com.btp.vms.dao.FMGDatabase; 
import com.btp.vms.data.Vehicle; 

/** 
 * 
 * @author sadhana 
 * 
 */ 
public class AddVehicle{ 
        
         @Inject 
         @Service("FMGDatabase") 
         private FMGDatabase  _database; 
          
        @Component(id = "addVehicle") 
    private Form form; 
        
        @Persist 
    private String _modelName; 

    private String _vehicleId; 
    
    private Date _year; 
    
  
    /** 
         * @return Returns the _modelName. 
         */ 
    public String getModelName() 
    { 
        return _modelName; 
    } 

    public void setModelName(String modelName) 
    { 
    _modelName = modelName; 
    } 
    
    /** 
         * @return Returns the _vehicleId. 
         */ 
    public String getVehicleId() 
    { 
        return _vehicleId; 
    } 

    public void setVehicleId(String vehicleId) 
    { 
    _vehicleId = vehicleId; 
    } 

        /** 
         * @return Returns the _year. 
         */ 
        public Date getYear() { 
                return _year; 
        } 

        /** 
         * @param _year The _year to set. 
         */ 
        public void setYear(Date year) { 
                _year = year; 
        } 
    
        @OnEvent(value = "submit",component="addVehicle") 
          void  addVehicle(){ 
                 System.out.println("-----------here:: "+_vehicleId); 
                 System.out.println("-----------here:: "+_modelName); 
                 Vehicle vehicle = new Vehicle(); 
                 vehicle.setVehicleId(_vehicleId); 
                 vehicle.setModelName(_modelName); 
                _database.add(vehicle);     
          } 


} 

Hear we are Using FMGDataBase is the Interface and FMGservice is the service which extends HibernateDaoSupport implements FMGDatabase

// Copyright 2007 The Apache Software Foundation 
// 
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
//     http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License. 

FMGDatabase.java 

package com.btp.vms.dao; 

import com.btp.vms.data.Vehicle; 
/** 
 * 
 * @author sadhana 
 * 
 */ 
public interface FMGDatabase 
{ 
    /** Adds an item to the database, first assigning a unique id to the item. */ 
    void add(Vehicle item); 

} 

FMGService.java

package com.btp.vms.dao; 

import org.hibernate.Session; 
import org.springframework.orm.hibernate3.HibernateCallback; 
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 
import org.springframework.transaction.PlatformTransactionManager; 

import com.btp.vms.data.Vehicle; 

/** 
 * 
 * @author sadhana 
 * 
 */ 

public class FMGService extends HibernateDaoSupport implements FMGDatabase { 

        private PlatformTransactionManager transactionManager; 
            
            public FMGService() 
            { 
                
            } 

            public void add(final Vehicle vehicle) 
            { 
            getHibernateTemplate().execute(new HibernateCallback() { 
            public Object doInHibernate(Session session) 
            throws org.hibernate.HibernateException ,java.sql.SQLException { 
            
            session.saveOrUpdate(vehicle); 
            return vehicle; 
            }; 
            }); 
            
            } 



                /** 
                 * @return Returns the transactionManager. 
                 */ 
                public PlatformTransactionManager getTransactionManager() { 
                        return transactionManager; 
                } 



                /** 
                 * @param transactionManager The transactionManager to set. 
                 */ 
                public void setTransactionManager(PlatformTransactionManager transactionManager) { 
                        this.transactionManager = transactionManager; 
                } 
} 

SpringContext.xml SpringContext.xml tells about datasource specification,configLocation,Bean services

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
        
        
        
        <!-- ========================= RESOURCE DEFINITIONS ========================= --> 
     <bean id="TCMJndiTemplate" class="org.springframework.jndi.JndiTemplate"> 
      
    </bean> 
    <!-- DATASOURCE:BEGIN --> 

    <bean id="dataSource" 
      class="org.springframework.jndi.JndiObjectFactoryBean"> 
      <!-- use "java:comp/env/jdbc/TCMPCDS" in case of tomcat --> 
      <property name="jndiName" value="java:comp/env/jdbc/TCMPCDS"/> 
      <property name="resourceRef" value="false"/> 
      <property name="jndiTemplate"> 
        <ref local="TCMJndiTemplate"/> 
      </property> 
    </bean> 
        <!-- DATASOURCE:END --> 
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource"/> 
        <property name="configLocation"> 
        <value>classpath:/com/btp/vms/resources/hibernate.cfg.xml</value></property> 
                <property name="configurationClass"><value>org.hibernate.cfg.AnnotationConfiguration</value></property>         
    </bean> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
        <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
    
    <bean id="FMGDatabase" class="com.btp.vms.dao.FMGService"> 
       <property name="sessionFactory"><ref local="sessionFactory"/></property> 
       <property name="transactionManager"><ref local="transactionManager"/></property> 
    </bean> 

</beans> 

Hear hibernate.cfg.xml tells about mapping resources

<!DOCTYPE hibernate-configuration PUBLIC 
                "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
    <hibernate-configuration> 
        <session-factory> 
                <property name="show_sql">true</property> 
                <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property> 
                 <mapping resource="/com/btp/vms/data/Vehicle.hbm.xml"/> 
        </session-factory> 
    </hibernate-configuration> 

web.Xml which tells about SpringContext Integration

<?xml version="1.0" encoding="UTF-8"?> 
<!-- 
   Copyright 2007 The Apache Software Foundation 

   Licensed under the Apache License, Version 2.0 (the "License"); 
   you may not use this file except in compliance with the License. 
   You may obtain a copy of the License at 

       http://www.apache.org/licenses/LICENSE-2.0 

   Unless required by applicable law or agreed to in writing, software 
   distributed under the License is distributed on an "AS IS" BASIS, 
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
   See the License for the specific language governing permissions and 
   limitations under the License. 
--> 

<!DOCTYPE web-app 
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
      "http://java.sun.com/dtd/web-app_2_3.dtd"> 
      <web-app> 
          <display-name>Tapestry 5 Tutorial #1</display-name> 
          <context-param> 
              <!-- The only significant configuration for Tapestry 5, this informs Tapestry 
                   of where to look for pages, components and mixins. --> 
              <param-name>tapestry.app-package</param-name> 
              <param-value>com.btp.vms</param-value> 
          </context-param> 
          <filter> 
              <filter-name>vehicle</filter-name> 
              <filter-class>org.apache.tapestry.spring.TapestrySpringFilter</filter-class> 
          </filter> 
          <filter-mapping> 
              <filter-name>vehicle</filter-name> 
              <url-pattern>/*</url-pattern> 
          </filter-mapping> 
        <context-param> 
                <param-name>contextConfigLocation</param-name> 
                <param-value> /WEB-INF/springContext.xml 
                </param-value> 
       </context-param> 
        
        <!-- Listeners --> 
            <listener> 
               <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
            </listener> 
            
      </web-app> 

Vehicle.java(BO) here Iam using Xdoclate mappings in BO

package com.btp.vms.data; 

import java.io.Serializable; 
import java.util.Date; 
/** 
 * @hibernate.class table = "VECHILE_MASTER" lazy = "false" 
 * 
 */ 

public class Vehicle implements Serializable { 
        
        private String vehicleId; 
        
        private String modelName; 
        
        private Date year; 

        /** 
         * @hibernate.property column = "VECHILE_MODEL" 
         */ 
        public String getModelName() { 
                return modelName; 
        } 

        /** 
         * @param model The model to set. 
         */ 
        public void setModelName(String modelName) { 
                this.modelName = modelName; 
        } 

         /** 
         * @hibernate.id column = "VECHILE_ID" 
         * 
         */ 
        public String getVehicleId() { 
                return vehicleId; 
        } 

        /** 
         * @param vehicleId The vehicleId to set. 
         */ 
        public void setVehicleId(String vehicleId) { 
                this.vehicleId = vehicleId; 
        } 

        

} 

If u want you can write ant build script to hbm.build.xml

<?xml version="1.0"?> 
<project name="fmg" default="build-mappings"> 

        <property name="context" value="${basedir}/context" /> 
        <property name="webinf" value="${context}/WEB-INF" /> 
        <property name="webinf.lib" value="${webinf}/lib" /> 
        <property name="src.dir" value="${basedir}/src" /> 
    <!-- called proj.* to avoid conflict when called from pepsibuild.xml --> 
        <property name="proj.lib.dir" value="${basedir}/lib" /> 

        <path id="class.path"> 
                <fileset dir="${proj.lib.dir}"> 
                </fileset> 
                <fileset dir="${tomcat.home}/common/lib"> 
                </fileset> 
                <fileset dir="${tomcat.home}/server/lib" /> 
                <fileset dir="${java.home}/../lib" includes="tools.jar"/> 
        </path> 

        <target name="build-mappings" > 
        <echo message="basedir is ${basedir}"/> 
        <echo message="libdir is ${proj.lib.dir}"/> 
                <path id="allcontextjars"> 
                                        <fileset dir="${webinf.lib}"> 
                                                <include name="*.jar"/> 
                                        </fileset> 
                    <fileset dir="${proj.lib.dir}"> 
                                                <include name="*.jar"/> 
                                        </fileset> 
                                </path> 
                <taskdef name="hibernatedoclet" 
                            classname="xdoclet.modules.hibernate.HibernateDocletTask" 
                                        classpathref="allcontextjars" 
                            /> 
                <hibernatedoclet 
                            destdir="${webinf}/classes" 
                            verbose="true"> 

                            <fileset dir="${src.dir}"> 
                            <include name="**/*.java"/> 
                            <exclude name="**/VehicleModule.java"/> 
                            
                            </fileset> 

                            <hibernate version="3.0"/> 
                        </hibernatedoclet> 
        </target> 

</project> 

You can write your own project.build.xml

Jars You can add in lib folder

All tapestry5 jars(Tapestry.org) site hibernate-annotations.jar hibernate-commans-annotations.jar jta.jar persistence.jar All Xdoclate.jar(If u are using xdoclate mapping) or ejb3-persistence.jar for (If U r using Entity bean mappings) spring.jar sping-hibernate3.jar All commans.


jars

If u fallow all these rules its easy way to Tapestry5 with hibernate3.2.2 ga and spring 2.o If people intrested to run with maven you can install maven plug in eclipse then proceed springContext.xml,hibernate.cfg.xml,web.xml these 3 files are importent to Integrate The application

I hope every one can feel easy to use tapestry5 with hibernate.3.2.2ga and spring 2.0

Thanks Sadhana


CategoryCategory

OnSubmit (last edited 2009-09-20 23:20:07 by localhost)