Struts2+spring+hibernate的配置文件配置

Struts2+spirng+hibernate的配置文件-文档式

新人贴:

声明:这是我在Java培训学习阶段的配置文件并不能使用真正开发的环境,只能用于学习与借鉴.

本文章的重点在于applicationContext.xml文件的配置.

如有不对的地方,请留言告知,谢谢!

目录

  1. 所需的jar包
  2. web配置文件
  3. applicationContext.xml配置文件设置
  4. Struts配置文件配置
  5. 实体类的映射文件配置

jar包

Struts2+spring+hibernate的配置文件配置_第1张图片

jar的版本的不同可能会影响到配置文件引用的类文件地址的不同,到application配置时,我会点出.

web文件的配置

web配置文件:


<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">

    
    <filter>
        <filter-name>struts2filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>
    filter>
    <filter-mapping>
        <filter-name>struts2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>

  
    <filter>  
       <filter-name>springUtf8Encodingfilter-name>  
       <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
       <init-param>  
           <param-name>encodingparam-name>  
           <param-value>UTF-8param-value>  
       init-param>  
       <init-param>  
           <param-name>forceEncodingparam-name>  
           <param-value>trueparam-value>  
       init-param>   
    filter>  
    <filter-mapping>  
       <filter-name>springUtf8Encodingfilter-name>  
       <url-pattern>/*url-pattern>  
   filter-mapping> 

    <welcome-file-list>
        <welcome-file>login.jspwelcome-file>
    welcome-file-list>
web-app>

application配置文件的配置

applicationContext.xml文件



<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>


    
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <value>com/ssh/entity/User.hbm.xmlvalue>
            list>
        property>
        <property name="hibernateProperties">  
            <props>  
               <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>  
               <prop key="hibernate.show_sql">trueprop>  
            props>  
        property>  
    bean>  

    
    
    <bean name="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
       <property name="sessionFactory" ref="sessionFactory">property>  
    bean>   

    
    <aop:config>
        <aop:pointcut id="bussinessService" expression="execution(public * com.ssh.biz.*.*(..))"/>
        <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice"  />
    aop:config>

    
    <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>



    

    
    <bean id="userDaoImpl" class="com.ssh.daoImpl.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    bean>

    
    <bean id="userBizImpl" class="com.ssh.bizImpl.UserBizImpl">
        <property name="userDaoImpl" ref="userDaoImpl">property>
    bean>

    
    <bean id="loginAction" class="com.ssh.action.LoginAction">
        <property name="userBizImpl" ref="userBizImpl">property>
    bean>
beans>

标红的地方就是jar不同,会引起包路径的不同
Struts2+spring+hibernate的配置文件配置_第2张图片

Struts2配置文件的配置

struts.xml




<struts>

        
<constant name="struts.devMode" value="true" />
    
    <constant name="struts.objectFactory" value="spring" /> 

    <package name="login" extends="struts-default" >
        <action name="loginAction" class="loginAction" method="checkLogin">
            <result name="success">/main.jspresult>
            <result name="login">/login.jspresult>
        action>
    package>

struts>

实体类映射文件的配置

User.hbm.xml




    
<hibernate-mapping package="com.ssh.entity">


    <class name="User" table="user">
    
        <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配置只是最基础的配置.其中省略了,一些默认配置和一些在学习中并不适用的配置.

你可能感兴趣的:(ssh整合)