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 ...
随机推荐
- shell tips
1.shopt 命令可以设置shell的可选参数 shopt [-psu] [optname...] -s 开启某个选项 -u 关闭某个选项 -p 列出所有可设置的选项 其中开启extglob选项,s ...
- POJ1386Play on Words[有向图欧拉路]
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11846 Accepted: 4050 De ...
- java变量的初始化
public class Init { private int age;//非静态初始化语句<3> private static String name; //静态初始化语句,先初始化静态 ...
- Github 下载单个文件
前言 通常我们对Github上的项目都是完整的clone下来,但对于某些大型项目,或者某些时候只需要其中一两个文件,那该怎么办呢? 本文就是教你如何在github上下载单个文件. 方法 1.找到需要下 ...
- Flex布局教程及属性速查
一.Flex布局介绍 伸缩盒模型(flexbox)是一个新的盒子模型,意为"弹性布局",用来为盒状模型提供最大的灵活性,主要优化了UI布局.Flexbox的功能主要包手:简单使用一 ...
- python黑客编程之端口爆破
#coding:utf-8 from optparse import OptionParser import time,re,sys,threading,Queue import ftplib,soc ...
- NOIP模拟赛 行走
题目描述 “我有个愿望,我希望走到你身边.” 这是个奇异的世界,世界上的n-1条路联结起来形成一棵树,每条路有一个对应的权值ci. 现在我会给出q组询问或操作. 每次询问我会从一个x点走到y点,初始在 ...
- Android使用Java Mail API发送邮件
最近在考虑为已经有的一个应用程序增加一个用户反馈的功能,用户可以通过反馈功能将用户的意见和建议.程序出现的问题以一种更符合用户习惯的方式反馈回来.网上也有一些实现好的反馈程序的服务,包括bug的提交. ...
- 逗号分隔的字符串转换为行数据(collection)(续)
逗号分隔的字符串转行数据的存储过程一个: CREATE OR REPLACE FUNCTION SP_YX_SPLIT ( p_list CLOB, p_sep VARCHAR2 := ',' ) R ...
- Oracle 行转列,列转行
一.行转列1.1.初始测试数据表结构:TEST_TB_GRADESql代码:1 create table TEST_TB_GRADE2 (3 ID NUMBER(1 ...