SSH Framework Implementation Guide

SSH Framework Implementation Guide

Project Framework Setup:

  1. Create Control Project

Required JAR Packages:

  • db: Database connection driver
  • hibernate: Hibernate framework libraries
  • jstl: Java Standard Tag Library
  • junit: Testing framework
  • spring: Spring framework libraries
  • struts2: Struts2 framework libraries

Project Layer Structure:

  • com.tech.app.container: Custom Spring container for controller layer
  • com.tech.app.dao: Data Access Object layer for database operations
  • com.tech.app.domain: Entity objects (PO) mapping to database tables
  • com.tech.app.service: Service layer handling business logic
  • com.tech.app.util: Utility classes and methods
  • com.tech.app.control.action: Controller layer for page navigation
  • com.tech.app.control.form: Value objects (VO) for form data
  • junit: Test classes

Configuration Files:

  • beans.xml: Spring configuration file
  • hibernate.cfg.xml: Hibernate configuration file
  • struts.xml: Struts2 configuration file
  1. Persistence Layer Setup

Create entity class ElecText.java in com.tech.app.domain:

public class ElecText implements java.io.Serializable {
    private String textID;
    private String textName;
    private Date textDate;
    private String textRemark;
    // getters and setters
}

Create mapping file ElecText.hbm.xml:

<hibernate-mapping>
    <class name="com.tech.app.domain.ElecText" table="Elec_Text">
        <id name="textID" type="string">
            <column name="textID" sql-type="varchar(50)" not-null="true"/>
            <generator class="uuid"/>
        </id>
        <property name="textName" type="string">
            <column name="textName" sql-type="varchar(50)"/>
        </property>
        <property name="textDate" type="date">
            <column name="textDate" length="50"/>
        </property>
        <property name="textRemark" type="string">
            <column name="textRemark" sql-type="varchar(500)"/>
        </property>
    </class>
</hibernate-mapping>

Create Hibernate configuration file hibernate.cfg.xml:

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/elec</property>
        <property name="hibernate.connection.autocommit">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <mapping resource="com/tech/app/domain/ElecText.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

  1. DAO Layer Implementation

Create ICommonDao interface in com.tech.app.dao:

public interface ICommonDao<T> {
    void save(T entity);
}

Create CommonDaoImpl implementation:

public class CommonDaoImpl<T> extends HibernateDaoSupport implements ICommonDao<T> {
    public void save(T entity) {
        this.getHibernateTemplate().save(entity);
    }
    
    @Resource(name="sessionFactory")
    public void setSessionFactoryDi(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }
}

Create IElecTextDao interface:

public interface IElecTextDao extends ICommonDao<ElecText> {
    String SERVICE_NAME = "com.tech.app.dao.impl.ElecTextDaoImpl";
}

Create ElecTextDaoImpl implementation:

@Repository(IElecTextDao.SERVICE_NAME)
public class ElecTextDaoImpl extends CommonDaoImpl<ElecText> implements IElecTextDao {
    // Implementation is inherited from parent class
}

Configure Spring beans.xml:

<!-- 1: Configure annotation scanning -->
<context:component-scan base-package="com.tech.app"></context:component-scan>
<!-- 2: Configure data source -->

<!-- 3: Create sessionFactory for Spring-Hibernate integration -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
</bean>
<!-- 4: Create transaction manager -->
<bean id="txManage" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 5: Manage transactions using annotations -->
<tx:annotation-driven transaction-manager="txManage"/>

  1. Service Layer Setup

Create IElecTextService interface:

public interface IElecTextService {
    String SERVICE_NAME = "com.tech.app.service.impl.ElecTextServiceImpl";
    void saveElecText(ElecText elecText);
}

Create ElecTextServiceImpl implementation:

@Transactional(readOnly=true)
@Service(IElecTextService.SERVICE_NAME)
public class ElecTextServiceImpl implements IElecTextService {

    @Resource(name=IElecTextDao.SERVICE_NAME)
    private IElecTextDao elecTextDao;

    @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
    public void saveElecText(ElecText elecText){
        elecTextDao.save(elecText);
    }
}

  1. Controller (Action) Layer Setup

Create ElecTextAction:

@SuppressWarnings("serial")
public class ElecTextAction extends BaseAction implements ModelDriven<ElecTextForm>{

    private IElecTextService elecTextService = (IElecTextService)ServiceProvider.getService(IElecTextService.SERVICE_NAME);

    private ElecTextForm elecTextForm = new ElecTextForm();

    public ElecTextForm getModel() {
        return elecTextForm;
    }

    public String save(){
        ElecText elecText = new ElecText();
        elecText.setTextName(elecTextForm.getTextName());
        elecText.setTextDate(DateHelper.stringToDate(elecTextForm.getTextDate()));
        elecText.setTextRemark(elecTextForm.getTextRemark());
        elecTextService.saveElecText(elecText);
        return "save";
    }
}

Create ElecTextForm value object:

public class ElecTextForm implements java.io.Serializable {
    private String textID;
    private String textName;
    private String textDate;
    private String textRemark;
    // getters and setters
}

Create BaseAction class:

@SuppressWarnings("serial")
public class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware {
    protected HttpServletRequest request;
    protected HttpServletResponse response;

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }
}

Create custom Spring container:

public class ServiceProviderContext {
    protected static ApplicationContext ac;
    
    public static void load(String filename){
        ac = new ClassPathXmlApplicationContext(filename);
    }
}

public class ServiceProvider {
    public static ServiceProviderContext spc;
    
    static{
        spc = new ServiceProviderContext();
        spc.load("beans.xml");
    }
    
    public static Object getService(String serviceName){
        if(StringUtils.isBlank(serviceName)){
            throw new RuntimeException("Service name not found");
        }
        
        Object object = null;
        if(spc.ac.containsBean(serviceName)){
            object = spc.ac.getBean(serviceName);
        }
        
        if(object==null){
            throw new RuntimeException("Service node not found for: " + serviceName);
        }
        
        return object;
    }
}

Configure Struts2:

<struts>
    <!-- Configure action extension as .do -->
    <constant name="struts.action.extension" value="do"></constant>
    
    <!-- Enable development mode -->
    <constant name="struts.devMode" value="true"></constant>
    
    <!-- Use simple theme -->
    <constant name="struts.ui.theme" value="simple"></constant>
    
    <package name="system" namespace="/system" extends="struts-default">
        <action name="elecTextAction_*" class="com.tech.app.control.action.ElecTextAction" method="{1}">
            <result name="save">
                /system/textAdd.jsp
            </result>
        </action>
    </package>
</struts>

Add Struts2 filter to web.xml:

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Tags: Struts Spring hibernate java web development

Posted on Fri, 28 Aug 2026 16:51:05 +0000 by zizzy80