Component Roles in the SSH Stack
Struts2 – Acts as the web layer controller. It routes HTTP requests to appropriate business logic via its central servlet (ActionServlet) and configuration defined in struts.xml.
Spring – Functions as the integration backbone using Inversion of Control (IoC) to decouple components, enabling seamless collaboration among the three frameworks.
Hibernate – Serves as the persistence layer. It maps relational database schemas to object-oriented models, allowing developers to manipulate data using objects instead of SQL.
Framework Versions Used
- Struts2: 2.3.15.3
- Spring: 3.2.0
- Hibernate: 3.6.10
Required Libraries
Struts2 Dependencies
- All JARs from
struts-2.3.15.3/apps/struts-blank/WEB-INF/lib struts2-convention-plugin-2.3.15.3.jarfor annotation-based actionsstruts2-spring-plugin-2.3.15.3.jarfor Spring integration
Hibernaet Dependencies
hibernate3.jarfrom root distribution- All JARs in
hibernate/lib/required hibernate-entitymanager-2.0.Final.jar- Logging bridge:
slf4j-log4j12-1.7.5.jar - MySQL JDBC driver:
mysql-connector-java-5.1.25-bin.jar
Spring Dependencies
Core IoC:
spring-beans-3.2.5.RELEASE.jarspring-context-3.2.5.RELEASE.jarspring-core-3.2.5.RELEASE.jarspring-expression-3.2.5.RELEASE.jarcom.springsource.org.apache.log4j-1.2.15.jarcom.springsource.org.apache.commons.logging-1.1.1.jar
AOP and Supporting Modules:
spring-aop-3.2.5.RELEASE.jarspring-aspects-3.2.5.RELEASE.jarcom.springsource.org.aopalliance-1.0.0.jarcom.springsource.org.aspectj.weaver-1.6.8.RELEASE.jarspring-jdbc-3.2.5.RELEASE.jarspring-test-3.2.5.RELEASE.jarspring-web-3.2.5.RELEASE.jarspring-orm-3.2.5.RELEASE.jarspring-tx-3.2.5.RELEASE.jar
Configuration Files
Struts2 Setup
web.xml
<filter>
<filter-name>strutsDispatcher</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>strutsDispatcher</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<struts>
<!-- Action mappings go here -->
</struts>
Hibernate Setup
hibernate.cfg.xmlcan be omitted if configuration is merged into Spring config.- Mapping files:
*.hbm.xmlplaced alongside entity classes.
Spring Setup
web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:appContext.xml</param-value>
</context-param>
appContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:dbconfig.properties"/>
<bean id="connPool" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.driver}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="user" value="${db.user}"/>
<property name="password" value="${db.pass}"/>
</bean>
<bean id="hibSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="connPool"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/example/model/ItemEntity.hbm.xml</value>
</list>
</property>
</bean>
<bean id="itemAction" class="com.example.controller.ItemController">
<property name="itemSvc" ref="itemSvc"/>
</bean>
<bean id="itemSvc" class="com.example.service.ItemService">
<property name="itemDao" ref="itemDao"/>
</bean>
<bean id="itemDao" class="com.example.dao.ItemDao">
<property name="sessionFactory" ref="hibSessionFactory"/>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibSessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
Package Structure and Entity Definition
Entity Class
package com.example.model;
public class ItemEntity implements java.io.Serializable {
private int itemId;
private String name;
private double cost;
public int getItemId() { return itemId; }
public void setItemId(int itemId) { this.itemId = itemId; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getCost() { return cost; }
public void setCost(double cost) { this.cost = cost; }
}
Example Flow: Adding an Item via Struts2 and Spring
Input Form (JSP)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/struts-tags" prefix="sx" %>
<html>
<head><title>Add Item</title></head>
<body>
<h2>Create New Item</h2>
<sx:form action="item_add" method="post" theme="simple">
| Name | <textfield name="name"></textfield> |
|---|---|
| Cost | <textfield name="cost"></textfield> |
| <submit value="Submit"></submit> |
</sx:form>
</body>
</html>
**Request Path:**Browser form → mapped in struts.xml to ItemController.add() → injected ItemService → delegated to ItemDao → persisted via Hibernate session.
Struts2 Action Mapping
<package name="webPkg" extends="struts-default">
<action name="item_*" class="itemAction" method="{1}">
<result name="ok">/index.jsp</result>
</action>
</package>
ItemController Implementation
package com.example.controller;
import com.opensymphony.xwork2.*;
import com.example.model.ItemEntity;
import com.example.service.ItemService;
public class ItemController extends ActionSupport implements ModelDriven<ItemEntity> {
private ItemEntity model = new ItemEntity();
public ItemEntity getModel() { return model; }
private ItemService itemSvc;
public void setItemSvc(ItemService svc) { this.itemSvc = svc; }
public String add() {
System.out.println("Adding item...");
itemSvc.save(model);
return "ok";
}
}
Service Layer
package com.example.service;
import com.example.model.ItemEntity;
import com.example.dao.ItemDao;
public class ItemService {
private ItemDao dao;
public void setDao(ItemDao d) { dao = d; }
public void save(ItemEntity e) {
dao.persist(e);
System.out.println("Service saved item.");
}
}
DAO Layer
package com.example.dao;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.example.model.ItemEntity;
public class ItemDao extends HibernateDaoSupport {
public void persist(ItemEntity e) {
System.out.println("DAO persisting item.");
getHibernateTemplate().saveOrUpdate(e);
}
}
Integrating Spring and Hibernate
Database Properties (dbconfig.properties)
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/demoDb
db.user=root
db.pass=secret
Hibernate Mapping (ItemEntity.hbm.xml)
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping>
<class name="com.example.model.ItemEntity" table="item_table">
<id name="itemId" column="id">
<generator class="native"/>
</id>
<property name="name" column="item_name"/>
<property name="cost" column="item_cost"/>
</class>
</hibernate-mapping>
With hibernate.hbm2ddl.auto set to update, the schema synchronizes automatically on application startup.