您的位置:首页 > 其它

SSH框架整合更新

2012-11-17 17:24 281 查看
SSH框架整合:

1. 数据库(mysql)的创建: create databases myoa default character set utf8;

2. 查看创建的数据 show create database myoa;

Contr+shift+t: 查找方言

Alt+shit+a快速去除重复的字段

Spring :

SHH框架整合:

spring与struts整合:

1.创建数据库

C:\Documents and Settings\Administrator>mysql -uroot -proot

mysql> create database myoa default character set utf8;

Query OK, 1 row affected (0.03 sec)

mysql> show create database myoa;

+----------+---------------------------------------------------------------+

| Database | Create Database |

+----------+---------------------------------------------------------------+

| myoa | CREATE DATABASE `myoa` /*!40100 DEFAULT CHARACTER SET utf8 */ |

+----------+---------------------------------------------------------------+

1 row in set (0.00 sec)

2.创建web项目

创建Web项目名为: MyOA

设置项目的编码方式为utf-8 . 设置后默认项目下所有文件与utf-8编码方式

项目名右击-->properties->Resource-->Text file encoding->Other-->UTF-8

3.配置环境

Struts 与spring整合

一: struts环境测试:

1. 导入相关的jar包

commons-fileupload-1.2.2.jar //文件上传和下载使用

commons-io-2.0.1.jar

commons-lang-2.5.jar //java.lang增强包

freemarker-2.3.18.jar

javassist-3.11.0.GA.jar

mysql-connector-java-5.1.7-bin.jar

ognl-3.0.3.jar //sruts标签库

struts2-core-2.3.1.1.jar //核心包

xwork-core-2.3.1.1.jar

2.配置web.xml文件

例如:

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

<web-app version="2.5" 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_2_5.xsd">
<display-name></display-name>

<filter><!-- 配置struts过滤器 -->

<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.写一个action类实现actionSupport

例如:

public class TestAction extends ActionSupport{

@Override

public String execute() throws Exception {

System.out.println("struts整合....");

return "success";

}

}

4.配置struts.xml文件

例如:

<struts>

<!-- 配置struts模式 -->

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

<!-- 配置struts扩展名 -->

<constant name="struts.action.extension" value="action"></constant>

<package name="mydefault" namespace="/" extends="struts-default">

<action name="test" class="test.TestAction">

<result>/WEB-INF/success.jsp</result>

</action>

</package>

</struts>

5.写一个返回页面success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<body>

<font color="red"> 测试struts环境搭建 </font>

</body>

</html>

6.进行struts环境搭建测试:

a.启动服务器

b.在浏览器中输入url访问: http://localhost:8080/myday62/test.action

c.浏览器中出现jsp页面中写的内容,( 测试struts环境搭建),表示配置成功.

二; Struts 与spring整合环境测试

1. strutsjar如上,导入spring jar包

aspectjrt.jar// Spring的AOP切面编程技

aspectjweaver.jar

spring.jar//spring核心包

log4j-1.2.15.jar

commons-logging.jar

cglib-nodep-2.1_3.ja// AOP切面编程所依赖的技术 动态代理和cglib 包

2. 配置文件; applicationContext.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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 配置spring自动扫描与装配bean -->

<context:component-scan base-package="cn.web.myoa.ssh.test"/>

</beans>

2. 配置web.xml文件:

如:

<!-- 配置spring用于创建容器的监听器 -->

<listener>

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

</listener>

<context-param>

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

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

</context-param>

3.将测试struts 类使用spring提供的注解注入bean

@Controller("testAction")

public class TestAction extends ActionSupport{

@Override

public String execute() throws Exception {

System.out.println("struts整合....");

return "success";

}

}

4.写一个测试类: 使用junit提供的测试方法进行测试:

如下: 使用@Test即可直接运行代码,无需写main函数

public class SpringStruts extends ActionSupport{

private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

@Test

public void testSpring()throws Exception {

TestAction ta = (TestAction) ac.getBean("testAction");

System.out.println("testAction----->"+ta);

}

}

5.测试在控制打印结果:

testAction----->cn.web.myoa.ssh.test.action.TestAction@55e55f

以上表示整合成功.

三: hibernate与spring测试:

1. 导入hibernate包

antlr-2.7.6.jar

c3p0-0.9.1.jar

commons-codec.jar

commons-collections-3.1.jar

commons-logging.jar

dom4j-1.6.1.jar

hibernate-jpa-2.0-api-1.0.0.Final.jar

hibernate3.jar

jta-1.1.jar

log4j-1.2.15.jar

slf4j-api-1.5.0.jar

slf4j-log4j12-1.5.0.jar

2. 配置hibernate.cfg.xml文件:

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- 该配置文件必须放在src目录下 -->

<hibernate-configuration>

<session-factory>

<!-- 数据库方言 -->

<property name="dialect">

org.hibernate.dialect.MySQL5Dialect

</property>

<!-- 显示SQL语句 -->

<property name="show_sql">true</property>

<!-- 通过映射文件自动产生sql语句 -->

<property name="hbm2ddl.auto">update</property>

<!-- 加载映射文件 -->

<mapping resource="cn/web/myoa/ssh/test/domain/User.hbm.xml" />

</session-factory>

</hibernate-configuration>

3. 配置applicationContext.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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置spring自动扫描与装配bean -->

<context:component-scan base-package="cn.web.myoa.ssh.test" />

<!-- 加载外部的properties配置文件 -->

<context:property-placeholder location="classpath:db.properties"/>

<!-- 配置sessionFactory -->

<bean id="sessionFactory"

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

<!-- 指定hibernate配置文件 -->

<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

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

</property>

</bean>

<!-- 配置数据源 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="${driverClass}" />

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

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

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

<!-- 初始化链接数 Default: 3-->

<property name="initialPoolSize" value="3"></property>

<!--连接池中保留的最小连接数。Default: 3 -->

<property name="minPoolSize" value="3"></property>

<!--连接池中最大连接数。Default: 15 -->

<property name="maxPoolSize" value="15"></property>

<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->

<property name="acquireIncrement" value="3"></property>

<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->

<property name="maxStatements" value="8"></property>

<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->

<property name="maxStatementsPerConnection" value="5"></property>

<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->

<property name="maxIdleTime" value="1800"></property>

</bean>

<!-- 配置spring事物管理 -->

<bean id="transactionManager"

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

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

</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

</beans>

4. 写一个测试类进行测试sessionFactory:

@Test

public void testTransaction()throws Exception {

SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");

System.out.println("sessionFactory------->"+sessionFactory);

}

5. 运行代码结果:

sessionFactory------->org.hibernate.impl.SessionFactoryImpl@162522

以上表示spring和hibernate整合成功

四: 事物测试(将事物交与spring进行管理)

//视图层

@Controller("testAction")

public class TestAction extends ActionSupport{

@Resource

private TestService testService;

@Override

public String execute() throws Exception {

System.out.println("struts整合....");

testService.save();

return "success";

}

}

//业务层

@Service

public class TestService {

@Resource

private SessionFactory sessionFactory;

@Transactional

public void save()throws Exception{

Session session = sessionFactory.getCurrentSession();

session.save(new User());

//int result = 9/0;//测试事物回滚

session.save(new User());

}

}

测试代码:

@Test

public void testTransaction()throws Exception {

TestService testService = (TestService) ac.getBean("testService");

testService.save();

System.out.println("testService------->"+testService);

}

整体测试,启动服务器,访问url: http://localhost:8080/myday62/test.action

Jsp页面显示出内容即可. 至此ssh框架整合完毕.

注:以上版本适用于javaEE5,如果使用javaEE6的则会出现sessionFactory不能注入错误信息

shh所需要的jar包,即配置文件如下:javaEE5环境,总共有24个jar包

antlr-2.7.6.jar

aspectjrt.jar

aspectjweaver.jar

c3p0-0.9.1.jar

cglib-nodep-2.1_3.jar

commons-collections-3.1.jar

commons-fileupload-1.2.1.jar

commons-io-1.3.2.jar

commons-logging.jar

dom4j-1.6.1.jar

freemarker-2.3.15.jar

hibernate-jpa-2.0-api-1.0.0.Final.jar

hibernate3.jar

javassist-3.12.0.GA.jar

jta-1.1.jar

log4j-1.2.15.jar

mysql-connector-java-5.1.5-bin.jar

ognl-2.7.3.jar

slf4j-api-1.5.0.jar

slf4j-log4j12-1.5.0.jar

spring.jar

struts2-core-2.1.8.1.jar

struts2-spring-plugin-2.1.8.1.jar

xwork-core-2.1.6.jar

需要配置的文件:

applicationContext.xml //主要用于加包下面的实体bean,直接在压缩包搜索即可,文件名不能修改

db.properties //属相文件,后缀必须为properties,主要存一些经常变换的属性,键值对存储形式,key=value

User.hbm.xml //实体/关系映射文件,主要是实体对象的属性和表之间的对应,文件名和类名一直,类名.hbm.xml,该文件与实体bean放在一起

hibernate.cfg.xml //固定文件,不可修改文件名,主要配置数据连接信息

log4j.properties//日志记录文件,该文件需要设置,为error级别,减少不必要显示的信息,配置log4j.rootLogger=error, stdout,其他带有日志级别(info,warn,debug)的全部使用#注释

struts.xml //用于配置action,文件名不可改变,只能放在src目录下.其他文件任意
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: