SSH整合(Struts2.3.7 + Spring3.2.0 + Hibernate4.1.9)

来源:互联网 发布:linux cut 命令 编辑:程序博客网 时间:2024/06/02 01:59

一、整合环境:window7 + Tomcat6 + MySQL5.1

二、需要的jar:相应的jar包,视功能而定。既然是学习,就应该自己尝试,这样就更熟悉每个jar包的作用。

三、各个文件配置:

1.WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name>mySystem</display-name><!-- 指定spring的配置文件,默认从web根目录寻找,我们可以通过spring提供的classpath:前缀来指定从类路径下寻找 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext*.xml</param-value></context-param><!-- Spring上下文监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 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><!-- Session时间配置(单位:分钟) --><session-config><session-timeout>30</session-timeout></session-config><!-- 登陆页面 --><welcome-file-list><welcome-file>/index.jsp</welcome-file></welcome-file-list></web-app>

2.src/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><constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 --><constant name="struts.serve.static.browserCache" value="false" /> <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 --><constant name="struts.configuration.xml.reload" value="true" /> <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 --><constant name="struts.ui.theme" value="simple"></constant> <!-- 默认的视图主题 --><constant name="struts.multipart.saveDir" value="E:/temp"></constant> <!-- 文件上传时的临时目录 --><constant name="struts.multipart.maxSize" value="1048576000" /> <!-- 文件上传时的最大值 (单位:k) --><constant name="struts.objectFactory" value="spring" /> <!-- 使用spring的工厂去替换Struts2的默认工厂,也就是Action由spring来创建和维护 --><constant name="struts.action.extension" value="action" /> <!-- 拦截的后缀名 --><include file="struts-default.xml" /> <!-- 导入默认的xml配置文件 --><!-- 定义全局的配置设置 --><package name="struts-main" extends="struts-default"><interceptors><!-- Session拦截器 --><interceptor name="sessionTimeOut" class="com.bao.utils.interceptors.SessionInterceptor" /><!-- 去除页面参数字符串两端的空格拦截器 --><interceptor name="trimInterceptor" class="com.bao.utils.interceptors.TrimInterceptor" /><!-- 定义拦截器栈 --><interceptor-stack name="baseStack"><interceptor-ref name="defaultStack" /> <!-- 系统默认的拦截器,必须要有 --></interceptor-stack></interceptors><default-interceptor-ref name="baseStack" /> <!-- 配置默认拦截器栈 --><!-- 全局配置 --><global-results><result name="error">/WEB-INF/admin/error.jsp</result><result name="success">/WEB-INF/admin/success.jsp</result></global-results></package><!-- 引用功能模块对应的Struts配置文件 --><include file="com/bao/admin/admin_struts.xml"></include></struts>

3.src/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: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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 启用扫描功能:扫描+注解的方式,把bean交给Spring管理 --><context:component-scan base-package="com.bao" /><!-- 加载JDBC的配置文件 --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 配置C3p0数据源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass" value="${driverClass}" /><property name="jdbcUrl" value="${jdbcUrl}" /><property name="user" value="${user}" /><property name="password" value="${password}" /><!-- 每60秒检查所有连接池中的空闲连接。Default: 0 --><property name="idleConnectionTestPeriod" value="60" /><!-- 初始化时获取的链接数,取值应在minPoolSize与maxPoolSize之间。Default:3 --><property name="initialPoolSize" value="3" /><!-- 连接池中保留的最小连接数。Default:2 --><property name="minPoolSize" value="2" /><!-- 连接池中保留的最大连接数。Default:15 --><property name="maxPoolSize" value="15" /><!-- 最大空闲时间,120秒内未使用则连接被丢弃。若为0则永不丢弃。Default:0 --><property name="maxIdleTime" value="120" /><!-- 当连接池中的连接耗尽时,c3p0一次同时获取的连接数。Default:3 --><property name="acquireIncrement" value="3" /></bean><!-- Hibernate 配置 --><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="mappingResources"><list><value>com/bao/admin/bean/Admin.hbm.xml</value></list></property><property name="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.MySQL5Dialecthibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=false</value></property></bean><!-- 配置事务管理器,以使用Spring提供的事务管理功能 --><bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 使用基于注解方式来配置事务 --><tx:annotation-driven transaction-manager="transactionManager" /></beans>


4.src/jdbc.properties:

driverClass=org.gjt.mm.mysql.DriverjdbcUrl=jdbc:mysql://localhost:3306/mysystem?useUnicode=true&characterEncoding=UTF-8user=rootpassword=123456


5.参考文档:http://txazo.iteye.com/blog/1669771 点击打开链接

说明:其它的实体类和Hibernate的映射文件,并没有给出,是为了逼自己学习更多的知识!

ps:其实查看官方文档并没有想像中的难,即使英语再差,当学习到一定的时候就应该逼着自己看看的官方文档,不能总是看已经翻译好的文档,这样才能提升自己的能力!

原创粉丝点击