SSH框架结构配置:
运行原理:
系统加载时对过配置文件中的定义,将spring中定义的bean实例化保存在内存中,页面访问的地址为action形式的,通过struts中的配置寻找相应的action实例,处理数据并返回结果,再由struts根据返回的result跳转到相应的jsp页面。
系统分层:
模型:model
数据库操作:dao 此框架中的dao全交由hibernate处理,以实现与数据库的分离。
业务:service 业务逻辑处理
界面业务:action 与界面进行交互
界面:jsp 界面呈现
1、WEB.XML
设置spring的配置文件路径。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring/applicationContext.xml
</param-value>
</context-param>
加入struts2的入点
<filter>
<filter-name>struts2</filter-name>
<filter-class>
com.iman.bsf.web.common.FilterDispatcher
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>locationGuideProperties</param-name>
<param-value>/WEB-INF/classes/loacationguide.properties</param-value>
</init-param>
</filter>
设置数据库访问
<resource-ref>
<res-ref-name>jdbc/wowgen_ds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
该设置使用的是容器提供的访问,因此在tomcat中需有如下的设置。
{tomcat路径}/conf/Catalina/localhost/wowgen.xml
<Context path="/wowgen" debug="99">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="wowgen_log." suffix=".txt" timestamp="true"/>
<!-- oracle datasource -->
<Resource name="jdbc/wowgen_ds"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="10"
maxActive="100"
maxWait="5000"
username="root"
password="root"
url="jdbc:mysql://192.168.1.12:3306/wowgen?useUnicode=true&characterEncoding=UTF-8"
/>
</Context>
tomcat的lib路径中需要有相应的数据库访问的jar包。
2、spring配置文件
配置datasource
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/comp/env/jdbc/wowgen_ds"/>
<property name="lookupOnStartup" value="false"/>
<property name="cache" value="true"/>
<property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>
配置sessionfactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
destroy-method="destroy">
<property name="dataSource" ref ="dataSource"/>
<!-- property name="lobHandler" ref ="lobHandler"/ -->
<property name="configLocation">
<value type=".Resource">classpath:/hibernate/wowgen.cfg.xml</value>
</property>
</bean>
配置事务
<!-- transactionManager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref ="sessionFactory"/>
</bean>
<!-- Transaction control -->
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.iman..service.*Service*.*(..))" advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="create*"/>
<tx:method name="save*"/>
<tx:method name="add*"/>
<tx:method name="update*"/>
<tx:method name="modify*"/>
<tx:method name="change*"/>
<tx:method name="remove*"/>
<tx:method name="del*"/>
<tx:method name="cancel*"/>
<tx:method name="assign*"/>
<tx:method name="bluk*"/>
<tx:method name="transposeUser"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="load*" read-only="true"/>
<tx:method name="has*" read-only="true"/>
<tx:method name="check*" read-only="true"/>
<tx:method name="isCanAccess" read-only="true"/>
</tx:attributes>
</tx:advice>
此项目还使用了一个封装的数据库操作类baseDao
<!-- baseDao -->
<bean id="baseDao" class="com.iman.common.BaseDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
包含其他的spring配置文件
<import resource="applicationContext-wowgen.xml"/>
<import resource="applicationContext-workflow.xml"/>
<import resource="applicationContext-finance.xml"/>
<import resource="applicationContext-logistics.xml"/>
<import resource="applicationContext-stm.xml"/>
2、struts
通过struts属性文件 struts.properties 里设置 struts.objectFactory = spring ,将struts中访问的类指向spring定义的bean
struts配置示例
<package name="logistics" extends="security" namespace="/logistics">
<action name="FwhOut_*" class="fwhOutAction" method="{1}">
<result name="outok">/page/logistics/store/FwhOutCreate.jsp</result>
<result name="listok">/page/logistics/store/FwhOutList.jsp</result>
<result name="viewok">/page/logistics/store/FwhOutView.jsp</result>
</action>
</package>
package设置action的访问路径“/logistics”,在action里加通配符* 可以访问指定格式中的名称对应的action中的方法,result返回字符串决定在action处理完后跳转的界面,
class指向 spring配置的bean名称。
spring配置片段:
<bean name="fwhOutAction" class="com.iman.logistics.web.action.FwhOutAction" scope="prototype">
<property name="fwhOutService" ref="fwhOutService"></property>
<property name="logisticsService" ref="logisticsService" />
<property name="fwhService" ref="fwhService" />
<property name="fwhsService" ref="fwhsService"></property>
</bean>
示例:
访问路径“/logistics/FwhOut_inputUI.action ”,实际先访问com.iman.logistics.web.action.FwhOutAction中的inputUI方法,该方法在处理完事务后返回字符串"outok",
此时界面将跳转到/page/logistics/store/FwhOutCreate.jsp,注:所有的访问路径均严格区分大小写。
3、hibernate
示例:
model类:
public class FwhOut implements Serializable{
//标识符
private String uid;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
//日期
protected Date fdCreateTime;
public Date getFdCreateTime() {
return fdCreateTime;
}
public void setFdCreateTime(Date fdCreateTime) {
this.fdCreateTime = fdCreateTime;
}
//以其他model为属性
protected Fwh fwh = null;
public Fwh getFwh() {
return fwh;
}
public void setFwh(Fwh fwh) {
this.fwh = fwh;
}
//以其他model集合为属性
public void setFmatOutIList(Set<FmatOutI> fmatOutIList) {
this.fmatOutIList = fmatOutIList;
}
public Set<FmatOutI> getFmatOutIList() {
return fmatOutIList;
}
private Set<FmatOutI> fmatOutIList;
}
xml文件:
<hibernate-mapping>
//指定类名与数据库表名
<class name="com.iman.logistics.domain.model.FwhOut" table="f_wh_out">
//定义唯一标识符并指定生成方法为uuid ,类属性为uid,数据库字段为u_id
<id name="uid" column="u_id" length="36">
<generator class="uuid.hex"/>
</id>
<property name="fdCreateTime" column="fd_create_time" type="timestamp" update="true" insert="true" not-null="true" />
//多对一的对象属性,lazy决定在加载本实例时fwh将同时加载。在数据表里保存的字段是fd_wh,实际保存的是该fwh类中对应的hibernate配置文件中定义的唯一标识符
<many-to-one name="fwh" column="fd_wh" insert="true" update="true" not-null="false" lazy="false" />
//一对多的集合属性,将在加载本实例时该列表将同时加载,
//在数据库中保存时,该表中不保存子集的任何字段,而在子集对应的表中的fd_wh_out字段保存该类的唯一标识符。
//子集的类中需定义该类的属性,配置文件中需配置为多对一的关系,类似本类的fwh。
<set name="fmatOutIList" table="f_mat_out_i" inverse="false" cascade="all" lazy="false">
<key column="fd_wh_out" />
<one-to-many class="com.iman.logistics.domain.model.FmatOutI" />
</set>
</class>
</hibernate-mapping>