一、SSH整合之环境搭建

jar包导入

  1. Struts2框架

    • struts-2.3.24\apps\struts2-blank\WEB-INF\lib*.jar – Struts2需要的所有jar包
    • struts2-spring-plugin-2.3.24.jar —Struts2整合Spring的插件包
  2. Hibernate框架

    • hibernate-release-5.0.7.Final\lib\required*.jar – Hibernate框架需要的jar包
    • slf4j-api-1.6.1.jar – 日志接口
    • slf4j-log4j12-1.7.2.jar – 日志实现
    • mysql-connector-java-5.1.7-bin.jar – MySQL的驱动包
    • c3p0-0.9.2.1.jar,hibernate-c3p0-5.0.7.Final.jar,mchange-commons-java-0.2.3.4.jar – 整合c3p0需要用到的3个jar包
  3. Spring框架

    • IOC核心包4个
    • 日志包2个
    • AOP+AspectJ 共4个包
    • spring-jdbc-4.2.4.RELEASE.jar,spring-tx-4.2.4.RELEASE.jar – JDBC模板和事务核心包
    • spring-test-4.2.4.RELEASE.jar – Spring整合JUnit测试包
    • spring-orm-4.2.4.RELEASE.jar – Spring整合Hibernate核心包
    • spring-web-4.2.4.RELEASE.jar – Spring整合Struts2核心包

以上所有jar包都在之前介绍struts、hibernate、spring的文章中找到。

这里我将所有SSH整合需要用到的jar包都上传到百度云,一共49个jar包,下载链接 ,提取码:w9mx ,有需要的可自行下载。

注意

Struts2的jar包中有一个javassist-3.11.0.GA.jar,而Hibernate的jar包中也有一个javassist-3.18.1-GA.jar,因此整合的时候会导致包冲突的问题,所以需要删除其中一个,这里我保留高版本的。

创建javabean

package blog.csdn.net.mchenys.domain;

public class Customer {
	private Long id;
	private String name;
	private String source;
	private String level;
	private String phone;

	//get set...

	@Override
	public String toString() {
		return "Customer [id=" + id + ", name=" + name + ", source=" + source + ", level=" + level+ ", phone=" + phone
				+ "]";
	}

}

引入配置文件

1.Struts2框架
在web.xml中配置核心的过滤器

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

在src目录下创建struts.xml,用来配置Action

<?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">
		
	</package>
</struts>

2.Hibernate框架
在src目录创建hibernate.cfg.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/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:///ssh_01</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">1234</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<!-- 可选配置 -->
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!-- 配置C3P0的连接池 -->
		<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		
		<!-- 映射配置文件 -->
		<mapping resource="blog/csdn/net/mchenys/domain/Customer.hbm.xml"/>
	</session-factory>
	
</hibernate-configuration>	

在JavaBean所在的包下创建映射的Customer.hbm.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
<hibernate-mapping>
	
	<class name="blog.csdn.net.mchenys.domain.Customer" table="cst_customer">
		<id name="id" column="cust_id">
			<generator class="native"/>
		</id>
		
		<property name="name" column="cust_name"/>
		<property name="source" column="cust_source"/>
		<property name="level" column="cust_level"/>
		<property name="phone" column="cust_phone"/>
		
	</class>
	
</hibernate-mapping>    

3.Spring框架
在web.xml配置整合WEB的监听器

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

在web.xml配置一个过滤器

<!--解决延时加载session关闭的问题, 要注意需要在struts2的核心过滤器之前进行配置 -->
<filter>
	<filter-name>OpenSessionInViewFilter</filter-name>
	<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>OpenSessionInViewFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

在src目录下创建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">
	
	
	
</beans>

在src目录下log4j.proerties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
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 mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.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

完成后,项目的包结构如下:
在这里插入图片描述

完整的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">
	
	<!-- spring的web监听器,用于程序启动的时候初始化applicationContext.xml -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<!--解决延时加载session关闭的问题, 要注意需要在struts2的核心过滤器之前进行配置 -->
	<filter>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 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>
	</filter-mapping>

	
</web-app>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值