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 ...
随机推荐
- UVA 12169 Disgruntled Judge【扩展欧几里德】
题意:随机选取x1,a,b,根据公式xi=(a*xi-1+b)%10001得到一个长度为2*n的序列,奇数项作为输入,求偶数项,若有多种,随机输出一组答案. 思路:a和b均未知,可以考虑枚举a和b,时 ...
- 洛谷10月月赛Round.1| P3399 丝绸之路 [DP]
题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚.西亚和欧洲,将那里的香 ...
- [No00001A]天天换图,百词斩到底在折腾啥
- 十种MySQL报错注入
1.floor() select * from test where id=1 and (select 1 from (select count(*),concat(user(),floor(rand ...
- lca入门———树上倍增法(博文内含例题)
倍增求LCA: father[i][j]表示节点i往上跳2^j次后的节点 可以转移为 father[i][j]=father[father[i][j-1]][j-1] 整体思路: 先比较两个点的深度, ...
- HTML DOM Document 对象
HTML DOM Document 对象 HTML DOM 节点 在 HTML DOM (Document Object Model) 中 , 每一个元素都是 节点: 文档是一个文档. 所有的HTML ...
- document.readyState等属性
document.readyState:判断文档是否加载完成.firefox不支持. 这个属性是只读的,传回值有以下的可能: 0-UNINITIALIZED:XML 对象被产生,但没有任何文件被加载. ...
- http协议(九)响应首部字段
响应首部字段: 服务器向客户端返回响应报文中所使用的字段,用于补充的附加信息.服务器信息.以及对客户端的附加要求等 1.Accept-Ranges 告知客户端服务器能否处理范围请求,以指定获取服务器的 ...
- java多线程系类:基础篇:05线程的等待与唤醒
概要 本章,会对线程等待/唤醒方法进行介绍.涉及到的内容包括:1. wait(), notify(), notifyAll()等方法介绍2. wait()和notify()3. wait(long t ...
- BZOJ 2733 【HNOI2012】 永无乡
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...