您的位置:首页 > 其它

一个ssh项目前期整合准备

2015-04-29 14:18 274 查看
ssh整合

1.先增加Struts2的运行环境
-----1.加入jar(基础jar有struts2-blank-2.1.8.war下面的6个jar)
-------2.加入Struts.xml配置文件
--------------------------------------------------------------------------------------------------------------------------------
<?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>
<!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
<constant name="struts.action.extension" value="action"/>
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false"/>
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<package name="test" namespace="/test" extends="struts-default">
  <!-- 用ssh整合后,class用的是spring容器中的bean -->
  <action name="test" class="test_struts1">
<result name="success">
/NewFile.jsp
</result>
  </action>
</package>
</struts>
--------------------------------------------------------------------------------------------------------------------------------
-----------3.在web.xml配置文件中加入Struts2的分发器,并且分发器必须放在所以分发器最后
--------------------------------------------------------------------------------------------------------------------------------
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

--------------------------------------------------------------------------------------------------------------------------------

2.加入hibernate的环境
---1.加入hibernate的jar:(数据库链接jar,request下的6个jar,hibernate核心jar,
hibernate-jpa-2.0-api-1.0.0.Final.jar,c3p0-0.9.1.jar)
------2.加入hibernate.cfg.xml配置文件
--------------------------------------------------------------------------------------------------------------------------------
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<!-- 配置与数据的链接 -->
<!-- 
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
-->
<!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3308/myoa</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 在hibernate启动时,自动更新表结构 -->
<property name="hbm2ddl.auto">update</property>
<!-- 不能加上下面的,单独使用hibernate时可以,整合后不可 -->
<!-- <property name="hibernate.current_session_context_class">thread</property>
--> <!-- <property name="hibernate.current_session_context_class">jta</property> -->
<!-- <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
--> <!-- 在控制台打印 -->
<property name="show_sql">true</property>
<!-- 配置实体类对象的hbm.xml的路径  注意前面是相对路径 -->
<!-- <mapping resource="test_20150331/Person.hbm.xml"/> -->
<mapping resource="com/test/User.hbm.xml"/> 
</session-factory>
</hibernate-configuration>
--------------------------------------------------------------------------------------------------------------------------------
------------3.加入*.hbm.xml配置文件
******************************************************************************************************************
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- package是指在那个包下 -->
<hibernate-mapping default-lazy="false" package="com.test">
<!-- name是指实体对象的类名 table是指对应数据库中的那个表,是表名 -->
<class name="User" table="user">
<!-- id是主键的标签 name对应实体对象中的属性  column是指数据库中的列名 -->
<id name="id" column="id">
<!-- 这个属性代表定义主键是不是自增的 -->
<generator class="native"></generator>
</id>
<!--  name对应实体对象中的属性  column是指数据库中的列 type是指对应的类型 -->
<property name="name" type="string" column="name"></property>
</class>

</hibernate-mapping>

******************************************************************************************************************

3.加入spring环境
---1.加入spring的jar(aspectjrt.jar,aspectjweaver.jar,cglib-nodep-2.1_3.jar,commons-logging.jar,spring.jar五个基本jar)
-------2.加入beans.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:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-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/aop   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   ">
<!-- 自动扫描和装配,子类下面的也会扫描 -->
<context:component-scan base-package="com.test"></context:component-scan>
<context:annotation-config></context:annotation-config>
</beans>

******************************************************************************************************************

4.整合Struts2和spring
----1.加入spring和Struts2整合需要的jar(struts2-spring-plugin-2.1.8.jar)
-------2.在web.xml中加入整合的配置  一般放在Struts2的分发器前面
------------------------------------------------------------------------------------------------------------------------
<!-- 配置Spring的用于初始化容器对象的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
------------------------------------------------------------------------------------------------------------------------

5.整合spring和hibernate
----1.加入jdbc.properties
******************************************************************************************************************
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3308/myoa?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456

******************************************************************************************************************

----------2.在beans.xml配置文件中配置hibernate需要的数据源和事务管理等信息
******************************************************************************************************************
<!-- 配置sessionfactory -->
<!-- 配置SessionFactory(整合Hibernate) -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 指定hibernate的配置文件的位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库连接信息 -->
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driverClassName}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
</property>
</bean>

<!-- 配置声明式事务管理(采用注解的方式) -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>

******************************************************************************************************************

6.配置日志文件log4j
----1.加入jar(slf4j-log4j12-1.5.0.jar这个jar版本必须与slf4j-api-1.5.0.jar一致,log4j-1.2.15.jar)
-------2.加入log4j.properties配置文件
******************************************************************************************************************
里面的操作:
#log4j.rootLogger=debug, stdout
log4j.rootLogger=error, stdout
#log4j.rootLogger=info, stdout

这个可以针对一个包下的配置
#log4j.logger.org.hibernate.test=info
******************************************************************************************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: