今天开个坑java ssh三大框架搭建整合(注解+XML 用全注解不是很好,有点地方需要用模板的时候XML直接可以复制一段)
1 所用框架、技术
编号 |
工具 |
版本 |
说明 |
|
Struts 2 |
2.3.20 |
|
|
Hibernate |
4.3.9 |
实现持久化操作 |
|
Spring |
4.1.5 |
|
|
Junit |
4 |
单元测试 |
2. 开发环境
操作系统 |
Windows 7 |
开发工具 |
Eclipse Java EE |
数据库 |
Oracle 11g |
Web容器 |
Tomcat 7.0.63 |
JAVA |
JDK 1.7 |
3.建立project
4.添加Junit
configure Build Path 后点Add library 选junit4 即可
5.添加Struts2
copy所需的jar包到lib文件夹(先排除struts2-spring-plugin.jar后面会说明原因)
准备struts.xml,web.xml
模板,直接在Struts文件夹里面搜索文件名struts.xml,web.xml。
web.xml(示例)
我这些XML 元素web-app 有版本信息,不一致会报错。
1 xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 5 6 7 8 <filter> 9 <filter-name>struts2filter-name> 10 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class> 11 filter> 12 <filter-mapping> 13 <filter-name>struts2filter-name> 14 <url-pattern>/*url-pattern> 15 filter-mapping> 16 17 18 19 <welcome-file-list> 20 <welcome-file>index.jspwelcome-file> 21 welcome-file-list> 22 23 web-app>
struts.xml
1 xml version="1.0" encoding="UTF-8" ?> 2 DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 9 <constant name="struts.devMode" value="true" /> 10 11 <constant name="struts.action.extension" value="action" /> 12 13 <constant name="struts.ui.theme" value="simple" /> 14 15 16 <package name="default" namespace="/" extends="struts-default"> 17 18 19 20 <action name="test" class="edu.hainu.knowledge.test.StrutsAction"> 21 <result name="success">/index.jspresult> 22 action> 23 24 25 package> 26 27 28 struts>
1 package edu.hainu.knowledge.test; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 6 public class StrutsAction extends ActionSupport{ 7 8 //用于 测试单个Struts是否成功 9 public String execute(){ 10 System.out.println("success"); 11 return "success"; 12 } 13 }
启动观察控制台有没有报错,报错的自行百度
用浏览器http://localhost:8080/edu.hainu.knowledge/test.action
http://服务器所在ip:端口号/projectName/actionName.action(注意我已经在struts.xml 配置类后缀为action)
其实这个地址可以也不用记住,我们可以直接run 一个页面,eclipse会自动访问该页面
页面成功跳转,添加struts2成功。
6.添加spring
applicationContext.xml (示例)
同样的可以去spring文件夹搜索applicationContext.xml (QAQ 不知道为什么找不到)
配置一下 component-scan base-package="edu.hainu.knowledge" 就是Sping将会扫描edu.hainu.knowledge包所有类上的注解
1 xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 11 12 13 <context:component-scan base-package="edu.hainu.knowledge">context:component-scan> 14 15 beans>
添加StrutsAction 的注解
@Controller
@Scope("prototype")
1 package edu.hainu.knowledge.test; 2 3 import org.springframework.context.annotation.Scope; 4 import org.springframework.stereotype.Controller; 5 6 import com.opensymphony.xwork2.ActionSupport; 7 8 @Controller 9 @Scope("prototype") 10 public class StrutsAction extends ActionSupport{ 11 12 //用于 测试单个Struts是否成功 13 public String execute(){ 14 System.out.println("success"); 15 return "success"; 16 } 17 }
在junit的Spring类
1 package edu.hainu.knowledge.test; 2 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 8 9 public class Spring { 10 11 private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 12 13 14 //用于测试单个Spring是否成功 15 @Test 16 public void testBean() throws Exception { 17 18 //Bean的名字 首字母要小写 19 StrutsAction StrutsAction = (StrutsAction) ac.getBean("strutsAction"); 20 System.out.println(StrutsAction); 21 } 22 23 } 24
单元测试testBean()方法,能正常输出信息 edu.hainu.knowledge.test.StrutsAction@a383aab 表明添加Spring 成功
7.整合spring与struts2
在web.xml中配置Spring的监听器 (配置一下applicationContext.xml 所在位置)
1 xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 5 6 7 8 <listener> 9 <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> 10 listener> 11 <context-param> 12 <param-name>contextConfigLocationparam-name> 13 <param-value>classpath:applicationContext*.xmlparam-value> 14 context-param> 15 16 17 <filter> 18 <filter-name>struts2filter-name> 19 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class> 20 filter> 21 <filter-mapping> 22 <filter-name>struts2filter-name> 23 <url-pattern>/*url-pattern> 24 filter-mapping> 25 26 27 28 <welcome-file-list> 29 <welcome-file>index.jspwelcome-file> 30 welcome-file-list> 31 web-app>
添加jar包 struts2-spring-plugin-2.3.20.jar(这就是文章开头struts2 排除的jar包,不然会报错)
修改struts.xml ( )
1 xml version="1.0" encoding="UTF-8" ?> 2 DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <constant name="struts.devMode" value="true" /> 9 10 <constant name="struts.action.extension" value="action" /> 11 12 <constant name="struts.ui.theme" value="simple" /> 13 14 15 <package name="default" namespace="/" extends="struts-default"> 16 17 18 19 <action name="test" class="strutsAction"> 20 <result name="success">/index.jspresult> 21 action> 22 23 24 <action name="log_*" class="logAction" method="{1}"> 25 <result name="list">/WEB-INF/jsp/logAction/list.jspresult> 26 <result name="saveUI">/WEB-INF/jsp/logAction/saveUI.jspresult> 27 <result name="toList" type="redirectAction">log_listresult> 28 action> 29 30 31 <action name="home_*" class="homeAction" method="{1}"> 32 <result name="{1}">/WEB-INF/jsp/homeAction/{1}.jspresult> 33 action> 34 35 package> 36 37 38 39 struts>
重新启动,访问 http://localhost:8080/edu.hainu.knowledge/test.action
成功跳转表明 整合spring与struts2成功(主要是修改struts.xml 证明)
8.添加hibernate 并且整合 hibernate 与 spring (我的习惯是)
添加jar包 sqljdbc41.jar 用于sqlserver ojdbc6.jar 用于Oracle
applicationContext.xml
管理SessionFactory实例(只需要一个)
声明式事务管理
1 xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 11 12 13 <context:component-scan base-package="edu.hainu.knowledge">context:component-scan> 14 15 16 <context:property-placeholder location="classpath:jdbc.properties"/> 17 18 19 20 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 21 22 <property name="configLocation" value="classpath:hibernate.cfg.xml">property> 23 24 <property name="dataSource"> 25 <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"> 26 27 <property name="jdbcUrl" value="${jdbcUrl}">property> 28 <property name="driverClass" value="${driverClass}">property> 29 <property name="user" value="${user}">property> 30 <property name="password" value="${password}">property> 31 32 33 <property name="initialPoolSize" value="3">property> 34 35 <property name="minPoolSize" value="3">property> 36 37 <property name="maxPoolSize" value="5">property> 38 39 <property name="acquireIncrement" value="3">property> 40 41 <property name="maxStatements" value="8">property> 42 43 <property name="maxStatementsPerConnection" value="5">property> 44 45 <property name="maxIdleTime" value="1800">property> 46 bean> 47 property> 48 bean> 49 50 51 <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 52 <property name="sessionFactory" ref="sessionFactory">property> 53 bean> 54 <tx:annotation-driven transaction-manager="txManager"/> 55 56 57 beans>
jdbc.properties
//通过SERVICE_NAME连接
1 jdbcUrl =jdbc:oracle:thin:@//ip:1521/SERVICE_NAME 2 driverClass = oracle.jdbc.driver.OracleDriver 3 user = user 4 password = password
Hibernate.java
1 package edu.hainu.knowledge.test; 2 3 import org.hibernate.SessionFactory; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 9 10 public class Hibernate { 11 12 private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 13 14 // 测试SessionFactory 15 //用于测试Spring 与 hibernate 16 @Test 17 public void testSessionFactory() throws Exception { 18 SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory"); 19 System.out.println(sessionFactory); 20 } 21 22 }
单元测试 testSessionFactory()
控制台有输出org.hibernate.internal.SessionFactoryImpl@5cc5e9d2
整合 hibernate 与 spring 成功
9.总结
struts
1.导入相关jar包
2.准备 web.xml struts2.xml
3.测试 action 成功跳转代表成功。
spring
1.导入相关jar包
2.准备 applicationContext.xml
3.测试 能否从spring管理中读取出bean
struts 整 合spring
1.struts2-spring-plugin-2.3.20.jar
2.web.xml 配置spring监听器
3.测试 struts.xml 当Struts2与Spring整合后,class属性可以写bean的名称(类名首字母小写)
hibernate 和 hibernate整合spring
1.导入相关jar包 (以及数据库连接jar包)
2.applicationContext,xml 配置SessionFactory 配置事务管理 修改 jdbc.properties
3.测试 能否从spring管理中读取出sessionFactory