Sunday, June 17, 2012

ZK with spring hibernate - Anotation based

HI All

1) Create dynamic web project using eclipse IDE ex:- zkspringhibernatetest

2) Jar file those we needed to compile and run zk+spring+hibernate project

























 

placed these jars to WEB-INF/lib directory

3) create web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>zkspringhibernatetest</display-name>
    <!-- spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
   
    <!-- ZK -->
    <listener>
        <description>ZK listener for session cleanup</description>
        <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
    </listener>
    <servlet>
        <description>ZK loader for ZUML pages</description>
        <servlet-name>zkLoader</servlet-name>
        <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
        <init-param>
            <param-name>update-uri</param-name>
            <param-value>/zkau</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup><!-- Must -->
    </servlet>
    <servlet-mapping>
        <servlet-name>zkLoader</servlet-name>
        <url-pattern>*.zul</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>zkLoader</servlet-name>
        <url-pattern>*.zhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>zkLoader</servlet-name>
        <url-pattern>/zk/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <description>The asynchronous update engine for ZK</description>
        <servlet-name>auEngine</servlet-name>
        <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>auEngine</servlet-name>
        <url-pattern>/zkau/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>dspLoader</servlet-name>
        <servlet-class>org.zkoss.web.servlet.dsp.InterpreterServlet</servlet-class>
        <init-param>
            <param-name>class-resource</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dspLoader</servlet-name>
        <url-pattern>*.dsp</url-pattern>
    </servlet-mapping>

    <!-- Hibernate OpenSession Filter -->
    <filter>
        <filter-name>lazyLoadingFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>lazyLoadingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
       
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>index.zul</welcome-file>
    </welcome-file-list>
</web-app>


4) create dao-config.xml (placed this file into the WEB-INF directory)

<?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.5.xsd">

<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>

    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
         <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>

    <property name="annotatedClasses">
    <list>
        <value>com.zkspringhibernatetest.model.Order</value>
        <value>com.zkspringhibernatetest.model.User</value>
    </list>
    </property>

    </bean>
</beans>


5) create datasource.xml (placed this file into the WEB-INF directory)

<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.5.xsd">

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>WEB-INF/database.properties</value>
    </property>
</bean>

<bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>
 
</beans>

6) create database.properties (placed this file into the WEB-INF directory)

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springhibernatetest
jdbc.username=root
jdbc.password=

7) in here I used mysql database

create table scripts

CREATE TABLE  user (
  user_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(10) NOT NULL,
  email VARCHAR(200) NOT NULL,
  user_name VARCHAR(50) NOT NULL,
  password VARCHAR(50) NOT NULL,
  PRIMARY KEY (user_id) USING BTREE
);



CREATE TABLE  orders (
  order_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  order_code VARCHAR(10) NOT NULL,
  order_name VARCHAR(50) NOT NULL,
  order_desc VARCHAR(200) NOT NULL,
  PRIMARY KEY (order_id) USING BTREE
);


Insert  into user (name,email,user_name,password) VALUES ('saneera1', 'saneera@gmail.com','saneera1','test');
Insert  into user (name,email,user_name,password) VALUES ('saneera2', 'saneera@gmail.com','saneera2','test');
Insert  into user (name,email,user_name,password) VALUES ('saneera3', 'saneera@gmail.com','saneera3','test');
Insert  into user (name,email,user_name,password) VALUES ('saneera4', 'saneera@gmail.com','saneera4','test');


Insert  into orders (order_code,order_name,order_desc) VALUES ('AAA', 'Computer','Intel Core I3');
Insert  into orders (order_code,order_name,order_desc) VALUES ('BBB', 'Printer','HP');
Insert  into orders (order_code,order_name,order_desc) VALUES ('CCC', 'Monitor','Samsung LCD');
Insert  into orders (order_code,order_name,order_desc) VALUES ('DDD', 'Extention','Aptella');


8) zk.xml put this file into the WEB-INF directory

<?xml version="1.0" encoding="UTF-8"?>
<zk>   
    <system-config>
        <disable-event-thread/>
    </system-config>       
</zk>


9)Java pacake and class structure




10)  index.zul (which has login details ex: username and password and login button )

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk>
 <window id="loginWindow" title="Spring Hibernate Login" border="normal" width="600px" height="300px"
            apply="com.zkspringhibernatetest.web.LoginController" position="center,center" >
          <grid>
            <columns>
              <column label="" width="70px" align="right" />
              <column label="" align="left" />
            </columns>
            <rows>
              <row>
                <label value="Username:" />
                <textbox id="username" value="" maxlength="12" focus="true" width="100px" />
              </row>
              <row>
                <label value="Password:" />
                <cell>
                  <textbox id="password" value="" maxlength="12"  width="100px" />
                  <space width="10px" height="5px"/>
                </cell>
              </row>
              <row>
                <cell colspan="2" align="center" >
                 <button label="Login" id="login" />
                </cell>
              </row>             
            </rows>
          </grid>
        </window>
</zk>

11)

This is not finished yet if anyone has issues about this wiring please send me an email(saneera@gmail.com) ,becuase i have already done the code 

No comments:

Post a Comment