读书人

ssh调整的三种方式

发布时间: 2012-10-16 09:57:37 作者: rapoo

ssh整合的三种方式

第一种方式:

?

整合方式:
Spring与Struts的整合
Action 继承Spring提供的Action ,该Action父类为Struts的Action,添加了方法getWebApplicationContext()
可以通过此方法获取ApplicationContext对象,从而获取到Bean
Spring与Hibernate的整合
通过动态代理整合HibernateTemplate

实现流程:
1.实现Spring与Hibernate的集成:

public class UserDaoImpl implements UserDao { private HibernateTemplate ht; /**  * 添加用户  */ public boolean addUser(User user) {  try{   ht.save(user);   return true;  }catch(Exception e){   e.printStackTrace();  }  return false; }  /**  * 删除用户  */ public boolean deleteUser(User user) {  try{   ht.delete(user);   return true;  }catch(Exception e){   e.printStackTrace();  }  return false; }}

?

 <!-- 配置数据源的bean --><bean id="datasrc" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/spring"></property> <property name="username" value="root"></property> <property name="password" value=""></property></bean><!-- 配置SessionFactory的Bean --><bean id="sessionfactory" ref="datasrc"></property> <!-- 配置映射文件 --> <property name="mappingResources">  <list>   <value>com/dowebber/entity/user.hbm.xml</value>  </list> </property> <property name="hibernateProperties">  <props>   <prop key="hibernate.show_sql">true</prop>   <prop key="hibernate.format_sql">true</prop>   <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  </props> </property></bean><!-- 配置HibernateTemplate的Bean--><bean id="hibernatetemplate" ref="sessionfactory"></property></bean><!-- 配置接口的实现类的Bean--><bean id="userdao" ref="hibernatetemplate"></property></bean><!-- 配置userdaoimpl的bean --><bean id="userdaoimpl" ref="userdao"></property></bean><!-- 配置事务的bean 使用hibernate的事务类,spring提供 --><bean id="transactionmanager" ref="sessionfactory"></property></bean><!-- 配置代理的bean --><bean id="userdaoproxy" ref="userdaoimpl"></property> <property name="transactionAttributes">  <props>   <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>  </props></property>

?2.整合Spring与Struts
2.1 写Action,继承Spring提供的ActionSupport

import org.springframework.web.struts.MappingDispatchActionSupport;import com.dowebber.entity.User;import com.dowebber.service.dao.UserServiceDao;public class UserAction extends MappingDispatchActionSupport {  public ActionForward login(ActionMapping mapping, ActionForm arg1,   HttpServletRequest request, HttpServletResponse response) throws Exception {  UserServiceDao dao = (UserServiceDao) this.getWebApplicationContext().getBean("userdaoproxy"); }}

?
2.2 在web.xml中配置Spring
?

 <context-param> <param-name>contextConfigLocation</param-name> <param-value>  /WEB-INF/spring.xml </param-value>   </context-param>     <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener>

?Struts的配置不变

应用流程: 请求-->ActionServlet-->构造Action的父类,即Spring提供ActionSupport,构造ApplicationContext->构造bean->执行Action->执行Bean

?

整合方式二:

集成方式
1.Action中把需要的bean设置成成员属性
2.把Action配置成Bean
3.struts的action的type指向Spring的代理类
4.配置struts的loader插件

请求过程 请求-->ActionServlet->Spring代理类-->加载spring配置文件,获取Action的Bean并注入其中的属性-->执行Action中的方法

整合的实现:
Action中添加需要的bean的私有属性并提供setter和getter

public class UserAction extends MappingDispatchAction {  private UserServiceDao dao;  public UserServiceDao getDao() {  return dao; } public void setDao(UserServiceDao dao) {  this.dao = dao; } public ActionForward login(ActionMapping mapping, ActionForm arg1,   HttpServletRequest request, HttpServletResponse response) throws Exception {  }}

?
配置Action的Bean

<!-- Action的bean --><bean name="/user/login" ref="userdaoimpl"></property></bean>

?配置struts的action指向spring的代理类
?

<action-mappings >   <action    path="/user/login"   type="org.springframework.web.struts.DelegatingActionProxy"   parameter="login"   >   <forward name="success" path="/success.jsp"></forward>   <forward name="fail" path="/fail.jsp"></forward>  </action>

?配置struts的插件,插件中加载spring的配置文件

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

?整合方式三:

配置流程:
1.Action中添加需要的bean的私有属性并提供setter和getter
2.spring中配置Action的Bean bean的name属性应该与Action的Path属性一致,不需要提供id
3.在struts配置中,配置controller和plugin,Action的type无需设置代理

实现过程:

1.Action中添加需要的bean的私有属性并提供setter和getter

public class UserAction extends MappingDispatchAction {  private UserServiceDao dao;  public UserServiceDao getDao() {  return dao; } public void setDao(UserServiceDao dao) {  this.dao = dao; } public ActionForward login(ActionMapping mapping, ActionForm arg1,   HttpServletRequest request, HttpServletResponse response) throws Exception { }}

?
2.spring中配置Action的Bean bean的name属性应该与Action的Path属性一致,不需要提供id

<!-- Action的bean --><bean name="/user/login" ref="userdaoimpl"></property></bean>

?

3.在struts配置中,配置controller和plugin,Action的type无需设置代理

 <controller processorvalue="/WEB-INF/spring.xml" /> </plug-in>

?

读书人网 >软件架构设计

热点推荐