基于SSH实现管理系统之框架整合篇 4 Struts整合Spring

来源:互联网 发布:淘宝蚂蚁花呗激活教程 编辑:程序博客网 时间:2024/05/18 01:31
1、创建页面
新建JSP页面
在页面中引入struts2的标签
<%@ taglib uri="/struts-tags" prefix="s" %>

使用struts的标签:
<s: xxxx />

2、编写Action、Service和Dao的类
Service.java中创建一个Dao的实例,并创建其set()和get()方法
Action继承ActionSupport类,实现ModelDriven<>结构(模型驱动)
创建Service实例,并创建其set()和get()方法
模型驱动:
创建实体类的实例,重写getModel方法,返回值类型为该实体类的类型


3、配置Action、Service和Dao的类
Spring的配置文件applicationContext.xml
<!-- 配置业务层的类-->
<bean id="xxxService" class="com.ssh.service.xxxService">
<property name="xxxDao" ref="xxxDao"/>
</bean>

<!--配置DAO的类-->
<bean id="xxxDao" class="com.ssh.dao.xxxDao">
</bean>

Struts2和Spring整合的两种方式
Action的类由Struts2自身去创建

jsp页面的表单中设置路径
<s:form action="xxxxxx" method="post" namespace="/" theme="simple">
……


在struts.xml配置
<package name="ssh" extends="struts-default" namespace="/">
<action name="xxxxxx_*" class="action的全路径" method="{1}">

</action>
</package>

此时,Dao和Service实现自动注入

Action的类由Spring框架去创建
ApplicationContext.xml中配置Action
<!--配置DAO的类-->
<!--singleton 只有一个实例,也即是单例模式。
prototype访问一次创建一个实例,相当于new。 -->
<bean id="xxxDao" class="com.ssh.dao.xxxDao" scope="prototype">
<!--手动注入Service-->
<property name="xxxService" ref="xxxService"/>
</bean>
此时,手动注入Service和Dao

Action在Struts.xml中配置
<package name="ssh" extends="struts-default" namespace="/">
<action name="xxxxxx_*" class="写ApplicationContext.xml中bean的id" method="{1}">
</action>
</package>

0 0
原创粉丝点击