整合SSH所需要的配置及注意事项

目录

说明

整合SSH的环境是:Eclipse+Tomcat 服务器+Access数据库。

项目的结构

图片: 项目结构

包1
包2

配置web.xml

在整合ssh框架时需要先在web.xml中分别配置struts和spring,代码片.

<?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">
	<display-name>**项目名称**</display-name>

	
	<!-- 配置Struts2的核心过滤器,拦截/ *.action -->
	<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>
		<!--在csdn编辑器中我不知道如何把**"/和*打在一起"**,在粘贴时请删除两种中的空格 -->
		<url-pattern>/ *</url-pattern>
	</filter-mapping>

	
	<!-- 配置Spring的监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 引入spring配置文件applicationContext.xml,路径在classpath -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- 注册页面 -->
	<welcome-file-list>
		<welcome-file>hello.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

说明:
struts 2 的拦截器是 StrutsPrepareAndExecuteFilter,不要用错老版本的了。
提醒:
当复制粘贴网络上的配置文件 .xml时,在编辑器中可能会报错,有两个原因:
1. 复制粘贴后,编辑器没有反应过来。解决: 在编辑器中全选已经粘贴的内容,剪切,保存当前文件(空白的),再粘贴,这样就可以了。
2. 网络上复制粘贴的代码中含有不可见的字符,在编辑器中不能识别这样字符,就会报错。解决: 将报错的行的上下空白部分删除,再自行调整格式。

配置struts.xml

配置struts的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="ssh" extends="struts-default">

		<!-- 整合的第二中方法,将action交给spring的bean :"productAction" -->
		<action name="product_*" method="{1}" class="productAction">
			<result name="success">success.jsp</result>
		</action>
	</package>
</struts>

说明:
整合ssh时(不止一种方法),将struts的action交给spring的bean容器管理,在< action >的class属性中的值是spring配置文件中定义的真正的action类的bean的id。
提醒:
在action的class属性不能写类的名称。

配置applicationContext.xml

配置spring的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">


	<!-- 开启注解扫描,使用注解,ssh整合的非必须 -->
	<context:component-scan base-package="service" />

	<!-- 配置连接池,引入外部属性文件 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	
	<!-- 配置连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>


	<!-- 配置hibernate Session工厂bean及相关属性 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 注入连接池 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 设置加载hibernate的属性 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<!-- 引入hibernate的映射文件,注意路径 -->
		<property name="mappingResources">
			<list>
				<value>domain/Product.hbm.xml</value>
			</list>
		</property>
	</bean>


	<!-- 配置DAO的类,注入sessionFactory -->
	<bean id="productDao" class="dao.ProductDao">
		<!-- set方法注入sessionFactory-->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	
	<!-- 配置业务层的类 -->
	<bean id="productService" class="service.ProductService">
		<property name="productDao" ref="productDao"></property>
	</bean>
	
	
	<!-- 配置action的类和struts.xml中action的class属性呼应 ,在spring中管理action,多例 -->
	<bean id="productAction" class="action.ProductAction" scope="prototype">
		<property name="productService" ref="productService"></property>
	</bean>

	
	<!-- 配置事务管理器,ssh整合的非必须 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 开启注解事务,可以使用事务注解,ssh整合的非必须 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>


说明:
采用配置的方式来进行依赖注入,set方法进行,连接池property name=的名称是固定的。
提醒:
1. 在SSH整合中上述的事务管理是非必须的。
2. 在进行spring+hibernate整合时一定要注意包的版本,如果你用的包是4版本的,则在配置hibernate Session工厂bean是要用4版本的class,否则会报错。
3. 如何使用注解的方式来整合SSH,要使用hibernate5(可能是我的个人问题)。

配置User.hbm.xml

配置hibernate的XXX.hbm.xml,我是在eclipse中使用了hibernate插件来做的,代码片.

<?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 2019-x-x 10:30:47 by Hibernate Tools 3.4.0.CR1 -->

<!-- 映射配置文件 -->
<hibernate-mapping>
	<!-- 实体类和映射的表名 -->
    <class name="domain.Product" table="PRODUCT">
    	<!-- 主键属性,生成策略native -->
        <id name="pid" type="java.lang.Integer">
            <column name="PID" />
            <generator class="native" />
        </id>
        <!-- 属性 -->
        <property name="pname" type="java.lang.String">
            <column name="PNAME" />
        </property>
        <property name="price" type="java.lang.Double">
            <column name="PRICE" />
        </property>
    </class>
</hibernate-mapping>


说明:
使用的是hibernate插件生成的配置文件,使用spring整合hibernate不需要在配置hibernate.cfg.xml,网上有在eclipse中使用插件的教程。
提醒:
1. 要特别注意主键的生成策略。
2. 使用插件时表属性名和实体类属性名可能不一样。

配置jdbc.properties

配置连接数据库的资源文件的jdbc.properties,代码片.

jdbc.driverClass=com.hxtt.sql.access.AccessDriver
jdbc.url=jdbc:Access:///F:HrMS.accdb
jdbc.username=
jdbc.password=

说明:
在配置applicationContext.xml中的连接池时引用key名。
提醒:
1. 在资源文件中key名的命名不能和操作系统的默认名称冲突了,否则访问的是系统的值。

总结

	1. 在整合SSH时要注意包的版本问题。
	2. 配置的内容名称不要写错了。
	3. 类不要写错了,检验方式,安Ctrl
	4. 注意空值情况
	5. 多看异常,要有信心
**引用包是最大的困难(一步一步来)**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值