Struts2 Spring Hibernate Integration Example

:
This example shows how to integrate Struts2 with Spring and Hibernate . User is shown a web page where he enters his details and clicks on the submit button. He is shown a list of all saved users in another page. See figures below. First page : User enters his details and clicks on submit button.

Second page : Here he is shown list of saved users.

Project Structure :

sun. This filter processes all the incoming requests and looks for Struts 2 actions for processing the requests.sun. <?xml version="1.sun.w3.dispatcher.org/2001/XMLSchema-instance" xmlns="http:// java.xml : In a Struts 2 application. the most important element in deployment descriptor is the filter and filter-mapping which configure the Struts 2 FilterDispatcher.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.com/xml/ns/javaee http:// java.struts2.sun.com/xml/ns/javaee/webapp_2_5. Basically this servlet filter StrutsPrepareAndExecuteFilter is the implementation of Struts 2 Framework. URL pattern '/*' is maaped with this filter .ng.com/xml/ns/javaee" xmlns:web="http://java.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.apache.web.StrutsPrepareAndExecuteFilter</filterclass> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> .filter.xsd" id="WebApp_ID" version="2.5"> <display-name>Struts2Example14</display-name> <filter> <filter-name>struts2</filter-name> <filterclass>org.

jsp</welcome-file> </welcome-file-list> </web-app> index.xml : All action mappings are defined in struts.e com.Actual class name is given in spring configuration file applicationContext. This value is used by Struts2 filter to find the action class to process this request.jsp’ is called by the action calss.springframework. The ‘result’ tag declared here specifies the view that can be called by the enclosing action.</h2> <s:form action="customerAction" > <s:textfield name="custName" label="Name " value=""/> <s:textarea name="address" label="Address " value="" cols="30" rows="5" /> <s:radio name="gender" label="Gender " list="{'Male'.In our example String “successView” is returned so the corresponding ‘success. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.web.jsp : This is the index page shown to user where he enters his details and submits the request.'Car'.0//EN" . Here ‘customerAction’ is the value of ‘action’ attribute of ‘s:form’ tag.xml) is same as the action attribute’s value (action="customerAction" in index.context.'Female'}" /> <s:select name="item" list="{'Laptop'. Multiple ‘result’ tags can be declared within an action but which view will be called by the action is decided by the String value returned from execute() method defined in the action class.xml within ‘struts’ root tag. <%@ page contentType="text/html.'Phone'}" headerKey="" headerValue="Select-Item" label="Select an Item " /> <s:submit name="submit"/> </s:form> </body> </html> struts. charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h2>Enter your details.xml</param-value> </context-param> <listener> <listener-class>org.xml.thecafetechno.jsp) . Here we have defined one action ‘customerActionBean’ which is mapped with request generated at index.<param-name>contextConfigLocation</param-name> <param-value>WEB-INF/applicationContext.ContextLoaderListener</ listener-class> </listener> <welcome-file-list> <welcome-file>index. Requests are mapped using name attribute of ‘action’ tag.CustomerAction).jsp because name attribute’s value (name="customerAction" in struts. Note that here we have given bean name (customerActionBean) of action class not the actual action class name (i.

auto=update hibernate.hbm.mysql.commons.apache.Driver"/> <property name="url" value="jdbc:mysql://localhost/devdb"/> <property name="username" value="dev"/> <property name="password" value="dev"/> </bean> <bean id="sessionFactoryBean" class="org.springframework.CustomerAction" > .0" encoding="UTF-8"?> <beans xmlns="http://www.HibernateTemplate" > <property name="sessionFactory"> <ref bean="sessionFactoryBean"/> </property> </bean> <bean id="customerDAOBean" class="dao.springframework.springframework. <?xml version="1.apache.orm.5.dialect.dtd"> <struts> <package name="myPack" extends="struts-default"> <action name="customerAction" class="customerActionBean"> <result name="successView">/success.org/dtds/struts-2.dbcp.MySQLDialect hibernate.orm.0.springframework.xml</value> </list> </property> <property name="hibernateProperties"> <value>hibernate.jsp</result> </action> </package> </struts> applicationContext.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jdbc.org/schema/beans" xmlns:xsi="http://www.org/schema/beans http://www.xsd" > <bean id="dataSourceBean" class="org.show_sql=true </value> </property> </bean> <bean id="hibernateTemplateBean" class="org.hibernate3.hbm2ddl.BasicDataSource" > <property name="driverClassName" value="com.CustomerDAO" > <property name="hibernateTemplate" ref="hibernateTemplateBean" /> </bean> <bean id="customerActionBean" class="com.w3.org/schema/beans/spring-beans-2.hibernate3."http://struts.LocalSessionFactoryBean" > <property name="dataSource" ref="dataSourceBean"/> <property name="mappingResources"> <list> <value>model/Customer.dialect=org.xml : It is the Spring configuration file where all the beans are defined.hibernate.thecafetechno.springframework.

saveOrUpdate(customer).java : Struts action class extends ActionSupport class.CustomerDAO. import com. List<Customer> savedCustomerList = new ArrayList<Customer>(). public class CustomerAction extends ActionSupport implements ModelDriven<Customer> { private Customer customer=new Customer(). return "successView". private CustomerDAO customerDAO. } public String execute() throws Exception { customerDAO.util. } public void setSavedCustomerList(List<Customer> savedCustomerList) { this. import dao. savedCustomerList=customerDAO.savedCustomerList = savedCustomerList. Another property ‘ customerDAO’ is injected by spring.xwork2.util.fetchAllCustomer().opensymphony. In this action class an object ‘customer’ (Pojo) is created which is automatically set by Struts2 framework since its (customer) properties name matches with the request parameters specified in index. import com. import model.jsp.ModelDriven. } public List<Customer> getSavedCustomerList() { return savedCustomerList. } public void setCustomer(Customer customer) { this.opensymphony.customer = customer.customerDAO = customerDAO. .<property name="customerDAO" ref="customerDAOBean" /> </bean> </beans> CustomerAction.Customer.thecafetechno.Here execute() method returns a string value which will be used by the action class to select a view to display. } public Customer getCustomer() { return customer. import java.List.xwork2.ArrayList.ActionSupport. [code lang=java] package com. public Customer getModel() { return customer. import java. } public void setCustomerDAO(CustomerDAO customerDAO) { this.

jsp : [code lang=text] <%@ page contentType="text/html.orm. public class CustomerDAO { private HibernateTemplate hibernateTemplate.} } success.!!! </h2> <table border="1px" > <tr> <th>Name</th> <th>Address</th> <th>Gender</th> <th>Item</th> </tr> <s:iterator value="savedCustomerList"> <tr> <td><s:property value="custName" /></td> <td><s:property value="address" /></td> <td><s:property value="gender" /></td> <td><s:property value="item" /></td> </tr> </s:iterator> </table> </body> </html> CustomerDAO..util.hibernate3. import model.HibernateTemplate. } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.Customer.hibernateTemplate = hibernateTemplate. public HibernateTemplate getHibernateTemplate() { return hibernateTemplate. import java. import org.java : package dao..List. } public void saveOrUpdate(Customer customer) { . charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h2>Customer Saved Successfully.springframework.

loadAll(Customer. } } Customer. private String custName. } public void setId(long id) { this.class). } public String getAddress() { return address. private String item. } } Customer. } public void setCustName(String custName) { this. public long getId() { return id. } public void setAddress(String address) { this.hibernateTemplate.save(customer).address = address. } public String getCustName() { return custName.hbm. } public void setGender(String gender) { this. private String address.item = item.gender = gender. private String gender.id = id.java : package model.custName = custName. } public void setItem(String item) { this. } public List<Customer> fetchAllCustomer() { return hibernateTemplate. } public String getItem() { return item. public class Customer { private long id. } public String getGender() { return gender.xml : .

net/hibernate-mapping-3.dtd"> <hibernate-mapping> <class name="model.<?xml version="1.0//EN" "http://hibernate.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.Customer" table="CUSTOMER"> <id name="id" column="Id"> <generator class="native"> </generator> </id> <property name="custName" /> <property name="address" /> <property name="gender" /> <property name="item" /> </class> </hibernate-mapping> .sourceforge.0.