Integrating Webwork, Hibernate and Velocity
We will write a webwork application to access database using hibernate and display the result using velocity ( i.e Integrating Webwork – Hibernate – Velocity)
Note: Include hibernate3 jar and webwork jar in classpath.
We will make application which will take name of employee from user and display all the data about that employee from database.
To use hibernate, we need to define hibernate.cfg.xml file, pojo class for employee.
Employee.java (we use hibernate annotations – latest one)
@Entity
Public class Employee{
@Id
Private long id;
Private String name;
Private String company;
Private String address;
getter and setter methods ………………….
}
hibernate.cfg.xml file (WEB-INF/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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/empdb</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--<property name="hbm2ddl.auto">create</property>-->
<mapping class="Employee"/>
</session-factory>
</hibernate-configuration>
Now we need to define web.xml
Add the below webwork filter to web.xml
web.xml file
<?xml version="1.0"?>
<!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>My WebWork Application</display-name>
<filter>
<filter-name>webwork</filter-name>
<filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
Now we will add our helloWorld action to xwork.xml
xwork.xml
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
<!-- Include webwork defaults (from WebWork JAR). -->
<include file="webwork-default.xml" />
<!-- Configuration for the default package. -->
<package name="default" extends="webwork-default">
<default-interceptor-ref name="basicStack"/>
<action name="findEmployee" class="action.FindEmployeeAction">
<result name=”success” type=”velocity”>employeeDetail.vm</result>
<action>
</package>
</xwork>
Now we will create a form from which the user can submit the employee name.
userForm.html
<html>
<body>
<form name=”myform” action=”findemployee.action”>
Employee Name: <input type=”text” name=”empName” id=”empName”>
<input type=”submit” value=”Find”>
</form>
</body>
</html>
Now we need to define our userDetail.vm page
userDetail.vm
<html>
<body>
Name : $emp.name<br>
Company : $emp.company<br>
Address : $emp.address<br>
</body>
</html>
Now we define our action class FindEmployee
FindEmployeeAction.java
package action;
import com.opensymphony.xwork.ActionSupport;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
private static SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
public class HelloWorldAction extends ActionSupport{
private Employee emp;
private String empName; //should be same as one define in userForm.html.
getter and setter method for emp…………………………… (used by userDetail.vm)
getter and setter method for empName…………………..(used by userForm.html to set empName)
public String execute() {
Session session=sessionFactory.getSession();
this.emp=Session.createQuery(“from Employee where name=’+empName+”’”).list().get(0);
session.flush();
session.close();
return SUCCESS;
}
}
This is all to be done to integrate webwork with hibernate and velocity.Run the application server.http://localhost:8080/MyWebapp/userForm.html