SSH框架搭建步骤(笔记)

SSH框架搭建步骤(笔记)

各框架版本:Struts2-2.3.28 + Spring-4.2.5 + Hibernate-5.1.0

步骤:

1.加入Spring
(1).加入jar包(别忘了加入aopalliance-1.0.jar和aspectjweaver-1.8.9.jar,否则会报错,后面说);
(2).配置web.xml文件(context-param和listener,最好在servlet之前);代码:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

3.创建(暂不配置)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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

</beans>

2.加入Hibernate
(1).建立持久化类(JavaBean)和其对应的*.hbm.xml 文件,并生成对应的数据表;
持久化类Student:

/**
 * @Title: Student.java 
 * @Package com.ynnu.sshdemo.domain 
 * @Description: TODO
 * @author Administrator   
 * @date 2016年4月7日 上午11:47:34 
 * @version V1.0
 */
package com.ynnu.sshdemo.domain;
/**
 * @ClassName: Student
 * @Description: TODO
 * @author Administrator
 * @date 2016年4月7日 上午11:47:34 
 */
public class Student {
    private Integer id;
    private String name;    
    public Integer getId() {
        return id;
    }   
    public void setId(Integer id) {
        this.id = id;
    }   
    public String getName() {
        return name;
    }   
    public void setName(String name) {
        this.name = name;
    }   
}

对应的Student.hbm.xml(与Student在同一包下):

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-4-7 11:58:09 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.ynnu.sshdemo.domain.Student" table="ssh_student">
        <id name="id" type="java.lang.Integer">
            <column name="stu_id" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="stu_name" />
        </property>
    </class>
</hibernate-mapping>

(2).开始Spring整合Hibernate,具体步骤:
①.加入Hibernate的jar包(required下的所有包,c3p0下的所有包);
②.加入数据库驱动的jar包(比如MySQL的驱动包);
③.在类路径下创建jdbc.properties文件,用来配置数据库连接的相关属性,便于后面配置c3p0数据源;

jdbc.user=root
jdbc.password=XXXXXX
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/ssh_demo
jdbc.initPoolSize=5
jdbc.maxPoolSize=10

④.在类路径下创建hibernate.cfg.xml,在其中配置相关属性(方言,SQL语句显示与格式化,生成数据表策略,缓存等等);

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- hibernate的相关配置 -->
        <!-- 方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <!-- 是否显示及格式化SQL -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <!-- 生成数据表的策略 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>

⑤.开始配置Spring的applicationContext.xml文件:
a.获取jdbc.properties文件;

<!-- 导入资源文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />

b.配置c3p0数据源;

<!-- 配置c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>

c.配置sessionFactory;

<!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="mappingLocations" value="classpath:com/ynnu/sshdemo/domain/*.hbm.xml"></property>
    </bean>

d.配置Spring的声明式事务(配置Hibernate事务管理器->配置事务属性->配置事务切入点,再把切入点和事务属性关联起来).

<!-- 配置Spring的声明式事务 -->
    <!-- 1.配置Hibernate事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 2.配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" />
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

    <!-- 3.配置事务切入点,再把切入点和事务属性关联起来 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.ynnu.sshdemo.service.*.*(..))" id="txPointcut" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
    </aop:config>

⑥.配置完成,启动项目看能不能生成数据表(只有数据表生成策略为create或者update时才会自动生成).

3.加入Struts2
(1).加入jar包(apps中blank.war下lib中的包为标准.加入后若有重复的jar包,需要删除低版本的一个);
(2).在web.xml中配置Struts2的Filter;

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

(3).开始整合Spring,具体步骤:
①.加入Struts2的Spring插件的jar包(struts2-spring-plugin-2.3.28.jar);
②.在Spring的配置文件(applicationContext-*.xml)中正常配置Action,注意Action对应bean节点的的scope为prototype,其他默认;

新建applicationContext-Student.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
    <bean id="studentDao" class="com.ynnu.sshdemo.dao.StudentDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="studentService" class="com.ynnu.sshdemo.service.StudentService">
        <property name="studentDao" ref="studentDao"></property>
    </bean>

    <bean id="studentAction" class="com.ynnu.sshdemo.action.StudentAction" scope="prototype">
        <property name="studentService" ref="studentService"></property>
    </bean>
</beans>

③.在Struts2的配置文件(struts.xml)中配置Action时,class指向Action在ICO(applicationContext-*.xml中action对应的bean)的id.

<?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>
    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="error"></default-action-ref>
        <action name="error">
            <result>/WEB-INF/error.jsp</result>
        </action>
        <action name="stu-*" class="studentAction" method="{1}">
            <result name="list">/WEB-INF/stu-list.jsp</result>
        </action>
    </package>
</struts>

4.至此,SSH框架搭建完成!

5.可能出现的错误:
(1).错误:Error occured processing XML ‘org/springframework/transaction/interceptor/TransactionInterceptor’. See Error Log for more details

解决方案:加入aopalliance-1.0.jar包,引入之后,clean项目,错误就会消失!

(2).错误:java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

解决方案:加入aspectjweaver.jar包,导入,clean就OK了!

(3).错误:Error configuring application listener of class org.springframework.web.context.ContextLoaderListener

解决方案:所引用的jar包不在lib文件夹(根目录)中造成该错误,所以直接把所有jar包放在lib根目录下即可解决!

(4).错误:org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current

解决方案:在web.xml文件中加上以下的配置

<filter>
    <filter-name>SpringOpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    </filter>
<filter-mapping>
    <filter-name>SpringOpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

在此做下笔记, 便于以后快速搭建 ! !

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值