Difference between struts and Spring
Struts
|
Spring
|
web framework
|
application framework
|
not layered framework
|
layered framework
|
heavy weight framework
|
Light weight framework
|
Support tag lib.
|
Does not support tag lib.
|
Provide manually code to integrate with ORM
|
xml configuration required
|
AOP,IOC not available
|
AOP,IOC available
|
How
do u integrate struts with spring
1) include the
"struts2-spring-plugin.jar” library into your project classpath.
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.1.8</version>
</dependency>
2) Configure the
Spring listener “org.springframework.web.context.ContextLoaderListener” in
web.xml file.
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
3) Register all the
Spring’s Beans in the applicationContext.xml file, the Spring listener will
locate this xml file automatically.
<bean id="msgBean"
class="example.Success" >
<property
name="message" value="welcome to student
DB"></property>
</bean>
4)Struts.xml
<action
name="showHomePage" class="msgBean">
<result>/example/Login.jsp</result>
</action>
How to handle transaction management in Spring?
ApplicationContext.xml
<bean id="ds"
class="org.sf.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
<property
name="url" value=""/>
<property
name="username" value=""/>
<property
name="password" value=""/>
</bean>
<bean id=”sessionFactory”
class=”org.sf.orm.hibernate3.LocalSessionFactoryBean”>
<property
name=”dataSource” ref=”ds”/>
<property
name=”mappingResources”>
<list>
<value>Account.hbm.xml</value>
</list>
</property>
<property
name=”hibernateProperties” >
<props>
<prop
key=”hibernate.dialect”>OracleDialect</prop>
<prop
key=”show_sql”>true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemp"
class="org.sf.orm.Hibernate3.HibernateTemplate" autowire="constructor"/>
<bean id="accounServiceProxy"
class="org.sf.tx.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"
ref="txManager">
<property
name="target" ref="accountServiceTarget">
<property
name="transactionAttributes">
<props>
<prop
key="addAccount*">PROPGATION_REQUIRED</prop>
<prop
key="deposite*">PROPGATION_REQUIRED</prop>
<prop
key="transferFunds*">PROPGATION_REQUIRES_NEW</prop>
</props>
</property>
</bean>
<bean id=”txManager”
class=”org.sf.orm.hibernate3.HibernateTransactionManager” autowire=”constructor”/>
<bean id="accountServiceTarget" class="AccountServiceImpl"/>
<bean id="accountDAO"
class="AccountDAOImpl"/>
[Note: hibernateTemp internally will use sessionFactory]
AccountDAOImpl.java
Public class AccountDAOImpl implements AccountDAO{
@Autowired
HibernateTemplate
hibernateTemp;
Public void
addAccount(Account acc){
hibernateTemp.save(acc);
}
}
AccountServiceImpl.java
Public class AccountServiceImpl implements AccountService{
@Autowired
Private AccountDAO
accountDAO;
//getters and
setters of accountDAO
Public void
addAccount(Account acc){
accountDAO.addAccount(acc);
}
}
Client.java
AccountService accService = (AccountService)ctx.getBean("accountServiceProxy");
accService.addAccount(acc1);
No comments:
Post a Comment