参加工作已经有一段日子了,很久以前就申请过csdn,好像发布了两篇文章,但是最近登进来一看,却不知道在哪里去了。现在工作基本不怎么忙了,觉得闲得无聊。也不知道写些什么,就来整几个框架吧。在这里我会讲三个整合的例子,这个例子是最基础最简单的,所以有经验的同志看了不要笑话我,在后期,我会把这个架子更加经典化,如果不经典,大家那是可以骂我。有兴趣的同志,请加我QQ:544508084,方便后期技术交流。
下面,我们就开始看我的这个架子。首先需要的jar包是这些:
大家可以从jar包中看出本人用的struts2、spring和hibernate的版本,这里就不必多说了。这里,我也不给大家分享我的jar包了,希望各位自己找到。本人用的是:jdk1.6-10,mysql5.5,tomcat6.10。这里我们将会写一个三大框架下的权限管理。
首先,简单的介绍一下本人的包结构:
这里没有将其细分,只是将action、service、和dao分开。数据库连接文件放在database.properties文件里面的。
下面我们开始研究配置文件:
1、web.xml
这个配置文件实现了struts2的最基本配置。了解struts2的朋友们应该知道它是基于拦截器实现的。这里主要是配置struts2的拦截器。其中的filter-name里面有一个encodingFilter,这个类的实现代码如下:
其实走到这一步,都是废话,大家都明白是什么意思。
那么我们接着看:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:property-placeholder location="classpath:database.properties" />
<context:component-scan base-package="tts.cookbook.ssh">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<context:annotation-config />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="initialSize" value="3" />
<property name="maxActive" value="5" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>domain/Admin.hbm.xml</value>
<value> Module.hbm.xml</value>
<value>Operation.hbm.xml</value>
<value>Role.hbm.xml</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="hibernateTxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="hibernateTxManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* tts.cookbook.ssh.service.*.*(..))" />
</aop:config>
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.action.extension" value="" />
<package name="default-package" extends="struts-default"
namespace="/">
<global-results>
<result name="login" type="redirectAction">
loginform
</result>
</global-results>
<!-- 用户登录 -->
<action name="loginform"
class="tts.cookbook.ssh.action.LoginAction" method="showLogin">
<result name="success">/WEB-INF/jsp/login.jsp</result>
</action>
<action name="login" class="tts.cookbook.ssh.action.LoginAction"
method="login">
<result name="success">/WEB-INF/jsp/main.jsp</result>
<result name="login">/WEB-INF/jsp/login.jsp</result>
</action>
<!-- 权限管理 -->
<!-- 权限浏览 -->
<action name="rightreview"
class="tts.cookbook.ssh.action.RightsAction" method="show">
<result name="success">
/WEB-INF/jsp/role/rightbrowse.jsp
</result>
</action>
<action name="rightbrowse"
class="tts.cookbook.ssh.action.RightsAction" method="browse">
<result name="success">
/WEB-INF/jsp/role/rightbrowse.jsp
</result>
</action>
<!-- 权限修改 -->
<action name="rightchange"
class="tts.cookbook.ssh.action.RightsAction" method="show">
<result name="success">
/WEB-INF/jsp/role/rightmodify1.jsp
</result>
</action>
<action name="rightchange1"
class="tts.cookbook.ssh.action.RightsAction" method="browse">
<result name="success">
/WEB-INF/jsp/role/rightmodify1.jsp
</result>
</action>
<action name="rightchange2"
class="tts.cookbook.ssh.action.RightsAction" method="form2">
<result name="success">
/WEB-INF/jsp/role/rightmodify2.jsp
</result>
</action>
<action name="rightchange3"
class="tts.cookbook.ssh.action.RightsAction" method="change">
<result name="success">
/WEB-INF/jsp/role/rightmodify2.jsp
</result>
</action>
<!-- 权限删除 -->
<action name="rightdelete"
class="tts.cookbook.ssh.action.RightsAction" method="show">
<result name="success">
/WEB-INF/jsp/role/rightdelete.jsp
</result>
</action>
<action name="rightdelete1"
class="tts.cookbook.ssh.action.RightsAction" method="browse">
<result name="success">
/WEB-INF/jsp/role/rightdelete.jsp
</result>
</action>
<action name="rightdelete2"
class="tts.cookbook.ssh.action.RightsAction" method="delete">
<result name="success">
/WEB-INF/jsp/role/rightdelete.jsp
</result>
</action>
<!-- 添加权限 -->
<action name="rightadd"
class="tts.cookbook.ssh.action.RightsAction" method="form">
<result name="success">
/WEB-INF/jsp/role/rightadd.jsp
</result>
</action>
<action name="rightadd1"
class="tts.cookbook.ssh.action.RightsAction" method="add">
<result name="success">
/WEB-INF/jsp/role/rightadd.jsp
</result>
</action>
<!-- 添加角色 -->
<action name="roleadd"
class="tts.cookbook.ssh.action.RoleAction" method="form">
<result name="success">
/WEB-INF/jsp/role/roleform.jsp
</result>
</action>
<action name="addrole_add"
class="tts.cookbook.ssh.action.RoleAction" method="add">
<result name="success">
/WEB-INF/jsp/role/operationSuccess.jsp
</result>
</action>
<!-- 角色浏览 -->
<action name="rolereview"
class="tts.cookbook.ssh.action.RoleAction" method="show">
<result name="success">
/WEB-INF/jsp/role/rolelist.jsp
</result>
</action>
<action name="rolebrowse"
class="tts.cookbook.ssh.action.RoleAction" method="roleBrowse">
<result name="success">
/WEB-INF/jsp/role/rolelist.jsp
</result>
</action>
<!-- 角色修改 -->
<action name="rolechange"
class="tts.cookbook.ssh.action.RoleAction" method="show">
<result name="success">
/WEB-INF/jsp/role/rolemodify1.jsp
</result>
</action>
<action name="rolechange1"
class="tts.cookbook.ssh.action.RoleAction" method="roleBrowse">
<result name="success">
/WEB-INF/jsp/role/rolemodify1.jsp
</result>
</action>
<action name="rolechange2"
class="tts.cookbook.ssh.action.RoleAction" method="form2">
<result name="success">
/WEB-INF/jsp/role/rolemodify2.jsp
</result>
</action>
<action name="rolechange3"
class="tts.cookbook.ssh.action.RoleAction" method="update">
<result name="success">
/WEB-INF/jsp/role/operationSuccess.jsp
</result>
</action>
<!-- 角色删除 -->
<action name="roledelete"
class="tts.cookbook.ssh.action.RoleAction" method="show">
<result name="success">
/WEB-INF/jsp/role/roledelete.jsp
</result>
</action>
<action name="roledb" class="tts.cookbook.ssh.action.RoleAction"
method="roleBrowse">
<result name="success">
/WEB-INF/jsp/role/roledelete.jsp
</result>
</action>
<action name="roledb2"
class="tts.cookbook.ssh.action.RoleAction" method="delete">
<result name="success">
/WEB-INF/jsp/role/operationSuccess.jsp
</result>
</action>
<!-- 管理员管理 -->
<action name="admininfchange"
class="tts.cookbook.ssh.action.AdminAction" method="show">
<result name="success">/WEB-INF/jsp/admin/admininfchange.jsp</result>
<result name="fail">/WEB-INF/jsp/admin/operationFail.jsp</result>
</action>
<action name="admininfchange1"
class="tts.cookbook.ssh.action.AdminAction" method="selfUpdate">
<result name="success">/WEB-INF/jsp/admin/admininfchange.jsp</result>
</action>
<action name="adminreview"
class="tts.cookbook.ssh.action.AdminAction" method="show">
<result name="success">/WEB-INF/jsp/admin/adminreview.jsp</result>
</action>
<action name="adminreview1"
class="tts.cookbook.ssh.action.AdminAction" method="review">
<result name="success">/WEB-INF/jsp/admin/adminreview.jsp</result>
</action>
<action name="adminadd"
class="tts.cookbook.ssh.action.AdminAction" method="form">
<result name="success">/WEB-INF/jsp/admin/adminadd.jsp</result>
</action>
<action name="adminadd1"
class="tts.cookbook.ssh.action.AdminAction" method="addAdmin">
<result name="success">/WEB-INF/jsp/admin/adminadd.jsp</result>
<result name="fail">/WEB-INF/jsp/admin/operationFail.jsp</result>
</action>
<action name="adminchange"
class="tts.cookbook.ssh.action.AdminAction" method="show">
<result name="success">/WEB-INF/jsp/admin/adminchange1.jsp</result>
</action>
<action name="adminchange1"
class="tts.cookbook.ssh.action.AdminAction" method="review">
<result name="success">/WEB-INF/jsp/admin/adminchange1.jsp</result>
</action>
<action name="adminchange2"
class="tts.cookbook.ssh.action.AdminAction" method="form1">
<result name="success">/WEB-INF/jsp/admin/adminchange2.jsp</result>
</action>
<action name="adminchange3"
class="tts.cookbook.ssh.action.AdminAction" method="updateAdmin">
<result name="success">/WEB-INF/jsp/admin/adminchange2.jsp</result>
</action>
<action name="admindelete"
class="tts.cookbook.ssh.action.AdminAction" method="show">
<result name="success">/WEB-INF/jsp/admin/admindelete.jsp</result>
</action>
<action name="admindelete1"
class="tts.cookbook.ssh.action.AdminAction" method="review">
<result name="success">/WEB-INF/jsp/admin/admindelete.jsp</result>
</action>
<action name="admindelete2"
class="tts.cookbook.ssh.action.AdminAction" method="deleteAdmin">
<result name="success">/WEB-INF/jsp/admin/admindelete.jsp</result>
</action>
</package>
</struts>
这里最大的麻烦就是struts配置文件太多了,后面我们讲的架子里面我们基本上可以不写一句struts2和hibernate配置文件,而且spring配置文件将会变得很少。其主要代码我这里就不写了,如果有兴趣的话,大家可以联系我的QQ一起讨论。