Search This Blog

Thursday 18 July 2013

Struts 2 Hibernate and Tiles Integration



Struts 2 Hibernate and Tiles Integration 



Required jar files list
----------------------------

antlr-2.7.7.jar
cglib-2.2.jar
commons-beanutils-1.7.0.jar
commons-collections-3.1.jar
commons-digester-1.8.1.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
freemarker-2.3.16.jar
hibernate-3.2.7.ga.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
javassist-3.7.ga.jar
mysql-connector-java-5.1.9.jar
ognl-3.0.jar
ojdbc14.jar
servlet-api.jar
struts2-convention-plugin-2.2.1.1.jar
struts2-core-2.2.1.1.jar
struts2-tiles-plugin-2.1.6.jar
tiles-api-2.0.6.jar
tiles-core-2.0.6.jar
tiles-jsp-2.0.6.jar
xwork-core-2.2.1.1.jar

struts.xml
-----------
--

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="resource.application" />
    <package name="default" namespace="/" extends="struts-default">
        <result-types>
            <result-type name="tiles"
                class="org.apache.struts2.views.tiles.TilesResult" />
        </result-types>
        <action name="menu" class="com.abc.xyz.actions.StudentAction" method="studentProcess">
            <result name="success" type="tiles">/welcome.tiles</result>
        </action>
        <action name="barrower" class="com.abc.xyz.actions.StudentAction" method="barrower">
            <result name="success" type="tiles">/barrower.tiles</result>
        </action>
        <action name="employment" class="com.abc.xyz.actions.StudentAction" method="employment">
            <result name="success" type="tiles">/employment.tiles</result>
        </action>
    </package>
</struts>

WEB-INF\web.xml
-------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Struts2Day1</display-name>
    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>
    <context-param>
        <param-name>tilesDefinitions</param-name>
        <param-value>/WEB-INF/tiles.xml</param-value>
    </context-param>
    <listener>
    <listener-class>
      com.abc.xyz.listener.HibernateListener
    </listener-class>
  </listener>
   
</web-app>

 WEB-INF\tiles.xml
--------------------------

<?xml version="1.0" encoding="UTF-8" ?>   <!DOCTYPE tiles-definitions PUBLIC        "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"        "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
    <definition name="baseLayout" template="/jsp/baselayout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/jsp/header.jsp" />
        <put-attribute name="landingHeader" value="/jsp/landingpage.jsp"/>
        <put-attribute name="menu" value="/jsp/menu.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/jsp/footer.jsp" />
    </definition>
    <definition name="/welcome.tiles" extends="baseLayout">
        <put-attribute name="title" value="Welcome" />
        <put-attribute name="body" value="/jsp/welcome.jsp" />
    </definition>
    <definition name="/barrower.tiles" extends="baseLayout">
        <put-attribute name="title" value="Welcome" />
        <put-attribute name="body" value="/jsp/borrower.jsp" />
    </definition>
    <definition name="/employment.tiles" extends="baseLayout">
        <put-attribute name="title" value="Welcome" />
        <put-attribute name="body" value="/jsp/employment.jsp" />
    </definition>
</tiles-definitions>

StudentAction.java
-------------------------

package com.abc.xyz.actions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.hibernate.SessionFactory;

import com.abc.xyz.dao.Test;
import com.abc.xyz.listener.HibernateListener;
import com.abc.xyz.service.ProcessService;
import com.abc.xyz.service.ProcessServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

public class StudentAction extends ActionSupport{
    Log LOG = LogFactory.getFactory().getLog("StudentAction");
    List<Test> testList = new ArrayList<Test>();
    /**
     * @return the testList
     */
    public List<Test> getTestList() {
        return testList;
    }


    /**
     * @param testList the testList to set
     */
    public void setTestList(List<Test> testList) {
        this.testList = testList;
    }


    public String studentProcess() {
        LOG.info("StudentAction - studentProcess start");
        SessionFactory sessionFactory =
             (SessionFactory) ServletActionContext.getServletContext()
                    .getAttribute(HibernateListener.KEY_NAME);

        ProcessService auditProcessService = new ProcessServiceImpl();
        testList = auditProcessService.getStudentResults(sessionFactory);
        System.out.println("StudentAction");
        Iterator<Test> it = testList.iterator();
        while (it.hasNext()) {
            Test t = it.next();
            System.out.println(t.getStudentId() + "   " + t.getStudentName());
        }
        LOG.info("StudentAction - studentProcess end");
        return "success";
    }
    public String employment() {
        return "success";
    }
    public String barrower() {
        return "success";
    }
}









Dao.java
-------------
package com.abc.xyz.dao;

import java.util.List;

import org.hibernate.SessionFactory;

public interface Dao {
    public List<Test> getStudentResults(SessionFactory sessionFactory);
    public List getSubProcess();
    public List getLoanProcessData();
}












DaoImpl.java
------------------

package com.abc.xyz.dao;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class DaoImpl implements Dao{
    Log LOG = LogFactory.getFactory().getLog("DaoImpl");
   
    public List<Test> getStudentResults(SessionFactory sessionFactory) {
        LOG.info("getProcessName start");
       
       
        List<Test> studentList = null;
        try {
       
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        studentList = session.createQuery("from Test").list();
        System.out.println("studentList size::::" + studentList.size());
       

        //Query query = entityManager.createQuery(" select customer from Process process");
        //List processList = query.getResultList();
        System.out.println("11111111111111111111111111111111111111111111111111");
        Iterator<Test> it = studentList.iterator();
        while (it.hasNext()) {
            Test t = it.next();
            System.out.println(t.getStudentId() + "   " + t.getStudentName());
        }
        //System.out.println("processList::::" + processList.size());
        //entityTransaction.commit();
        } catch(Exception exception){
            //entityTransaction.rollback();
            exception.printStackTrace();
        }
        finally {
            //entityManager.close();
        }
        LOG.info("getProcessName end");
        return studentList;
    }
    public List getSubProcess() {
        LOG.info("getSubProcess start");
        LOG.info("getSubProcess end");
        return null;
    }
    public List getLoanProcessData() {
        LOG.info("getLoanProcessData start");
        LOG.info("getLoanProcessData end");
        return null;
    }   
}


Test.java
------------
package com.abc.xyz.dao;


public class Test {
   
    private long studentId;
    private String studentName;
   
    /**
     * @return the studentId
     */
    public long getStudentId() {
        return studentId;
    }
    /**
     * @param studentId the studentId to set
     */
    public void setStudentId(long studentId) {
        this.studentId = studentId;
    }
   
    /**
     * @return the studentName
     */
    public String getStudentName() {
        return studentName;
    }
    /**
     * @param studentName the studentName to set
     */
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
   
}

com\abc\xyz\dao\Test.hbm.xml
----------------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.abc.xyz.dao.Test" table="TEST">

        <id name="studentId" type="java.lang.Long">
            <column name="student_id" />
            <generator class="identity" />
        </id>
        <property name="studentName" type="string">
            <column name="student_name" length="50" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

HibernateListener.java
------------------------------






package com.abc.xyz.listener;

import java.net.URL;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateListener  implements ServletContextListener {
    private Configuration config;
    private SessionFactory factory;
    private String path = "hibernate.cfg.xml";
    private static Class clazz = HibernateListener.class;

    public static final String KEY_NAME = clazz.getName();

    public void contextDestroyed(ServletContextEvent event) {
      //
    }

    public void contextInitialized(ServletContextEvent event) {

     try {
            URL url = HibernateListener.class.getResource(path);
            System.out.println("1111111111111111111111111111111111111");
            config = new Configuration().configure(url);
            System.out.println("222222222222222222222222222222222222" + config);
            factory = config.buildSessionFactory();
            System.out.println("333333333333333333333333333333333333");

            //save the Hibernate session factory into serlvet context
            event.getServletContext().setAttribute(KEY_NAME, factory);
            System.out.println("Hibernate SessionFactory loading sucess");
      } catch (Exception e) {
          e.printStackTrace();
          System.out.println("Hibernate SessionFactory loading failed");
             System.out.println(e.getMessage());
       }
    }

}

com\abc\xyz\listener\hibernate.cfg.xml
--------------------------------------------------


<?xml version="1.0" encoding="utf-8"?>
<!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="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.release_mode">auto</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root1</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student_db</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="use_sql_comments">false</property>
        <mapping resource="com/abc/xyz/dao/Test.hbm.xml" />
    </session-factory>
</hibernate-configuration>

ProcessService.java
---------------------------

package com.abc.xyz.service;

import java.util.List;

import org.hibernate.SessionFactory;

import com.abc.xyz.dao.Test;

public interface ProcessService {
    public List<Test> getStudentResults(SessionFactory sessionFactory);
}

ProcessServiceImpl.java
-------------------------------


package com.abc.xyz.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;

import com.abc.xyz.dao.Dao;
import com.abc.xyz.dao.DaoImpl;
import com.abc.xyz.dao.Test;

public class ProcessServiceImpl implements ProcessService{
    Log LOG = LogFactory.getFactory().getLog("ProcessServiceImpl");
    Dao processDao;
    public List<Test> getStudentResults(SessionFactory sessionFactory) {
        LOG.info("ProcessServiceImpl - getStudentResults start");
        processDao = new DaoImpl();
        processDao.getLoanProcessData();
        //auditProcessDao.getProcessName(sessionFactory);
        processDao.getSubProcess();
        LOG.info("ProcessServiceImpl - getStudentResults end");
        return processDao.getStudentResults(sessionFactory);
    }
}





index.jsp
------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
</head>
<body>
    <center><a href="menu.action">Click here to got to welcome page</a></center>
</body>
</html>
jsp\baselayout.jsp

-----------------------
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" /></title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%" align="center">
    <tr height="15%">
        <td><tiles:insertAttribute name="header" />
        </td>
    </tr>
    <tr height="75%">
    <td>
        <TABLE border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
        <TR>
            <td height="80%" width="20%"><tiles:insertAttribute name="menu" /></td>
            <td width="80%">
                <table>
                    <tr height="34%">
                        <td>
                            <tiles:insertAttribute name="landingHeader" />
                        </td>
                    </tr>
                    <tr  height="66%">
                        <td>
                            <tiles:insertAttribute name="body" />
                        </td>
                    </tr>
                </table>
            </td>
        </TR>
        </TABLE>
    </td>       
    </tr>
    <tr height="10%">
        <td><tiles:insertAttribute name="footer" />
        </td>
    </tr>
</table>
</body>
</html>


jsp\borrower.jsp
---------------------
<center><b>Borrower</b></center>

jsp\employment.jsp
-------------------------
<center><b>Employment</b></center>


jsp\footer.jsp
-----------------

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
 <TR>
    <TD><center>&copy;MPB</center></TD>
 </TR>
</TABLE>


jsp\header.jsp
-----------------

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<table width="100%" height="100%" cellspacing="0" cellpadding="0">
    <tr height="50%">
        <td width="10%">Home</td>
        <td width="75%"><IMG SRC="images/white.jpg" WIDTH="100%"
            HEIGHT="100%" BORDER="0"/>
        </td>
        <td width="15%"><span style="float: right">Sign Out</span></td>
    </tr>
    <TR height="50%">
     <TD colspan="3">
         <TABLE WIDTH="100%"   cellspacing="0" cellpadding="0">
             <TR>
                <TD bgcolor="#EFEDF1">&nbsp;<IMG SRC="images/homebar.jpg"/></TD>
                <TD bgcolor="#EFEDF1" align="right">&nbsp;</TD>
            </TR>
        </TABLE>
    </TD>
    </TR>
</table>

jsp\landingpage.jsp

-------------------------
  <TABLE border="0" cellpadding="1" cellspacing="0" width="100%" height="100%">
  <TR height="15%">
    <TD>
    <b>10000000</b>
    </TD>
  </TR>
  <TR height="40%">
    <TD>
    <TABLE border="1" cellpadding="0" cellspacing="0" width="100%" height="100%">
    <TR height="30%">
        <TD bgcolor="#FF0000"><CENTER><B>Attention</B></CENTER></TD>
    </TR>
    <TR  bgcolor="#FF6699" height="70%">
        <TD>
        <UL>
            <LI>Address is less than two years</LI>
            <LI>Monitoring Information</LI>
        </UL>
        </TD>
    </TR>
    </TABLE>
    </TD>
  </TR>
    <TR height="10%"><TD></TD></TR>
  <TR height="40%">
    <TD>
        <TABLE  border="1" cellpadding="0" cellspacing="0" width="100%" height="100%">
        <TR height="30%" >
            <TD bgcolor="#CCCCCC"><CENTER><A HREF="#"><b>Page I</b></A></CENTER></TD>
            <TD  bgcolor="#CCCCCC"><CENTER><A HREF="#"><b>Page II</b></A></CENTER></TD>
            <TD  bgcolor="#CCCCCC"><CENTER><A HREF="#"><b>Page III</b></A></CENTER></TD>
        </TR>
        <TR height="70%">
            <TD width="30%">
            <TABLE border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
            <TR>
                <TD width="40%"><CENTER><A HREF="#"><b>Property</b></A></CENTER></TD>
                <TD  width="30%"><CENTER><A HREF="<s:url action="barrower"/>" ><b>Borrower</b></A></CENTER></TD>
                <TD  width="30%"><CENTER><A HREF="<s:url action="employment"/>" ><b>Employment</b></A></CENTER></TD>
            </TR>
            </TABLE>
            </TD>
            <TD width="45%">
            <TABLE border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
            <TR>
                <TD  width="18%"><CENTER><A HREF="#"><b>Income</b></A></CENTER></TD>
                <TD  width="32%"><CENTER><A HREF="#"><b>Expense</b></A></CENTER></TD>
                <TD  width="25%"><CENTER><A HREF="#"><b>Assets</b></A></CENTER></TD>
                <TD  width="25%"><CENTER><A HREF="#"><b>Liabilities</b></A></CENTER></TD>
            </TR>
            </TABLE>
            </TD>
            <TD width="25%">
            <TABLE cellpadding="0" cellspacing="0" width="100%" height="100%">
            <TR>
                <TD><CENTER><A HREF="#"><b>Details </b></A></CENTER></TD>
                <TD><CENTER><A HREF="#"><b>Declaration</b></A></CENTER></TD>
            </TR>
            </TABLE>
            </TD>
        </TR>
        </TABLE>
    </TD>
  </TR>
  </TABLE>

jsp\menu.jsp
----------------
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<center>Left Navigation</center>
<!--
<s:a href="customer-form">Customer</s:a>
-->
jsp\welcome.jsp
--------------------
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
  <TABLE border="0" cellpadding="1" cellspacing="0" width="100%" height="100%">
    <TR height="66%"><TD><DIV id="pageData">
    <s:if test="testList.size() > 0">
    <table width="100%" height="100%" border="1" cellpadding="0" cellspacing="2">
    <tr>
        <th>Student Id</th>
        <th>Student Name</th>
    </tr>
    <s:iterator value="testList"  var="student" status="userStatus">
        <tr>
            <td><s:property value="#student.studentId" /></td>
            <td><s:property value="#student.studentName" /></td>
        </tr>
    </s:iterator>
</table>
</s:if>
    </DIV></TD></TR>
  </TABLE>
<!--<h2>Welcome to QA Tool</h2>

<h2>All Students</h2>

<s:if test="testList.size() > 0">
<table border="1" cellpadding="0" cellspacing="2">
    <tr>
        <th>Student Id</th>
        <th>Student Name</th>
    </tr>
    <s:iterator value="testList"  var="student" status="userStatus">
        <tr>
            <td><s:property value="#student.studentId" /></td>
            <td><s:property value="#student.studentName" /></td>
        </tr>
    </s:iterator>
</table>
</s:if>
-->


No comments:

Post a Comment