Tools
Eclipse IDE
Jboss 5
Create a dynamic web project in eclipse and add following files.
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>StrutsHibernate</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
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.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123</property> <mapping class="codesstore.strutshibernate.domain.Student" /> </session-factory> </hibernate-configuration> <!-- for oracle --> <!-- <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:mydb</property> <property name="hibernate.connection.username">scott</property> <property name="hibernate.connection.password">tiger</property> <mapping class="codesstore.strutshibernate.domain.Student" /> </session-factory> </hibernate-configuration> -->
when I connect to oracle express edition I use connection url, jdbc:oracle:thin:@localhost:1521:xe
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- Configuration for the default package. --> <package name="default" extends="struts-default"> <action name="addStudent" class="codesstore.strutshibernate.action.StudentAction" method="addStudent"> <result name="success">student.jsp</result> <result name="input">student.jsp</result> </action> <action name="listStudents" class="codesstore.strutshibernate.action.StudentAction" method="listStudents"> <result name="success">student.jsp</result> <result name="input">student.jsp</result> </action> </package> </struts>
HibernateUtil.java
package codesstore.strutshibernate.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } catch (Exception ex) { ex.printStackTrace(); throw new ExceptionInInitializerError(); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
Student.java
package codesstore.strutshibernate.domain; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @Table(name = "STUDENT") public class Student implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") private Integer id; @Column(name = "NAME", length = 100) private String username; @Column(name = "EMAIL", length = 100) private String email; @Column(name="ADDED_TIME") @Temporal(TemporalType.DATE) private Date addedTime; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getAddedTime() { return addedTime; } public void setAddedTime(Date addedTime) { this.addedTime = addedTime; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
StudentDao.java
package codesstore.strutshibernate.dao; import java.util.List; import codesstore.strutshibernate.domain.Student; public interface StudentDao { public boolean saveStudent(Student student); public List<Student> listStudents(); }
StudentDaoImpl.java
package codesstore.strutshibernate.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import codesstore.strutshibernate.domain.Student; import codesstore.strutshibernate.util.HibernateUtil; public class StudentDaoImpl implements StudentDao { @Override public boolean saveStudent(Student student) { Session session = null; Transaction transaction = null; try { session = HibernateUtil.getSessionFactory().openSession(); transaction = session.beginTransaction(); session.save(student); transaction.commit(); return true; } catch (Exception e) { if (transaction != null) { transaction.rollback(); } return false; } finally { if (session != null) { session.close(); } } } @Override public List<Student> listStudents() { Session session = null; Transaction transaction = null; try { session = HibernateUtil.getSessionFactory().openSession(); transaction = session.beginTransaction(); Query query = session.createQuery("FROM Student"); List<Student> students = query.list(); transaction.commit(); return students; } catch (Exception e) { e.printStackTrace(); if (transaction != null) { transaction.rollback(); } return null; } finally { if (session != null) { session.close(); } } } }
StudentAction.java
package codesstore.strutshibernate.action; import java.util.Date; import java.util.List; import codesstore.strutshibernate.dao.StudentDao; import codesstore.strutshibernate.dao.StudentDaoImpl; import codesstore.strutshibernate.domain.Student; import com.opensymphony.xwork2.ActionSupport; public class StudentAction extends ActionSupport{ private Student student; private List<Student> students; private StudentDao studentDaoImpl=new StudentDaoImpl(); public String addStudent() { getStudent().setAddedTime(new Date()); boolean isSaved=studentDaoImpl.saveStudent(getStudent()); if(isSaved){ addActionMessage("Successfully added student"); return SUCCESS; }else { addActionError("Adding failed"); return INPUT; } } public String listStudents(){ students = studentDaoImpl.listStudents(); if (students != null) { return SUCCESS; } else { addActionError("Student listing failed"); return INPUT; } } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
student.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib prefix="s" uri="/struts-tags" %> <!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>Stuts 2 Hibernate - codesstore.blogspot.com</title> </head> <body> <s:actionerror/> <s:actionmessage /> <s:form action="addStudent"> <s:textfield name="student.username" label="Username"/> <s:textfield name="student.email" label="Email"/> <s:submit value="Add"/> </s:form> <table> <s:iterator value="students"> <tr> <td><s:property value="username"/></td> <td><s:property value="email"/></td> <td><s:property value="addedTime"/></td> </tr> </s:iterator> </table> </body> </html>
index.jsp
<% response.sendRedirect("listStudents.action"); %>
Project structure in Eclipse.
ojdbc14.jar used to connect to Oracle database
Hi Sameera,
ReplyDeleteI have a project which has both ejb and dynamic web project. I have added the ejb project to the dynamic web project. I have a form where a person fills details which are caught in the action class which then calls the session bean class from ejb, but it its giving that the ejb class is not found. Do we need JNDI properties file? How do i solve this?
SEVERE: Exception starting filter struts2
java.lang.NoClassDefFoundError: com/DAO/AddStudentSessionDAO
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getConstructor(Unknown Source)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:371)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:329)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:429)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:239)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:152)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)
at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:205)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:273)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:254)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:372)
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:98)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4461)
at org.apache.catalina.core.StandardContext$2.call(StandardContext.java:5133)
at org.apache.catalina.core.StandardContext$2.call(StandardContext.java:5128)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.DAO.AddStudentSessionDAO
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
... 26 more
here AddStudentSessionDAO is the local interface for session bean in ejb
Refer this,
Deletehttp://codesstore.blogspot.com/2012/07/ejb3-and-jpa-step-by-step-tutorial.html
nice blog and it is very useful to struts & hibernate learners SRUTS ONLINE TRAINING
ReplyDelete