JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Struts2-JPA Integration

We can integrate struts2 with JPA too.

Example::-
  • The following example show the strut2-JPA integrated Application.
  • The following  Example show all the Employees in the database in a jsp file.
  • We only need persistence.xml file in META-INF folder in src.
  • All Required Jar files for JPA.

index.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<html>

<body>
  <h3>Welcome to Employee Information Page</h3>
  <br>

  <s:form action="showEmp">
    <s:submit value="Show Records" />
  </s:form>
</body>
</html>


struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software
Foundation//DTD Struts
Configuration 2.0//EN" "http://struts.apache.
org/dtds/struts-2.0.dtd">

<struts>
 <package name="default" extends="struts-default">
 
 <action name="showEmp" class="myaction.EmployeeAction">
  <result name="success">/success.jsp</result>
 </action>
 </package>
</struts>


EmployeeAction.java
package myaction;

import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import myDao.*;
import myentity.Employee;

public class EmployeeAction extends ActionSupport
 
implements ModelDriven {

  private Employee emp;
  private List emplist = new ArrayList();
  private EmployeeDaoImp empdao;

  public void setEmpdao(EmployeeDaoImp empdao) {
    this.empdao = empdao;
  }

  @Override
  public Employee getModel() {
    return emp;
  }

  @Override
  public String execute() throws Exception {
    emplist = empdao.showEmployuee();
    return SUCCESS;

  }

  public Employee getEmp() {
    return emp;
  }

  public void setEmp(Employee emp) {
    this.emp = emp;
  }

  public List getEmplist() {
    return emplist;
  }

  public void setEmplist(List emplist) {
    this.emplist = emplist;
  }

}

EmployeeDao.java
package myDao;

import java.util.List;

interface EmployeeDao {

  List showEmployuee();
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns=
"http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
xsi:schemaLocation="http://java.sun.com/xml/ns
/persistence http://java.sun.com/xml/ns/persistence/
persistence_1_0.xsd"
>
  <persistence-unit name="JPAStrutsPU"
transaction-type=
"RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>myentity.Employee</class>
    <properties>
      <property name="hibernate.connection.driver_class"
 
value="com.mysql.jdbc.Driver"/>
      <property name="hibernate.connection.url"
 
value="jdbc:mysql://localhost:3306/test"/>
      <property name="hibernate.connection.username"
value=
"root"/>
      <property name="hibernate.connection.password"
value=
""/>
      
      <property name="hibernate.cache.provider_class"
value="org.hibernate.cache.NoCacheProvider"/>
     
    </properties>
  </persistence-unit>
</persistence>


EmployeeDaoImp.java
package myDao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class EmployeeDaoImp implements EmployeeDao {
ntityManagerFactory fac = null;
      EntityManager em = null;
   

    @Override
    public List showEmployee() {
       
       
          fac = Persistence.createEntityManagerFactory
("JPAStrutsPU",System.getProperties());
                  em = fac.createEntityManager();
        List emp = null;
        try {
           
             emp  = em.createQuery("select object(e)
 from Employee as e").getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return emp;
    }

}

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/w
eb-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/SpringConfiguration.xml
</param-value>

  </context-param>

<filter>
      <filter-name>Struts2Filter</filter-name>
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
</filter-
class>
    </filter>
    <filter-mapping>
      <filter-name>Struts2Filter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
  <welcome-file-list>
    
    <welcome-file>index.jsp</welcome-file>
  
  </welcome-file-list>
</web-app>

success.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<html>

<body>
  <h3>Welcome to Employee Information Page</h3>
  <br>
  <table>
    <tr>
      <th>EMP_ID</th>
      <th>EMP_NAME</th>
      <th>EMP_SAL</th>
    </tr>
    <s:iterator value="emplist">
      <tr>
        <td><s:property value="empid" /></td>
        <td><s:property value="empname" /></td>
        <td><s:property value="salary" /></td>
      </tr>
    </s:iterator>
  </table>

</body>
</html>

Output:




Download Code without lib
Download War with lib