Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9整合
Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9整合教程并测试成功
一. 创建项目


二. 搭建struts-2.3.4.1
1.struts2必须的Jar包(放到WEB-INF/lib目录下):

2.配置struts2.3的过滤器
web.xml位置

web.xml内容
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="3.0"xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 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>
3.配置struts.xml(struts.xml在src目录下)
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<packagename="struts2"extends="struts-default">
<actionname="TestAction"class="com.xinvalue.action.TestAction">
<resultname="success">/test.jsp</result>
</action>
</package>
</struts>
4.测试struts2配置
新建一个TestAction

package com.xinvalue.action;
import com.opensymphony.xwork2.ActionSupport;
publicclass TestAction extends ActionSupport {
@Override
public String execute() throws Exception {
returnsuper.execute();
}
}
新建一个测试页面

成功界面

至此,struts2集成完毕!
二.整合Spring 3.2.3和Struts-2.3.4.1
1.必须的jar包
在配置好的struts的jar包的基础上,添加额外Struts jar包:struts2-spring-plugin-2.3.4.1.jar
commons-logging-1.1.1.jar
Spring的jar包:

2.web.xml配置
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="3.0"xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 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>
<!-- 创建spring工厂监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 告知spring context config location 的存储位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
</web-app>
3.spring的applicationContext.xml配置

applicationContext.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans>
三.整合Hibernate4.1.9
1.必需jar包
添加Hibernate jar文件
Hibernate压缩包中的
lib/required/

Spring中的jar文件

数据库连接池支持文件

以及连接mysql的jar
mysql-connector-java-5.1.22-bin.jar
2.配置文件applicationContext.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 数据库连接 -->
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<propertyname="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<propertyname="url">
<value>jdbc:mysql://localhost:3306/ssh2?characterEncoding=utf8</value>
</property>
<propertyname="username">
<value>root</value>
</property>
<propertyname="password">
<value></value>
</property>
</bean>
<!--Hibernate的Spring配置 -->
<beanid="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 数据库连接 -->
<propertyname="dataSource">
<reflocal="dataSource"/>
</property>
<!-- hibernate自身属性 -->
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.show_sql">true</prop>
<propkey="hibernate.format_sql">true</prop>
<propkey="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<!-- 解决no session found -->
<propkey="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<!-- 映射文件 -->
<propertyname="annotatedClasses">
<list>
<value>com.xinvalue.bean.User</value>
</list>
</property>
</bean>
<!-- 用户Dao -->
<beanid="userDao"class="com.xinvalue.dao.impl.UserDaoImpl"
scope="singleton">
<propertyname="sessionFactory">
<reflocal="sessionFactory"/>
</property>
</bean>
<!-- 用户Service -->
<beanid="userService"class="com.xinvalue.service.impl.UserServiceImpl"
scope="singleton">
<propertyname="userDao">
<reflocal="userDao"/>
</property>
</bean>
<!-- 用户Action -->
<beanid="saveUserAction"class="com.xinvalue.action.SaveUserAction"
scope="prototype">
<propertyname="userService">
<reflocal="userService"/>
</property>
</bean>
</beans>
创建测试数据库:
|
CREATE DATABASE `ssh2` ;
USE `ssh2`;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`userid`int(11)NOT NULL AUTO_INCREMENT,
`username`varchar(20)DEFAULT NULL,
`userpwd`varchar(20)DEFAULT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=2DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
insert into `users`(`userid`,`username`,`userpwd`)values (1,'terwer','123456');
|
创建bean

User.java
package com.xinvalue.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users")
publicclass User {
@Id
privateintuserId;
@Column(name = "username")
private String username;
@Column(name = "userpwd")
private String userpwd;
publicint getUserId() {
returnuserId;
}
publicvoid setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
returnuserpwd;
}
publicvoid setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
}
创建dao

UserDao.java
package com.xinvalue.dao;
import java.util.List;
import com.xinvalue.bean.User;
publicinterface UserDao {
public List<User> queryAllUsers();
boolean saveUser(User user);
}
UserDaoImpl.java
package com.xinvalue.dao.impl;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.xinvalue.bean.User;
import com.xinvalue.dao.UserDao;
publicclass UserDaoImpl implements UserDao {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
returnsessionFactory;
}
publicvoid setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public List<User> queryAllUsers() {
// TODO Auto-generated method stub
returnnull;
}
@Override
publicboolean saveUser(User user) {
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
try {
session.save(user);
tx.commit();
returntrue;
} catch (Exception e) {
if (e != null) {
tx.rollback();
}
}
returnfalse;
}
}
创建Service

UserService.java
package com.xinvalue.service;
import java.util.List;
import com.xinvalue.bean.User;
publicinterfaceUserService {
public List<User> queryAllUsers();
boolean saveUser(User user);
}
UserServiceImpl.java
package com.xinvalue.service.impl;
import java.util.List;
import com.xinvalue.bean.User;
import com.xinvalue.dao.UserDao;
import com.xinvalue.service.UserService;
publicclass UserServiceImpl implements UserService {
private UserDao userDao;
public UserDao getUserDao() {
returnuserDao;
}
publicvoid setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public List<User> queryAllUsers() {
returnuserDao.queryAllUsers();
}
@Override
publicboolean saveUser(User user) {
returnuserDao.saveUser(user);
}
}
创建Action

SaveUserAction.java
package com.xinvalue.action;
import com.opensymphony.xwork2.ActionSupport;
import com.xinvalue.bean.User;
import com.xinvalue.service.UserService;
publicclass SaveUserAction extends ActionSupport {
privateUserServiceuserService;
private String username;
private String userpwd;
public UserService getUserService() {
returnuserService;
}
publicvoid setUserService(UserService userService) {
this.userService = userService;
}
public String getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
returnuserpwd;
}
publicvoid setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
@Override
public String execute() throws Exception {
User user = new User();
user.setUsername(this.getUsername());
user.setUserpwd(this.getUserpwd());
boolean status=userService.saveUser(user);
System.out.println(status);
returnSUCCESS;
}
}
创建测试页面:
]
<body>
<s:formaction="SaveUserAction.action">
<s:textfieldname="username"label="用户名"/>
<s:passwordname="userpwd"label="密码"/>
<s:submitlabel="注册"/>
</s:form>
</body>
成功界面:


至此,全部整合完毕!所有jar包下载
转载注明本文地址: http://www.ablanxue.com/prone_9855_1.html
Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9整合的更多相关文章
- Struts2.3.4.1 + Spring3.1.2 + Hibernate4.1.6整合
1. Jar包 2. web.xml配置 3. struts.xml配置 4. hibernate.cfg.xml配置 5. applicationContext.xml配置 6. log4j.pro ...
- 基于Struts2.3.x+Spring3.2.x+Hibernate4.2.x+EasyUI1.3.4+Maven架构的示例程序
基于Struts2.3.x+Spring3.2.x+Hibernate4.2.x+EasyUI1.3.4+Maven架构的示例程序 不知道为什么,保存的时候显示有一个连接为违禁内容,可能是…………. ...
- SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结
下载地址: http://pan.baidu.com/s/1qWDinyk 一 开发环境 1.动态web工程 2.部分依赖 hibernate-release-4.1.0.Final.zip hibe ...
- Struts2+Hibernate4+Spring4框架整合搭建Java项目原型
收藏 http://www.cnblogs.com/mageguoshi/p/5850956.html Struts2+Hibernate4+Spring4框架整合搭建Java项目原型
- springmvc+spring3+hibernate4框架简单整合,简单实现增删改查功能
转自:https://blog.csdn.net/thinkingcao/article/details/52472252 C 所用到的jar包 数据库表 数据库表就不用教大家了,一张表,很简 ...
- Spring3系列4-多个配置文件的整合
Spring3系列4-多个配置文件的整合 在大型的Spring3项目中,所有的Bean配置在一个配置文件中不易管理,也不利于团队开发,通常在开发过程中,我们会按照功能模块的不同,或者开发人员的不同,将 ...
- SSH:Struts2.2+Hibernate3.6+Spring3.1分页示例[转]
参考资料 1 ssh分页(多个例子) http://useryouyou.iteye.com/blog/593954 2 ssh2分页例子 http://459104018-qq-com.iteye. ...
- JSP和Struts2、Hibernate、Spring3基础内容和原理
一.JSP工作原理 1.首先是利用客户端浏览器,然后由客户端浏览器请求JSP页面,向JSP服务器发出请求. 2.JSP服务器内部原理 JSP服务器首先在收到客户端传送过来的请求后,将JSP页面编译成S ...
- Spring3.1.2与Hibernate4.1.8整合
整合Spring3.1.2 与 Hibernate 4.1.8 首先准备整合jar: Spring3.1.2: org.springframework.aop-3.1.2.RELEASE.jar or ...
随机推荐
- Unity自学路线整理(参看微信公众号Unity墙外的世界的文章 )
目前还是个新手. 发现自己有时候还是会一脸蒙...的对着电脑屏幕不知所措,为了利用好在大学零散的时间所以整理一下学习unity的路线. 计划好才能更好的利用时间. 1. 先学好C#再去看引擎,我看的是 ...
- Student学生管理系统
1.定义各个层 2.添加各个层之间的引用 DAL 层调用Model BLL层调用DAL和Model UI层调用BLL和Model层 Model层供各个层调用 3.根据数据库建立实体类,每张表对应一个实 ...
- AC日记——统计单词数 openjudge 1.12 5
05:统计单词数 总时间限制: 1000ms 内存限制: 65536kB 描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次 ...
- AC日记——字符串P型编码 openjudge 1.7 31
31:字符串p型编码 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个完全由数字字符('0','1','2',…,'9')构成的字符串str,请写出str的p型编码串.例如: ...
- AC日记——单词翻转 1.7 27
27:单词翻转 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一个句子(一行),将句子中的每一个单词翻转后输出. 输入 只有一行,为一个字符串,不超过500个字符.单词之间以空 ...
- 将pdf文件通过itunes直接拖到ipad的ibooks里面
开始不太清楚进行过什么设置,使得以前可以直接通过拖动的方式复制pdf文件到ipad里面的方法不管用了.在帖子http://bbs.weiphone.com/read-htm-tid-864091-pa ...
- 基于jquery的消息提示框toastr.js
//消息提示全局配置 toastr.options = { "closeButton": false,//是否配置关闭按钮 "debug": false,//是 ...
- iOS UIControl 详解
UIControl是UIView的子类,当然也是UIResponder的子类.UIControl是诸如UIButton,UISwitch,UItextField等控件的父类,它本身包含了一些属性和方法 ...
- charCode与keyCode的区别
在标准浏览器下获取键盘按键我们可以使用e.which,但是非标准下没有这个属性,所以大部分情况下用keyCode,但是这是存在兼容性问题的.我们来看下他两的区别: onkeydown: e.keyCo ...
- gitlab两种连接方式:ssh和http配置介绍
gitlab环境部署好后,创建project工程,在本地或远程下载gitlab代码,有两种方式:ssh和http (1)ssh方式:这是一种相对安全的方式 这要求将本地的公钥上传到gitlab中,如下 ...