Spring-3.2.0+Struts2.3.4+JPA2.0整合,其所需的jar包与Spring-3.2.0 +Struts-2.3.4+Hibernate-4.1.6所需的jar包基本相同.下面列出SSH整合所需要的jar包.wKioL1L8ZdmC816LAALpayoLuQc466.jpg

如上图所示是Spring-3.2.0 +Struts-2.3.4+Hibernate-4.1.6的jar包,这些jar包在各官网上都有提供下载:

Spring的jar包下载地址:http://www.springsource.org/spring-community-download(需要填写一些信息)

Hibernate的jar包下载地址:http://www.hibernate.org/downloads.html

Struts的jar包下载地址:http://struts.apache.org/download.cgi


注意在jar包整合,由于spring的jar已经相应独立开,还有其它jar也不同,所以要注意相关jar包的版本,不要以为包引多了没关系,jar冲突和版本不同也会导致整合失败.另外用MyEclipse(Eclipse)新建项目时会自动引入J2EE相关的jar包,建议删除,手动引入.

下面使用MySQL做演示:

首先我们在(src)下新建jdbc.properties文件用来配置数据库基本信息,其内容如下:

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://127.0.0.1:3306/shopping

jdbc.username=root

jdbc.password=619100



然后我们新建Spring配置文件beans.xml,将其放入在classpath(src)下,其内容如下:

<?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-3.2.xsd

          http://www.springframework.org/schema/context

          http://www.springframework.org/schema/context/spring-context-3.2.xsd

          http://www.springframework.org/schema/aop

          http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

          http://www.springframework.org/schema/tx

          http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<context:annotation-config/>

<!-- 声明Spring要管理的范围 -->

<context:component-scan base-package="gd.hz.shopping" />

<!-- 读取jdbc.properties配置文件 -->

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<value>classpath:jdbc.properties</value>

</property>

</bean>

<bean id="dataSource" destroy-method="close"

class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName"

value="${jdbc.driverClassName}" />

<property name="url" value="${jdbc.url}" />

<property name="username" value="${jdbc.username}" />

<property name="password" value="${jdbc.password}" />

</bean>


<!-- 创建sessionFactory -->

<bean id="sessionFactory"

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="packagesToScan">

<list>

<!-- 需要映射的实体类,指定到实体类所在的包即可 -->

<value>gd.hz.shopping.model</value>

</list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.format_sql">true</prop>

       <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>

       <prop key="hibernate.show_sql">true</prop>

       <prop key="current_session_context_class">thread</prop>

       <prop key="connection.pool_size">3</prop>

       <prop key="hibernate.hbm2ddl.auto">update</prop>

       <prop key="javax.persistence.validation.mode">none</prop>

       <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>

</props>

</property>

</bean>

<!-- 开启事务管理 -->

<bean id="txManager"

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<aop:config>

<aop:pointcut id="bussinessService"

expression="execution(public * gd.hz.shopping.service.*.*(..))" />

<aop:advisor pointcut-ref="bussinessService"

advice-ref="txAdvice" />

</aop:config>

<!-- 使用声明式事务 -->

<tx:advice id="txAdvice" transaction-manager="txManager">

<tx:attributes>

<tx:method name="find*" read-only="true" propagation="REQUIRED" />

<tx:method name="save*" propagation="REQUIRED"/>

<tx:method name="modify*" propagation="REQUIRED"/>

<tx:method name="remove*" propagation="REQUIRED"/>

</tx:attributes>

</tx:advice>

</beans>



新建我们的struts配置文件struts.xml,内容如下:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">


<struts>

<constant name="struts.devMode" value="true" />

<constant name="struts.i18n.encoding" value="UTF-8" />

</struts>



然后配置我们的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="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">


<!-- 加载Spring配置文件 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:beans.xml</param-value>

</context-param>

<!-- ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>


<!-- 指定字符集 -->

<filter>

<filter-name>encodingFilter</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>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>


<!-- 保持session到页面 -->

<filter>

<filter-name>openSessionInView</filter-name>

<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>

<init-param>

<param-name>sessionFactoryBeanName</param-name>

<param-value>sessionFactory</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>openSessionInView</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>


<!-- 声明struts管理请求 -->

<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>


新建Dao接口:

package gd.hz.shopping.dao;


import gd.hz.shopping.model.ProductType;


public interface ProductTypeDao {

public ProductType selProductType(int id);

}

实现Dao接口:

package gd.hz.shopping.dao.impl;


import javax.annotation.Resource;


import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.stereotype.Component;


import gd.hz.shopping.dao.ProductTypeDao;

import gd.hz.shopping.model.ProductType;


@Component("productTypeDao")

public class ProductTypeDaoImpl implements ProductTypeDao {

private SessionFactory sessionFactory = null ;

@Resource(name="sessionFactory")

public void setSessionFactory(SessionFactory sessionFactory) {

this.sessionFactory = sessionFactory;

}


@Override

public ProductType selProductType(int id) {

Session session = sessionFactory.getCurrentSession() ;

ProductType productType = (ProductType)session.get(ProductType.class , id);

return productType ;

}

}


 

Manager层接口:

package gd.hz.shopping.service;


import gd.hz.shopping.model.ProductType;


public interface ProductTypeMnager {

public ProductType findProductType(int id) ;

}



Manager层接口实现:

package gd.hz.shopping.service.impl;


import javax.annotation.Resource;


import org.springframework.stereotype.Component;


import gd.hz.shopping.dao.ProductTypeDao;

import gd.hz.shopping.model.ProductType;

import gd.hz.shopping.service.ProductTypeMnager;


@Component("productTypeMnager")

public class ProductTypeMnagerImpl implements ProductTypeMnager {


private ProductTypeDao productTypeDao = null ;

@Resource(name="productTypeDao")

public void setProductTypeDao(ProductTypeDao productTypeDao) {

this.productTypeDao = productTypeDao;

}


@Override

public ProductType findProductType(int id) {

return productTypeDao.selProductType(id) ;

}


}


新建控制类ProductTypeAction:

package gd.hz.shopping.action;


import javax.annotation.Resource;


import gd.hz.shopping.model.ProductType;

import gd.hz.shopping.service.ProductTypeMnager;


import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Component;


import com.opensymphony.xwork2.ActionSupport;


@Component("productTypeAction")

@Scope("prototype")

public class ProductTypeAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private ProductTypeMnager productTypeMnager = null ;

private ProductType productType = null ;

@Resource(name="productTypeMnager")

public void setProductTypeMnager(ProductTypeMnager productTypeMnager) {

this.productTypeMnager = productTypeMnager;

}

public ProductType getProductType() {

return productType;

}

public void setProductType(ProductType productType) {

this.productType = productType;

}

public String test()

{

productType = productTypeMnager.findProductType(1) ;

return "test" ;

}

}



配置struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">


<struts>

<constant name="struts.devMode" value="true" />

<constant name="struts.i18n.encoding" value="UTF-8" />

<package name="productType" namespace="/" extends="OA_default">

<action name="productTypeAction" class="productTypeAction">

<result name="test">/org/add_input.jsp</result>

</action>

</package>

</struts>

启动服务器,然后手动插入测试数据:

insert into ProductType(name) values('lfd');

新建jsp页面:

引入jstl 标签:<%@ taglib prefix="s"  uri="/struts-tags" %>

<%@ page language="java" contentType="text/html; charset=UTF-8"

   pageEncoding="UTF-8"%>

<%@ taglib prefix="s"  uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>ssh整合测试</title>

</head>

<body>

 <%--使用OGNL表达式--%>  

  Hello:<s:property value="productType.name"/><br>

</body>

</html>