Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9_设备步调
添加时间:2013-7-4 点击量:
一. 创建项目
二. 搭建struts-2.3.4.1
1.struts2必须的Jar包(放到WEB-INF/lib目次下):
2.设备struts2.3的过滤器
web.xml地位
web.xml内容
<?xmlversion=1.0encoding=UTF-8?>
<web-appversion=3.0xmlns=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.StrutsPrepareAnduteFilter</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.0encoding=UTF-8?>
<!DOCTYPEstrutsPUBLIC
-//Apache Software Foundation//DTD Struts Configuration 2.3//EN
http://struts.apache.org/dtds/struts-2.3.dtd>
<struts>
<packagename=struts2extends=struts-default>
<actionname=TestActionclass=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.0encoding=UTF-8?>
<web-appversion=3.0xmlns=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.StrutsPrepareAnduteFilter</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.0encoding=UTF-8?>
<beansxmlns=http://www.springframework.org/schema/beans
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexmlns:p=http://www.springframework.org/schema/p
xmlns:tx=http://www.springframework.org/schema/txxmlns: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.0encoding=UTF-8?>
<beansxmlns=http://www.springframework.org/schema/beans
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexmlns:p=http://www.springframework.org/schema/p
xmlns:tx=http://www.springframework.org/schema/txxmlns: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=dataSourceclass=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=userDaoclass=com.xinvalue.dao.impl.UserDaoImpl
scope=singleton>
<propertyname=sessionFactory>
<reflocal=sessionFactory/>
</property>
</bean>
<!-- 用户Service -->
<beanid=userServiceclass=com.xinvalue.service.impl.UserServiceImpl
scope=singleton>
<propertyname=userDao>
<reflocal=userDao/>
</property>
</bean>
<!-- 用户Action -->
<beanid=saveUserActionclass=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=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
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=usernamelabel=用户名/>
<s:passwordname=userpwdlabel=暗码/>
<s:submitlabel=注册/>
</s:form>
</body>
成功界面:
至此,全部整合完毕!
若是大师有题目可以留言,或发邮件cyutyw@sina.com。感谢大师对新价值网的支撑,我是新价值网的博主小唐(网名terwer)。
附:所有jar包:ss2-libs
本文来自笔者博客:原文地址 http://www.xinvalue.com/html/2013/07/04/485.html
容易发怒的意思就是: 别人做了蠢事, 然后我们代替他们, 表现出笨蛋的样子。—— 蔡康永
一. 创建项目
二. 搭建struts-2.3.4.1
1.struts2必须的Jar包(放到WEB-INF/lib目次下):
2.设备struts2.3的过滤器
web.xml地位
web.xml内容
<?xmlversion=1.0encoding=UTF-8?>
<web-appversion=3.0xmlns=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.StrutsPrepareAnduteFilter</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.0encoding=UTF-8?>
<!DOCTYPEstrutsPUBLIC
-//Apache Software Foundation//DTD Struts Configuration 2.3//EN
http://struts.apache.org/dtds/struts-2.3.dtd>
<struts>
<packagename=struts2extends=struts-default>
<actionname=TestActionclass=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.0encoding=UTF-8?>
<web-appversion=3.0xmlns=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.StrutsPrepareAnduteFilter</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.0encoding=UTF-8?>
<beansxmlns=http://www.springframework.org/schema/beans
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexmlns:p=http://www.springframework.org/schema/p
xmlns:tx=http://www.springframework.org/schema/txxmlns: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.0encoding=UTF-8?>
<beansxmlns=http://www.springframework.org/schema/beans
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexmlns:p=http://www.springframework.org/schema/p
xmlns:tx=http://www.springframework.org/schema/txxmlns: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=dataSourceclass=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=userDaoclass=com.xinvalue.dao.impl.UserDaoImpl
scope=singleton>
<propertyname=sessionFactory>
<reflocal=sessionFactory/>
</property>
</bean>
<!-- 用户Service -->
<beanid=userServiceclass=com.xinvalue.service.impl.UserServiceImpl
scope=singleton>
<propertyname=userDao>
<reflocal=userDao/>
</property>
</bean>
<!-- 用户Action -->
<beanid=saveUserActionclass=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=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
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=usernamelabel=用户名/>
<s:passwordname=userpwdlabel=暗码/>
<s:submitlabel=注册/>
</s:form>
</body>
成功界面:
至此,全部整合完毕!
若是大师有题目可以留言,或发邮件cyutyw@sina.com。感谢大师对新价值网的支撑,我是新价值网的博主小唐(网名terwer)。
附:所有jar包:ss2-libs
本文来自笔者博客:原文地址 http://www.xinvalue.com/html/2013/07/04/485.html
容易发怒的意思就是: 别人做了蠢事, 然后我们代替他们, 表现出笨蛋的样子。—— 蔡康永