配置方式一:struts.xml, applicationContext.xml(hibernate.cfg.xml配置信息写入spring配置文件中)
(版本号, struts2:2.3.15; spring:3.2.0; hibernate:3.6.10)
web.xml配置信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>zjs_bos</display-name>
<!-- 配置hibernate延迟加载的过滤器 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 指定spring核心配置文件的加载路径 -->
<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>
<!--同事拦截转发数据和请求数据-->
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml配置信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 开发者模式,在开发者模式中,
如果jsp发送的请求数据,在action中没有给提供相应的get和set方法,会报错java.lang.NoSuchFieldException -->
<constant name="struts.devMode" value="true"></constant>
<!-- 与spring整合struts的配置,可以不用配置,默认就是这个配置 -->
<constant name="struts.objectFactory" value="spring"></constant>
<!-- 配置struts国际化参数,设置message.properties路径 -->
<constant name="struts.custom.i18n.resources" value="message"></constant>
(备注,可以在action类中,通过this.getText("usernameOrPasswordError")来获取message.properties文件中usernameOrPasswordError对应的值)
<!-- 配置package -->
<package name="basicstruts2" extends="struts-default" namespace="/">
<!-- 配置自定义拦截器 -->
<interceptors>
<interceptor name="bosLoginInterceptor" class="cn.rodge.bos.web.interceptoer.BOSLoginInterceptor">
<!--声明不需要拦截的方法-->
<param name="excludeMethods">login</param>
</interceptor>
<!-- 声明拦截器栈 -->
<interceptor-stack name="bos">
<!--声明自定义拦截器-->
<interceptor-ref name="bosLoginInterceptor"></interceptor-ref>
<!--添加默认拦截器栈-->
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 将bos定义为默认拦截器栈 -->
<default-interceptor-ref name="bos"></default-interceptor-ref>
<!--声明全局结果变量,注意全局结果变量的声明是有顺序的,不能再拦截器前面声明,不然会报错-->
<global-results>
<result name="login">/login.jsp</result>
</global-results>
<!-- 配置通过action访问jsp的路径
class不配置的话,默认访问actionsupport
method不配置的话,默认访问execute方法
-->
<action name="page_*_*">
<!-- name不配置的话,默认访问SUCCESS对应的 -->
<result type="dispatcher">/WEB-INF/pages/{1}/{2}.jsp</result>
</action>
</package>
</struts>
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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 5.设置配置数据库文件路径 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 1.配置数据库c3p0连接池,即数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 2.配置sessionfactory -->
<!--在hibernate3中自动提交事务的参数为false,默认不自动提交-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 2.1配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 2.2配置hibernate核心配置文件 -->
<property name="hibernateProperties">
<props>
<!-- 配置方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<!-- 配置输出 -->
<prop key="hibernate.show_sql">true</prop>
<!-- 配置格式化 -->
<prop key="hibernate.format_sql">true</prop>
<!-- 配置自动建表 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 2.3配置映射文件路径 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:cn/rodge/bos/domain</value>
</list>
</property>
</bean>
<!-- 3.开启注解扫描 -->
<context:component-scan base-package="cn.rodge.bos"></context:component-scan>
<!-- annotation-config可以不用配置,默认开始注解扫描就会自动配置上 -->
<context:annotation-config/>
<!-- 4.配置事务,并开启事务注解扫描 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
hibernate中映射文件信息(User.hbm.xml)
(通过myeclipse的反转引擎根据数据库中的表自动生成对应的实体类和映射文件)
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="cn.rodge.bos.domain.User" table="t_user" catalog="zjs_bos">
<id name="id" type="java.lang.String">
<column name="id" length="32" />
<generator class="assigned" />
</id>
<property name="username" type="java.lang.String">
<column name="username" length="20" not-null="true" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="32" not-null="true" />
</property>
<property name="salary" type="java.lang.Double">
<column name="salary" precision="22" scale="0" />
</property>
<property name="birthday" type="java.util.Date">
<column name="birthday" length="10" />
</property>
<property name="gender" type="java.lang.String">
<column name="gender" length="10" />
</property>
<property name="station" type="java.lang.String">
<column name="station" length="40" />
</property>
<property name="telephone" type="java.lang.String">
<column name="telephone" length="11" />
</property>
<property name="remark" type="java.lang.String">
<column name="remark" />
</property>
</class>
<!--通过QueryName进行查询-->
<query name="findByNameAndPassword">FROM User WHERE username = ? and password = ?</query>
</hibernate-mapping>
jdbc.properties数据链接信息
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/zjs_bos
user=zjs_user
password=123
message.properties中提示信息
checkCodeError=\u9A8C\u8BC1\u7801\u8F93\u5165\u9519\u8BEF
usernameOrPasswordError=\u7528\u6237\u540D\u6216\u5BC6\u7801\u9519\u8BEF
log4j.properties日志记录信息
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=D:/hibernate.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout