下面给出整合开发时Struts 2、Hibernate、Spring需要的JAR。
struts2-core-2.x.x.jar :Struts 2框架的核心类库
Xwork-core-2.x.x.jar :XWork类库,Struts 2在其上构建
ognl-2.6.x.jar :对象图导航语言(Object Graph Navigation Language),struts2框架通过其读写对象的属性
freemarker-2.3.x.jar :Struts 2的UI标签的模板使用FreeMarker编写
commons-fileupload-1.2.x.jar 文件上传组件,2.1.6版本后需要加入此文件
struts2-spring-plugin-2.x.x.jar :用于struts2集成Spring的插件
hibernate核心安装包下的(下载路径:http://sourceforge.net/projects/hibernate/files/hibernate3/3.3.1.GA/hibernate-distribution-3.3.1.GA-dist.zip/download)
hibernate3.jar
lib\bytecode\cglib\hibernate-cglib-repack-2.1_3.jar
lib\required\*.jar
hibernate 注解安装包下的(下载路径:http://sourceforge.net/projects/hibernate/files/hibernate-annotations/3.3.1.GA/hibernate-annotations-3.3.1.GA.zip/download)
hibernate-annotations.jar
lib\ejb3-persistence.jar、hibernate-commons-annotations.jar
Hibernate针对JPA的实现包(下载路径:http://sourceforge.net/projects/hibernate/files/hibernate-entitymanager/3.3.1.GA/hibernate-entitymanager-3.3.1.GA.zip/download)
hibernate-entitymanager.jar
Spring安装包下的
dist\spring.jar
lib\c3p0\c3p0-0.9.1.2.jar
lib\aspectj\aspectjweaver.jar、aspectjrt.jar
lib\cglib\cglib-nodep-2.1_3.jar
lib\j2ee\common-annotations.jar
lib\log4j\log4j-1.2.15.jar
lib\jakarta-commons\commons-logging.jar
lib\slf4j\slf4j-log4j12-1.5.0.jar
MYSQL数据库驱动jar
Spring的配置模版:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> </beans>
第一步在Spring中配置数据源:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="org.gjt.mm.mysql.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/SSH2?useUnicode=true&characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="password" value="mysql" /> <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="1" /> <!--连接池中保留的最小连接数。 --> <property name="minPoolSize" value="1" /> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="300" /> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="60" /> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="5" /> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="60" /> </bean>
第二步集成进hibernate :
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>org/spring/bean/Employee.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=false hibernate.format_sql=false </value> </property> </bean>
第三步使用Spring容器管理事务服务:
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!--使用基于注解方式配置事务 --> <tx:annotation-driven transaction-manager="txManager" />
整合后的beans.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 采用自动扫描bean的方式让spring容器管理 --> <context:component-scan base-package="org.spring" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="org.gjt.mm.mysql.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/SSH2?useUnicode=true&characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="password" value="mysql" /> <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="1" /> <!--连接池中保留的最小连接数。 --> <property name="minPoolSize" value="1" /> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="300" /> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="60" /> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="5" /> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="60" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>org/spring/bean/Employee.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=false hibernate.format_sql=false </value> </property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!--使用基于注解方式配置事务 --> <tx:annotation-driven transaction-manager="txManager" /> </beans>
配置hibernate实体映射文件Employee.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.spring.bean"> <class name="Employee"> <id name="username" length="20" /> <property name="password" length="20" not-null="true" /> <property name="gender" not-null="true" length="5"> <type name="org.hibernate.type.EnumType"> <param name="enumClass">org.spring.bean.Gender</param> <!-- 12为java.sql.Types.VARCHAR常量值,即保存枚举的字面值到数据库。如果不指定type参数,保存枚举的索引值(从0开始)到数据库 --> <param name="type">12</param> </type> </property> </class> </hibernate-mapping>
实体映射文件Employee.hbm.xml所对应的类
package org.spring.bean; /** * 实体类 */ public class Employee { private String username; private String password; private Gender gender = Gender.MAN; public Employee() { } public Employee(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; } }
其中枚举类型Gender类
package org.spring.bean; /** * 性别 */ public enum Gender { MAN, WOMEN; }
在web容器中使用Listener实例化spring容器和配置struts2
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!-- 对Spring容器进行实例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
struts2的配置文件模版struts.xml如下。常量struts.objectFactory=spring明确指出将由Spring负责创建Action实例。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 默认的视图主题 --> <constant name="struts.ui.theme" value="simple" /> <constant name="struts.objectFactory" value="spring" /> <package name="employee" namespace="/employee" extends="struts-default"> <action name="list" class="employeeAction"> <result name="list">/WEB-INF/page/employee.jsp</result> </action> <action name="manage_*" class="employeeManageAction" method="{1}"> <result name="add">/WEB-INF/page/employeeAdd.jsp</result> <result name="message">/WEB-INF/page/message.jsp</result> </action> </package> </struts>
为了能从spring容器中寻找到Action bean,要求action配置的class属性值和spring中bean的名称相同。
EmployeeAction:
package org.spring.action; import javax.annotation.Resource; import org.spring.service.EmployeeService; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; @Controller public class EmployeeAction { @Resource EmployeeService employeeService; public String execute() { ActionContext.getContext().put("employees", employeeService.list()); return "list"; } }
EmployeeManageAction
package org.spring.action; import javax.annotation.Resource; import org.spring.bean.Employee; import org.spring.service.EmployeeService; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; @Controller @Scope("prototype") public class EmployeeManageAction { @Resource EmployeeService employeeService; private Employee employee; public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } public String addUI() { return "add"; } public String add() { employeeService.save(employee); ActionContext.getContext().put("message", "保存成功"); return "message"; } }
employee.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>员工列表</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> ONGL: <br /> <s:iterator value="#request.employees"> <s:property value="username" /> ,<s:property value="password" />, <s:property value="gender" /> <br /> </s:iterator> <br /> <a href="<s:url action="manage_addUI" namespace="/employee"/>">员工添加</a> <br /> JSTL/EL: <br /> <c:forEach items="${employees}" var="employee"> ${employee.username}, ${employee.password}, ${employee.gender}<br /> </c:forEach> </body> </html>
employeeAdd.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>员工添加</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <s:form action="manage_add" namespace="/employee" method="post"> 用户名:<s:textfield name="employee.username"/><br/> 密码:<s:textfield name="employee.password"/><br/> 性别:<s:radio list="#{'MAN':'男','WOMEN':'女'}" listKey="key" listValue="value" name="employee.gender"/> <input type="submit" value="保存"/> </s:form> </body> </html>
message.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>结果信息</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <h1><font color="red"> <s:property value="#request.message"/> </font></h1> </body> </html>
使用spring解决struts2乱码问题。
<filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
使用spring解决hibernate因session关闭导致的延迟加载例外问题。
<filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <!-- 指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory.如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外 --> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
整合后的web.xml配置文件为
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!-- 对Spring容器进行实例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <!-- 指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory.如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外 --> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
EmployeeService
package org.spring.service; import java.util.List; import org.spring.bean.Employee; public interface EmployeeService { public void save(Employee employee); public void update(Employee employee); public Employee find(String username); public void delete(String... usernames); public List<Employee> list(); }
EmployeeServiceBean
package org.spring.service.impl; import java.util.List; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.spring.bean.Employee; import org.spring.service.EmployeeService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class EmployeeServiceBean implements EmployeeService { @Resource SessionFactory factory; public void delete(String... usernames) { for (String username : usernames) { factory.getCurrentSession().delete( factory.getCurrentSession().load(Employee.class, username)); } } @Transactional(propagation = Propagation.NOT_SUPPORTED) public Employee find(String username) { return (Employee) factory.getCurrentSession().get(Employee.class, username); } @SuppressWarnings("unchecked") @Transactional(propagation = Propagation.NOT_SUPPORTED) public List<Employee> list() { return factory.getCurrentSession().createQuery("from Employee").list(); } public void save(Employee employee) { factory.getCurrentSession().persist(employee); } public void update(Employee employee) { factory.getCurrentSession().merge(employee); } }
业务bean的测试类
package org.spring.junit; import java.util.List; import org.junit.BeforeClass; import org.junit.Test; import org.spring.bean.Employee; import org.spring.bean.Gender; import org.spring.service.EmployeeService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class EmployeeTest { private static EmployeeService employeeService; @BeforeClass public static void setUpBeforeClass() throws Exception { try { ApplicationContext act = new ClassPathXmlApplicationContext( "beans.xml"); employeeService = (EmployeeService) act .getBean("employeeServiceBean"); } catch (RuntimeException e) { e.printStackTrace(); } } @Test public void save() { employeeService.save(new Employee("xxx", "123456")); } @Test public void update() { Employee employee = employeeService.find("xxx"); employee.setGender(Gender.WOMEN); employeeService.update(employee); } @Test public void delete() { employeeService.delete("xxx"); } @Test public void list() { List<Employee> ems = employeeService.list(); for (Employee em : ems) System.out.println(em.getUsername()); } @Test public void find() { Employee em = employeeService.find("xxx"); System.out.println(em.getPassword()); } }
相关推荐
【jbpm4整合struts2+spring2.5+hibernate3.3】的集成是企业级应用开发中常见的技术栈组合,旨在利用各框架的优势实现高效、灵活的业务流程管理。jbpm4是一个开源的工作流管理系统,用于定义、执行和管理业务流程。...
总的来说,Struts2.1、Spring2.5和Hibernate3.3的整合为开发者提供了一个强大的开发平台,可以高效地构建复杂的Java Web应用。尽管现代框架如Spring Boot已逐渐取代这种传统的整合方式,但对于理解企业级开发的底层...
在这个"Struts2+Spring2.5+Hibernate3.3整合开发"项目中,"需要的JAR.txt"文件很可能包含了运行项目所需的库文件列表,包括这三大框架的版本以及其他可能依赖的JAR包。SSH2可能是指该项目的源代码,其中应该包含...
在Java Web开发中,"Eclipse中Struts2+Spring2.5+Hibernate3整合"是一个常见的技术栈,用于构建高效、模块化的Web应用程序。这三个框架分别是:Struts2(MVC框架)、Spring2.5(依赖注入和企业服务框架)和Hibernate...
标题 "ssh2(struts2+spring2.5+hibernate3.3+ajax)带进度条文件上传(封装成标签)" 涉及到的是一个基于Java Web的项目,利用了Struts2、Spring2.5、Hibernate3.3和Ajax技术,实现了文件上传并带有进度条显示的功能...
一、整合开发时Hibernate、Spring需要的JAR文件。 hibernate核心安装包下的(下载路径:http://www.hibernate.org/,点击“Hibernate Core”右边的“Downloads”): hibernate3.jar lib\bycode\cglib\hibernate-...
Struts2、Spring2.5和Hibernate3.3是经典的Java Web开发框架组合,它们各自在应用程序的不同层面提供了强大的功能。这个例子展示了如何将这三个框架整合在一起,实现一个简单的图书管理系统的增删改操作。 **Struts...
总结来说,Struts2+Spring2.5+Hibernate3.3整合是一个经典的Java Web开发模式,通过合理配置和集成这三个框架,可以构建出结构清晰、维护性强的企业级应用。在实际项目中,开发者需要对每个框架有深入理解,并熟练...
Struts2.1、Spring2.5和Hibernate3.3是Java Web开发中经典的三大框架,它们的整合应用是企业级应用开发中的常见实践。这三个框架的集成为开发者提供了强大的功能,包括模型-视图-控制器(MVC)架构、依赖注入(DI)...
此项目整合了目前主流和最前源的web开发技术:采用ehcache实现二级缓存(包含查询缓存);用sf4j及logback(log4j的升级版)记录日志;proxool(据说是dbcp和c3p0三者中最优秀的)做连接池;使用jquery的ajax实现仿...
Struts2.18、Spring2.5和Hibernate3.3是Java开发中经典的开源框架组合,被广泛用于构建企业级的电子商务网站。这个“基于Struts2.18+Spring2.5+Hibernate3.3+Annotation注解开发的电子商务网站demo”是一个很好的...
【标题】"jsr168 portlet(struts2+spring2.5+hibernate3.3)" 是一个基于Java技术栈的Web应用程序开发示例,它利用JSR168规范来创建portlet,同时结合了Struts2、Spring2.5和Hibernate3.3这三大框架,为构建可重用、...
这个压缩包包含了所有必要的jar文件,使得开发者可以直接引入到项目中,快速搭建起Struts2+Spring2.5+Hibernate3.3的基础环境,从而更专注于业务逻辑的实现。 总的来说,Struts2提供了用户交互的控制,Spring提供了...
2. **Hibernate3.3整合**: Hibernate3.3是ORM框架,它可以将Java对象映射到数据库表,简化数据库操作。整合Spring2.5时,我们可以在Spring配置文件中定义SessionFactory bean,并使用HibernateTemplate或...
Struts2.1+Spring2.5+Hibernate3.3是一个经典的Java Web开发技术栈,它们结合使用可以构建高效、可扩展的企业级应用程序。在这个简单的登录案例中,我们将深入理解这三个框架如何协同工作来实现用户身份验证。 ...