失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【Struts2+Spring3+Hibernate3】SSH框架整合实现CRUD_1.0

【Struts2+Spring3+Hibernate3】SSH框架整合实现CRUD_1.0

时间:2019-08-19 11:15:31

相关推荐

【Struts2+Spring3+Hibernate3】SSH框架整合实现CRUD_1.0

作者: hzboy192@Blog: /peng_hao1988

版本总览:/peng_hao1988/article/details/9026897

实现步骤:

一、导入Spring3.0、Hibernate3.0、Struts2开发库。

二、配置web.xml文件的内容,如下:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="/2001/XMLSchema-instance" xmlns="/xml/ns/javaee" xmlns:web="/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd" version="3.0"><display-name></display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><listener><description>Spring core configuration</description><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><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>*.action</url-pattern></filter-mapping></web-app>

1.如上struts2的核心控制器filter也可配置为如下类

org.apache.struts2.dispatcher.FilterDispatcher

2.FilterDispatcher和StrutsPrepareAndExecuteFilter、StrutsPrepareFilter、

ExecuteFilter的区别参见:http://peng-/admin/blogs/1472063

三、Spring的配置文件applicationContext.xml的配置内容如下:

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:p="/schema/p"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.0.xsd"><bean id="dataSource" class="mons.dbcp.BasicDataSource"><property name="driverClassName"value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/myssh"></property><property name="username" value="root"></property><property name="password" value="123"></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- if use the hibernate.cfg.xml as dataSource configuration, cancel the annotations --><!-- remove other properties and dataSource bean. --><!-- <property name="configLocation" value="classpath:hibernate.cfg.xml" />--><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property><property name="mappingResources"><list><value>com/hzboy/orm/Userinfo.hbm.xml</value></list></property></bean><bean id="userAct" class="com.hzboy.action.UserManagerAct" scope="prototype"><property name="userService" ref="userService"></property></bean><bean id="userService" class="com.hzboy.service.UserManagerService"><property name="dao" ref="dao"></property></bean><bean id="dao" class="com.hzboy.dao.BaseDao"><property name="sessionFactory" ref="sessionFactory"/></bean></beans>

1.Demo默认Hibernate的DataSource、HibernateProperties、 MappingResources信息配置到applicationContext.xml文件中。如果使用Hibernate.cfg.xml配置这些信息,需要在applicationContext.xml文件中将configLocation这个属性去掉注释,将其他的property注释或删除。

2.applicationContext.xml默认放在webRoot\WEB-INF\路径下,如果想要将spring的配置文件移动到其他文件夹或修改名字,需要在web.xml中添加如下代码:

<context-param><description>我的spring配置文件放在WEB-INF根目录下,文件名为springconfig.xml</description><param-name>contextConfigLoaction</param-name><param-value>/WEB-INF/springconfig.xml</param-value></context-param>

3.Demo默认Hibernate的DataSource、HibernateProperties、MappingResources信息配置到applicationContext.xml文件中。如果使用Hibernate.cfg.xml配置这些信息,需要在applicationContext.xml文件中将configLocation这个属性去掉注释,并将其他的property注释或删除。Hibernate.cfg.xml配置如下:

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration><session-factory><property name="hbm2ddl.auto">update</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.url">jdbc:mysql://localhost:3306/myssh</property><property name="connection.username">root</property><property name="connection.password">123</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="myeclipse.connection.profile">MySQL</property><mapping resource="com/hzboy/orm/Userinfo.hbm.xml" /></session-factory></hibernate-configuration>

四、配置struts2的配置文件struts.xml如下:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "/dtds/struts-2.1.dtd"><struts><constant name="struts.objectFactory" value="spring" /><package name="myApp" extends="struts-default"><global-results><result name="error">/WEB-INF/jsp/error.jsp</result><result name="success">/WEB-INF/jsp/success.jsp</result></global-results><action name="userManagerAct" class="userAct" method="doLogin"><result name="login">/index.jsp</result></action><action name="query" class="userAct" method="doQuery" /><action name="delete" class="userAct" method="doDelete" /><action name="edit" class="userAct" method="doEdit"><result name="editUser">/WEB-INF/jsp/editUser.jsp</result></action><action name="add" class="userAct" method="doAdd"><result name="addUser">/WEB-INF/jsp/addUser.jsp</result></action></package></struts>

1.当使用spring管理struts2的 action时要注意需在struts2的action配

置文件中添加如下代码:

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

并且需要将struts2-spring-plugin-2.2.1.jar添加到classpath中去。

2.如要将Struts2的默认配置文件struts.xml文件重命名或移动到WEB-INF到下去,需要在web.xml的struts2过滤器中添加如下代码:

<init-param><description>我的struts配置文件放在WEB-INF根目录下,文件名为action.xml</description><param-name>config</param-name><param-value>struts-default.xml,struts-plugin.xml,../action.xml</param-value></init-param>

五、基本配置已经弄完了,在用工具添加MyEclipse和Eclipse添加开发库是要注意,尽量使用高版本的,如果是工具自带的库,一般不会出现问题,如果有错误的话,一般是JAR包冲突,把一些名字一样版本号不一样的包删除低版本的。如果是用户自定义的开发库最好不要随意添加JAR包,去官方下载完整的开发库,根据需要添加,如果是在不知道那些是需要的,那就全部添加进去。

注意:有部分网友反馈示例代码报错,原因是之前使用的是myeclipse,自带的lib下有servlet-api.jar文件,所以打包代码是没有吧这个jar弄进去,所以在eclipse或其他IDE下可能会报错,解决办法将将Tomcat\lib下的servlet-api.jar文件添加的WEB-INF\lib文件夹中。

示例代码:/detail/peng_hao1988/4191421

》》》》》》》》》》》》》》》》转载请注明出处《《《《《《《《《《《《《《《《

如果觉得《【Struts2+Spring3+Hibernate3】SSH框架整合实现CRUD_1.0》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。