spring hibernate struts 整合开发(4) - Struts与Spring集成2

一. spring hibernate struts 整合开发(1) - 搭建环境
二. spring hibernate struts 整合开发(2) - Spring集成的Hibernate编码和测试
三. spring hibernate struts 整合开发(3) - Struts集成Spring
四. spring hibernate struts 整合开发(4) - Struts与Spring集成2
五. spring hibernate struts 整合开发(5) - Hibernate二级缓存
六. spring hibernate struts 整合开发(6) - 额外功能



Java Web应用,最重要的文件是web.xml。Struts需要注册个servlet才可以被调用,spring需要注册个listener才可以被实例化,hibernate配置被集成在spring的配置文件中。

如果把action交给spring管理,我们可以使用依赖注入在action中注入业务层的bean。

1. Struts配置

确保action的path属性值与bean的名称相同。

<!-- the type property could be eliminated, otherwise if we couldn't fetch the action instance from spring container, struts would create the instance of the class represented by the type -->
<action path="/person/list" validate="false">
  ...
</action>


在struts配置文件中添加进spring的请求控制器,该请求控制器会先根据action的path属性值到spring容器中寻找跟该属性值同名的bean。如果寻找到即使用该bean处理用户请求
<controller>
  <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>


2. spring配置

在beans.xml中加入:
<bean name="/person/list" class="com.john.web.action.PersonAction"/>


3. 修改PersonAction.java

public class PersonAction {
  @Resource PersonService personService;
}


访问页面,测试是否显示结果。

整理自:传智播客spring教程

猜你喜欢

转载自czj4451.iteye.com/blog/1535063