如何使用SSH框架开发实际项目-开发规范

SSH框架应用非常广泛,很多公司很多项目都会用到这3钟框架,那么如何正确规范的使用这三种框架开发实际的项目呢?这个问题是我们老大给我布置的任务,要采用SSH框架开发一个新人(我就是一个新人,老大在培训我)培训的系统。开始的时候,我想这还不是很容易吗,我那么多项目代码,随便拿过来一个,稍微改改就可以了。但是老大给我任务,让我写成文档,从开始部署SSH框架一直到系统实现都要说明出来。这就让我犯难了,可是,我是一个喜欢挑战的人,越是困难我越是喜欢。于是,就开始了我一点一点采用SSH框架开发的过程。下面,是我对这个过程的总结:

J2EE框架的基本功能需求图:


struts2+hibernate3+spring3
分别去网上下载这3个开源框架,下载地址:
Struts2: http://struts.apache.org/2.2.3/index.html
hibernate3: http://sourceforge.net/projects/hibernate/files/hibernate3/
spring3: http://www.springsource.org/download

首先,引入SSH框架所需的最小jar包
    通过这个过程,了解SSH框架所需要的jar包,以及它们的各自用途。下面是我引入的最小jar包的列表:
Struts2   9个
1.struts2-core-2.2.3.jar struts的核心jar包。
2.freemarker-2.3.16.jar Freemarker是struts2默认的模版语言
3.commons-logging-1.1.1.jar Apache  Commons包中的一个,包含了日志功能,必须使用的jar包
4.ognl-3.0.1.jar Struts2默认的表达式语言OGNL:对象图形化导航语言
5.xwork-core-2.2.3.jar Struts2核心包,毕竟struts2很大部分是来webwork6.commons-io-2.0.1.jar 封装了一些输入输出流的常用操作
7.commons-fileupload-1.2.2.jar 用来实现文件上传
8. struts2-json-plugin-2.2.3.jar JSON 插件提供了一个 "json" 结果类型来把 action 序列化成 JSON
9. struts2-spring-plugin-2.1.6.jar 使struts2能集成到spring中

Spring框架 13个
1.org.springframework.aop.jar 包含在应用中使用Spring的AOP特性时所需的类
2. org.springframework.beans.jar 所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。
3.org.springframework.context.support.jar 包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。
4.org.springframework.context.jar 为Spring核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类,UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、 JasperReports集成的类,以及校验Validation方面的相关类。
5.org.springframework.core.jar 包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心。
6.org.springframework.expression.jar Spring表达式语言
7.org.springframework.jdbc.jar 包含对Spring对JDBC数据访问进行封装的所有类。
8.org.springframework.jms.jar 提供了对JMS 1.0.2/1.1的支持类。
9. org.springframework.orm.jar 包含Spring对DAO特性集进行了扩展,使其支持 iBATIS、JDO、OJB、TopLink,因为Hibernate已经独立成包了,现在不包含在这个包里了。这个jar文件里大部分的类都要依赖spring-dao.jar里的类,用这个包时你需要同时包含spring-dao.jar包。
10.org.springframework.transaction.jar 为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。
11.org.springframework.web.struts.jar Struts框架支持,可以更方便更容易的集成Struts框架。
12.org.springframework.web.jar 包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。
13. org.springframework.asm.jar Spring独立的asm程序, Spring2.5.6的时候需要asmJar 包,3.0开始提供他自己独立的asmJar

Hibernate3框架10个

1.hibernate3.jar 这个是hibernate3.0的核心jar包,必须的,呵呵,没的选,像我们常用的Session,Query,Transaction都位于这个jar文件中,必要。2.cglib-2.1.3.jar CGLIB库,Hibernate用它来实现PO字节码的动态生成,非常核心的库,必要。3.asm.jar ASM字节码库 如果使用“cglib”则必要,必要
4.asm-attrs.jar ASM字节码库 如果使用“cglib”则必要,必要
5.ehcache.jar EHCache缓存 如果没有其它缓存,则必要,必要
6.antlr.jar ANother Tool for Language Recognition是一个工具,必要7.jta.jar JTA规范,当Hibernate使用JTA的时候需要,不过AppServer都会带上,所以也是多余的。但是为了测试方便建议还是带上。必要
8.commons-collections.jar ApacheCommons包中的一个,包含了一些Apache开发的集合类,功能比java.util.*强大。必要
9.dom4j 是一个Java的XMLAPI,类似于jdom,用来读写XML文件的。Hibernate用它来读写配置文件。必要
10.C3P0.jar 提供数据库连接池


其他4个
1. com.springsource.javax.mail-1.4.0.jar Java email组建,提供email的常用方法
2. mysql-connector-java-5.0.8-bin.jar mysql数据库驱动
3. commons-lang-2.3.jar Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包http://commons.apache.org/lang/
4. log4j-1.2.15.jar 提供日志功能http://logging.apache.org/log4j/



SSH框架的部署需要一些相关的配置文件,下面这个部分,是SSH框架的部署说明,部署说明主要分成俩块,一块是应用的部署,一块为数据库的部署。
SSH:Struts2+Hibernate3.0+Spring3.0

配置SSH开发框架
1)引入struts+spring+hibernate所需要的包,包列表在最小jar表格中有说明。

2)在web.xml中加入如下代码令服务器自动加载Spring
Java代码 复制代码  收藏代码
  1.              <listener>   
  2.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  3. </listener>   
  4. <context-param>   
  5.         <param-name>contextConfigLocation</param-name>   
  6.         <param-value>classpath*:applicationContext.xml</param-value>   
  7. </context-param>   
  8.             
             <listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
		<param-name>contextConfigLocation</param-name>
	  	<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
          


3)整合struts2
    1.Spring与struts的整合,就是将struts的action类交给spring进行管理。需要导入所需要的jar包。Struts2-spring-plugin.jar
    2.配置web.xml文件,在web.xml文件中添加下面的代码
 
Java代码 复制代码  收藏代码
  1. <filter>   
  2.         <filter-name>struts2</filter-name>  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>   
  3.     </filter>   
  4.     <filter>   
  5.         <filter-name>struts-cleanup</filter-name>   
  6.         <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>   
  7.     </filter>   
  8.     <filter-mapping>   
  9.         <filter-name>struts-cleanup</filter-name>   
  10.         <url-pattern>/*</url-pattern>   
  11.     </filter-mapping>   
  12.     <filter>   
  13.         <filter-name>encodingFilter</filter-name>   
  14.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   
  15.         <init-param>   
  16.             <param-name>encoding</param-name>   
  17.             <param-value>UTF-8</param-value>   
  18.         </init-param>   
  19.     </filter>   
  20.     <filter-mapping>   
  21.         <filter-name>struts2</filter-name>   
  22.         <url-pattern>/*</url-pattern>   
  23.     </filter-mapping>   
  24.     <filter-mapping>   
  25.         <filter-name>encodingFilter</filter-name>   
  26.         <url-pattern>*.action</url-pattern>   
  27.     </filter-mapping>   
  28.     <filter-mapping>   
  29.         <filter-name>encodingFilter</filter-name>   
  30.         <url-pattern>*.jsp</url-pattern>   
  31.     </filter-mapping>  
<filter>
		<filter-name>struts2</filter-name>	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter>
		<filter-name>struts-cleanup</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts-cleanup</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>

3.spring管理struts2的action,以userLoginAction为例。
a.在reource/spring文件夹下面,编写spring配置文件,实现对struts2的action的管理。这里以spring-bean-user.xml为例。这里,首先创建了UserAction的bean,给它指定了一个唯一id为:UserAction,scope="prototype"意味着,每一次请求,创建一个UserAction的实例。
  
Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
  5.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"   
  6.     default-lazy-init="true" default-autowire="byName">   
  7.   
  8.   
  9.     <bean id="UserAction" class="com.neweducation.user.action.UserAction"  
  10.         scope="prototype">   
  11.         <property name="userService" ref="userService" />   
  12.     </bean>   
  13.   
  14.     <bean id="userService" class="com.neweducation.user.service.impl.UserServiceImpl">   
  15.         <property name="userDao" ref="userDao" />        
  16.     </bean>   
  17.     <bean id="userDao" class="com.neweducation.user.dao.UserDao">   
  18.         <property name="sessionFactory" ref="sessionFactory" />   
  19.     </bean>   
  20.   
  21. </beans>  
<?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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
	default-lazy-init="true" default-autowire="byName">


	<bean id="UserAction" class="com.neweducation.user.action.UserAction"
		scope="prototype">
		<property name="userService" ref="userService" />
	</bean>

	<bean id="userService" class="com.neweducation.user.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao" />		
	</bean>
	<bean id="userDao" class="com.neweducation.user.dao.UserDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

</beans>

b.在struts2.xml文件中,配置登录的action,为例防止struts2.xml的内容过多,这里,我们对每个不同的部分创建对应的配置文件,比方说,对user,创建一个struts-user.xml的配置文件。然后在struts2.xml文件中引入这个配置文件。





配置struts-user.xml文件
Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.         "http://struts.apache.org/dtds/struts-2.0.dtd">   
  4.   
  5. <struts>   
  6.   
  7.     <package name="user" extends="json-default" >   
  8.   
  9.         <action name="userLogin" class="UserAction" method="userLogin">   
  10.             <result name="success">index.jsp</result>   
  11.             <result name="input">login.jsp</result>   
  12.         </action>          
  13.     </package>   
  14. </struts>  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="user" extends="json-default" >

        <action name="userLogin" class="UserAction" method="userLogin">
            <result name="success">index.jsp</result>
            <result name="input">login.jsp</result>
        </action>       
    </package>
</struts>

Struts-user.xml文件中,action的class为UserAction, 即为UserAction类的bean的ID,这样,我们就实现了通过spring来管理Struts2的action。

最后,还需要在struts2.xml文件中,引入struts-user.xml配置文件。
Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.         "http://struts.apache.org/dtds/struts-2.0.dtd">   
  4.   
  5. <struts>   
  6.   
  7.     <package name="user" extends="json-default" >   
  8.   
  9.         <action name="userLogin" class="UserAction" method="userLogin">   
  10.             <result name="success">index.jsp</result>   
  11.             <result name="input">login.jsp</result>   
  12.         </action>          
  13.     </package>   
  14. </struts>  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="user" extends="json-default" >

        <action name="userLogin" class="UserAction" method="userLogin">
            <result name="success">index.jsp</result>
            <result name="input">login.jsp</result>
        </action>       
    </package>
</struts>






4) 整合hibernate3
Spring整合hibernate主要是对hibernate的Session进行管理,包含session的创建,提交,关闭的整个生命周期。Spring对事务的管理应用了AOP技术。
a.配置sessionFactory,让spring来创建session。在appliContext.xml中增加如下代码
Java代码 复制代码  收藏代码
  1. <bean id="sessionFactory"  
  2.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  3.         <property name="dataSource" ref="dataSource"></property>   
  4.         <property name="hibernateProperties">   
  5.             <props>   
  6.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>   
  7.                 <prop key="hibernate.connection.autocommit">true</prop>   
  8.                 <prop key="hibernate.show_sql">true</prop>   
  9.             </props>   
  10.         </property>   
  11.         <property name="mappingResources">   
  12.             <list>   
  13.                 <value>hibernate/User.hbm.xml</value>   
  14.             </list>   
  15.         </property>   
  16.   
  17.     </bean>  
<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.connection.autocommit">true</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>hibernate/User.hbm.xml</value>
			</list>
		</property>

	</bean>


这里,创建了一个Session工厂类的bean,其ID为sessionFactory
b.配置事务管理器
在applicationContext.xml中增加下面的代码
Java代码 复制代码  收藏代码
  1. <bean id="transactionManager"  
  2.     class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
  3.         <property name="sessionFactory" ref="sessionFactory" />   
  4. </bean>  
<bean id="transactionManager"
	class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
</bean>

c.对事务管理进行事务设置
在applicationContext.xml中增加下面代码
Java代码 复制代码  收藏代码
  1. <bean id="transactionBase"  
  2.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
  3.         lazy-init="true" abstract="true">   
  4.             
  5.         <property name="transactionManager" ref="transactionManager" />   
  6.         
  7.         <property name="transactionAttributes">   
  8.             <props>   
  9.                 <prop key="*">PROPAGATION_REQUIRED</prop>   
  10.             </props>   
  11.         </property>   
  12.     </bean>  
<bean id="transactionBase"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
		lazy-init="true" abstract="true">
		 
		<property name="transactionManager" ref="transactionManager" />
	 
		<property name="transactionAttributes">
			<props>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>



通过上面的配置,就可以成功的搭建ssh的开发框架了。下面要做的就是来进行实际的项目开发了。下面是我的目录结构:
resource里面存放着一些需要的配置文件,项目名称为,newEmplyeeEducation,包neweducation里面是关于项目的一些内容。

下面介绍SSH框架的开发规范:
  1. 项目命名规则及其路径:
       1. 项目名及版本:如NewEducation _X . X . X . XX
      2. 包命名规则:
         1. action:com.NewEducation.模块名.action;
         2. service:com.NewEducation.模块名.service;
         3. service.impl:com.NewEducation.模块名.service.impl;
         4. dao:com.NewEducation.模块名.dao;
         5. dao.impl:com.NewEducation.模块名.dao.impl;
         6. model:com.NewEducation.模块名.model;
2. 模块命名规则:
       1. core;
       2. 其他以该模块的英文名定义。
3. 配置文件命名规范:
1.spring框架
Spring实现对struts的action类的管理,对于每个不同模块创建一个spring的配置文件,其命名为:spring-模块名.xml。如:对user创建一个spring-user.xml配置文件,然后在applicationContext.xml里面import这个进去。
2.struts2框架
为了防止struts的配置文件过于庞大,采用每个模块创建一个struts的配置文件,其命名为:struts-模块名.xml。如:对user创建一个struts-user.xml的配置文件,然后在struts.xml里面include这个配置文件
3.hibernate框架
对于每个对象,创建一个hibernate的配置文件,其命名为:对象名-hbm.xml,如:user,创建的配置文件为:User-hbm.xml。然后在applicationContext.xml里面进行管理
4. 配置文件书写规范:
     1. struts.xml采用一个action类对应一个action,一个模块,一个package,尽量避免使用带*号的配置,保证可读性;
     2. spring用来管理struts的action,一个模块要对应一个spring的配置文件。如:对于user模块,要有一个对应的spring配置文件,spring-user.xml
    3. hibernate对于对象的配置文件,变量名称要一致
5. 配置文件路径:
     1. struts-模块名.xml放在resource/struts下面;
     2.sping-模块名.xml放在resource/spring下面
     3.对象名.hbm.xml放在resource/hibernate下面
     4. 其他放到resource目录;
6. 类命名规则:
    1. 采用驼峰标识;
    2. 各个层之间通信尽量精简数据流量,提高系统运行效率;
    3. 接口定义遵循正常人思维;
    4. 各层命名规则:
        1. action:XxxAction;
        2. service:XxxService;
        3. service.impl:XxxServiceImpl;
        4. dao:XxxDao;
        5. dao.impl:XxxDaoImpl;
        6. model:XxxModel;
        7. util:Xxx(Util)可选;
7. 类体方法常量等等定义规范:
       1. getter和setter方法由IDE自动生成,放在所有逻辑方法之后;
       2. 方法命名规则:xxxYyyZzz()首字母小写驼峰标识;
       3. 变量定义规则:xxxYyyZzz,统一用private声明;
       4. util中的类统一用static声明必要时用final;
       5. 常量类:定义常量采用大写字母,并用static、final声明;
       6. 枚举:大写字母;
8. 开发环境和字符集:
       1. 字符集:utf-8;采用常量定义格式放在常量类中;
       2. lib统一放在lib目录下;
       3. 开发环境jdk6.0;java ee 6;
       4. 数据库:mysql 5.1;
       5. IDE:eclipse,版本任选;
       6. 辅助工具自选;
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值