Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9整合

java教程|Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9整合教程并测试成功一.创建项目二.搭建struts-2.3.4.11.struts2必须的Jar包(放到WEB-INF/...
 

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

posted @ 2016-03-11 00:11  西北逍遥  阅读(227)  评论(0编辑  收藏  举报