Integration Struts with Spring Hibernate J2EE

Integrating Struts Spring Hibernate


Follow the below steps to integrate.
Step 1:
In the struts-config.xml add plugin below code

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml"/>
</plug-in>


Step 2.
In the applicationContext.xml file
Configure datasourse

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url"><value>jdbc:oracle:thin:@10.10.01.24:1541:ebizd</value>
</property>
<property name="username"><value>sa</value></property>
<property name="password"><value></value></property>
</bean>


Step 3:
Configure SessionFactory

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>com/test/dbxml/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect </prop>
</props>
</property>
</bean>


Step 4:
Configure User.hbm.xml

<hibernate-mapping>
<class name="org.test.model.User" table="app_user">
<id name="id" column="id" >
<generator class="increment"/>
</id>
<property name="firstName" column="first_name" not-null="true"/>
<property name="lastName" column="last_name" not-null="true"/>
</class>
</hibernate-mapping>


Step 5:
In the applicationContext.xml configure for DAO

<bean id="userDAO" class="org.test.dao.hibernate.UserDAOHibernate">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>


Step 6:
DAO Class

public class UserDAOHibernate extends HibernateDaoSupport implements
UserDAO {
private static Log log = LogFactory.getLog(UserDAOHibernate.class);
public List getUsers() {
return getHibernateTemplate().find("from User");
}
public User getUser(Long id) {
return (User) getHibernateTemplate().get(User.class, id);
}
public void saveUser(User user) {
getHibernateTemplate().saveOrUpdate(user);
if (log.isDebugEnabled()) {
log.debug("userId set to: " + user.getId());
}
}
public void removeUser(Long id) {
Object user = getHibernateTemplate().load(User.class, id);
getHibernateTemplate().delete(user);
}
}


Related Topic Struts FAQs
Struts 1 vs Struts2 Difference Performance
What Design Patterns Used in Struts?
Latest Hibernate Interview Questions & Answers
Benefits of an XML Schema

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback