Struts2+spirng+hibernate的配置文件-文档式
新人贴:
声明:这是我在Java培训学习阶段的配置文件并不能使用真正开发的环境,只能用于学习与借鉴.
本文章的重点在于applicationContext.xml文件的配置.
如有不对的地方,请留言告知,谢谢!
目录
- 所需的jar包
- web配置文件
- applicationContext.xml配置文件设置
- Struts配置文件配置
- 实体类的映射文件配置
jar包
jar的版本的不同可能会影响到配置文件引用的类文件地址的不同,到application配置时,我会点出.
web文件的配置
web配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<!-- struts2过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置 applicationContext.xml配置文件位置 classpath:代表src-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码-->
<filter>
<filter-name>springUtf8Encoding</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springUtf8Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
application配置文件的配置
applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- beans的头文件是对应的 除了 http://www.w3.org/2001/XMLSchema-instance
其他的四个都与xsi:schemaLocation的下面相对应,一个对应两个
-->
<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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!-- 数据源 这里没有连接池的配置,上网可以搜索出很多 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 2.数据库连接工程 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/ssh/entity/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- spring管理3大块 -->
<!-- 1.将sessionFactory纳入spring管理 -->
<bean name="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 2 设置监控的位置 Aop -->
<aop:config>
<aop:pointcut id="bussinessService" expression="execution(public * com.ssh.biz.*.*(..))"/>
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config>
<!-- 3事务的配置 设置 3.1方法名约束 3.2设置是否只读 3.3设置事务的策略 3.4 设置事务的隔离级别-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" read-only="true" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" read-only="true" propagation="REQUIRED" />
<tx:method name="delete*" read-only="true" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 自定义类注入(注册) -->
<!-- dao -->
<bean id="userDaoImpl" class="com.ssh.daoImpl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- biz -->
<bean id="userBizImpl" class="com.ssh.bizImpl.UserBizImpl">
<property name="userDaoImpl" ref="userDaoImpl"></property>
</bean>
<!-- action -->
<bean id="loginAction" class="com.ssh.action.LoginAction">
<property name="userBizImpl" ref="userBizImpl"></property>
</bean>
</beans>
标红的地方就是jar不同,会引起包路径的不同
Struts2配置文件的配置
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.devMode" value="true" />
<!-- 改嫁协议书 这是spring和Struts连接的重要部分-->
<constant name="struts.objectFactory" value="spring" />
<package name="login" extends="struts-default" >
<action name="loginAction" class="loginAction" method="checkLogin">
<result name="success">/main.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
</struts>
实体类映射文件的配置
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--package是映射文件所在的包路径 -->
<hibernate-mapping package="com.ssh.entity">
<!-- name是实体类名 table是对应的表的名字 -->
<class name="User" table="user">
<!-- name是实体类的属性 column是数据库的字段 -->
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="username" column="username" type="java.lang.String"/>
<property name="password" column="password" type="java.lang.String"/>
</class>
</hibernate-mapping>
总结:
ssh框架的配置难点就在spring的配置上,这里的spring配置只是最基础的配置.其中省略了,一些默认配置和一些在学习中并不适用的配置.