struts2、spring、hibernate手工整合

[quote][/quote]用例的版本号:struts2-2.16
1、增加struts2的commons-logging-1.0.4.jar、freemarker-2.3.13.jar、ognl- 2.6.11.jar、struts2-core-2.1.6.jar、xwork-2.1.2.jar、commons-fileupload-1.2.1.jar这几个包到工程里。
2、从struts2的example里面copy一个struts.xml文件到项目里面
3、在web.xml里面配置struts2的核心过滤器及struts.xml里配置package
 <!-- 配置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>
------------------------------------------
<struts>

<package name="struts2" extends="struts-default">

</package>

</struts>

4、引入spring2.5的spring.jar包,并copy一个applicationContext.xml到项目里
5、在web.xml里面增加一个spring的监听器

<!-- 配置spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

6、引入struts2提供的spring与struts2的整合插件包,struts2-spring-plugin-2.1.6.jar
7、写一个注册service,配置applicationContext.xml,struts.xml

<!-- action的配置,每个action一个 -->
<action name="LoginAction" class="loginAction">
<result name="success">/result.jsp</result>
<result name="input">/login.jsp</result>
</action>
-------------------------------------------
<!-- 要注入struts2的Action里面的service -->
<bean id="loginServiceImpl" class="com.struts2.service.impl.LoginServiceImpl" />

<!-- struts2里面的action配置 -->
<bean id="loginAction" class="com.struts2.action.LoginAction">
<property name="loginService" ref="loginServiceImpl" />
</bean>
--------------------------------------------
jsp页面上增加对struts2标签的支持:
<%@ taglib prefix="s" uri="/struts-tags"%>


注:关于el表达式在web.xml用2.5版本的xsd不起作用的处理方法:
1.在jsp页面上增加:[color=red]<%@ page isELIgnored="false"%>[/color]
2.修改web.xml文件的dtd定义,还改回2.4版本

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

自此,struts2和spring2.5的integrate完成。下面整合hibernate3.3和spring2.5

--------------------------------------------------------------------------

1、增加hibernate的jar包:hibernate3.jar,以及/lib/required目录下的所有jar包,再增加slf4j-api-1.5.8.jar这个jar包的实现jar包slf4j-log4j12-1.5.8.jar及log4j-1.2.15.jar
2、在web.xml文件里增加如下的配置:

<!--applicationContext.xml配置文件分成多个,让配置的spring监视器每个都能加载到 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>

<!-- 配置Spring 自动管理Session-->
<filter>
<filter-name>hibernateSessionFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateSessionFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

<!--配置中文编码的过滤器 由spring管理 -->
<filter>
<filter-name>encodingFilter</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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


3、配置hibernate.cfg.xml文件:

<?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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/oa</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<mapping resource="com/oa/model/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>


4、将applicationContext.xml文件分成3个,分别是applicationContext-action.xml,applicationContext-beans.xml,applicationContext-common.xml,放到conf文件目录里,统一方便管理各种bean。


----------------------------------------------------------------
几个xml文件的头:
1.hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

2.Person.hbm.xml

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

<hibernate-mapping>

<class name="com.oa.model.Person" table="t_person">
<id name="id" access="field">
<generator class="native" />
</id>
<property name="name" access="field"/>
<property name="sex" access="field"/>
<property name="address" access="field"/>
<property name="duty" access="field"/>
<property name="phone" access="field"/>
<property name="description" access="field"/>

</class>

</hibernate-mapping>

3.struts2.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

4.applicationContext.xml 包含aop提示

<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


hibernate对象生成表的工具代码:

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {

public static void main(String[] args) {

//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();

SchemaExport export = new SchemaExport(cfg);

export.create(true, true);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值