Java ssh2简单整合流程
2013-04-26 16:58
281 查看
一、关于本次整合的部署版本
spring 3.1.4
hibernate 4.2.0 final(原本使用的4.3beta,后来有个bug未能攻破,换了4.2的core包)
struts 2.3.14相关链接:http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.1.4.RELEASE-with-docs.ziphttp://jaist.dl.sourceforge.net/project/hibernate/hibernate4/4.2.0.Final/hibernate-release-4.2.0.Final.ziphttp://labs.mop.com/apache-mirror//struts/library/struts-2.3.14-lib.zip
二、部署过程详解1、首先需要spring文档一份2、导入spring的jar包,在类路径下添加一个applicationContext.xml对ioc进行测试
TestService.java代码
spring测试代码:
note:
可能会提示缺少commons-logging.jar包,你可以在下载的struts的lib文件下找到,或者别的什么地方
————————————————————spring部署结束————————————————————
3、将下载的hibernate的jar包导入工程中
i)在applicationContext.xml中加入
dbcp数据源配置(由spring进行自动管理)
配置sessionFactory
note:
这边仅仅hibernate的jar包是不够的,因为涉及到数据库连接池,所以单独使用dbcp需要去apache
下载三个包,分别是:commons-dbpc.jar、commons-collections.jar、commons-pool.jar
(详见http://commons.apache.org/)
当然不能忘记数据库驱动,我这边用的是oracle,当然就要导入ojdbc了。
ii)配置jdbc.properties
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.username=system
jdbc.password=1234
java测试代码:
5、这里是Student类的domain和xml
Student.java
student.hbm.xml
————————————————————hibernate部署结束————————————————————
6、struts2的部署比较简单,参考spring文档的公共部署部分配置web.xml
7、加入struts2核心包和其他几个必须的包
commons-fileupload.jar
freemarker.jar
ognl-2.7.3.jar
struts2-core.jar
xwork-core.jar
note:
这里有个和spring整合时候用到的特殊的包:
struts2-spring-plugin-2.3.14.jar
8、配置struts2的过滤器到web.xml
note:
这里一般会配置一个防止内存溢出的监听器(spring管理的bean本身不会溢出,但是第三方可能会在请求后无法正常回收):
9、在类路径下配置struts2的配置文件struts.xml
同时在applicationContext.xml下加入:
note:
这里struts.xml的action中的class指向的是applicationContext.xml中的bean的id,这样就能找到类了。
至此,spring的ioc和struts.xml的ioc就关联起来了,大功告成!
最后的一些note:
可能会缺少一些相关的包,这里列出一些我遇到的,假如还不足,自求多福吧(哈哈哈,开玩笑的,互相交流~)
可以在struts2的lib目录下找到的包:
commons-lang3.jar
commons-io.jar
java测试代码:
index.jsp代码:
spring 3.1.4
hibernate 4.2.0 final(原本使用的4.3beta,后来有个bug未能攻破,换了4.2的core包)
struts 2.3.14相关链接:http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.1.4.RELEASE-with-docs.ziphttp://jaist.dl.sourceforge.net/project/hibernate/hibernate4/4.2.0.Final/hibernate-release-4.2.0.Final.ziphttp://labs.mop.com/apache-mirror//struts/library/struts-2.3.14-lib.zip
二、部署过程详解1、首先需要spring文档一份2、导入spring的jar包,在类路径下添加一个applicationContext.xml对ioc进行测试
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd" default-autowire="byName"> <bean id="testService" class="cn.tv189.service.TestService"></bean> </beans>
TestService.java代码
package cn.tv189.service; public class TestService { public String doService(){ return "got u"; } }
spring测试代码:
package cn.tv189.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.tv189.service.TestService; public class TestSpring { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); TestService ts = (TestService)ac.getBean("testService"); String re = ts.doService(); System.out.println(re); } }
note:
可能会提示缺少commons-logging.jar包,你可以在下载的struts的lib文件下找到,或者别的什么地方
————————————————————spring部署结束————————————————————
3、将下载的hibernate的jar包导入工程中
i)在applicationContext.xml中加入
dbcp数据源配置(由spring进行自动管理)
<bean id="dataSource2" 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}"/> <property name="initialSize" value="3"/> <property name="maxActive" value="100"/> <property name="maxIdle" value="2"/> <property name="minIdle" value="1"/> </bean> <context:property-placeholder location="classpath:jdbc.properties"/>
配置sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource2"/> <property name="mappingResources"> <list> <value>cn/tv189/domain/student.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.OracleDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true </value> </property> </bean>
note:
这边仅仅hibernate的jar包是不够的,因为涉及到数据库连接池,所以单独使用dbcp需要去apache
下载三个包,分别是:commons-dbpc.jar、commons-collections.jar、commons-pool.jar
(详见http://commons.apache.org/)
当然不能忘记数据库驱动,我这边用的是oracle,当然就要导入ojdbc了。
ii)配置jdbc.properties
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.username=system
jdbc.password=1234
java测试代码:
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); SessionFactory sf = (SessionFactory) ac.getBean("sessionFactory"); Session s = sf.openSession(); Student stu = new Student("汤姆andJerry"); Transaction ts = s.beginTransaction(); s.save(stu); ts.commit();
5、这里是Student类的domain和xml
Student.java
package cn.tv189.domain; import java.io.Serializable; public class Student implements Serializable{ private static final long serialVersionUID = -3807511470861510540L; private int id; private String name; public Student(){ } public Student(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.tv189.domain"> <class name="Student" table="student"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id> <property name="name" type="java.lang.String"> <column name="email" length="160"/> </property> </class> </hibernate-mapping>
————————————————————hibernate部署结束————————————————————
6、struts2的部署比较简单,参考spring文档的公共部署部分配置web.xml
<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>
7、加入struts2核心包和其他几个必须的包
commons-fileupload.jar
freemarker.jar
ognl-2.7.3.jar
struts2-core.jar
xwork-core.jar
note:
这里有个和spring整合时候用到的特殊的包:
struts2-spring-plugin-2.3.14.jar
8、配置struts2的过滤器到web.xml
<filter> <filter-name>strutsFilter</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>strutsFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
note:
这里一般会配置一个防止内存溢出的监听器(spring管理的bean本身不会溢出,但是第三方可能会在请求后无法正常回收):
<listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener>
9、在类路径下配置struts2的配置文件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> <package name="stu-package" extends="struts-default"> <action name="list" class="studentAction"> <result name="success">/index.jsp</result> </action> </package> </struts>
同时在applicationContext.xml下加入:
<bean id="studentAction" class="cn.tv189.action.StudentAction"> <property name="testService" ref="testService"/> </bean>
note:
这里struts.xml的action中的class指向的是applicationContext.xml中的bean的id,这样就能找到类了。
至此,spring的ioc和struts.xml的ioc就关联起来了,大功告成!
最后的一些note:
可能会缺少一些相关的包,这里列出一些我遇到的,假如还不足,自求多福吧(哈哈哈,开玩笑的,互相交流~)
可以在struts2的lib目录下找到的包:
commons-lang3.jar
commons-io.jar
java测试代码:
package cn.tv189.action; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import cn.tv189.domain.Student; import cn.tv189.service.TestService; public class StudentAction { private TestService testService; private Student s; public String execute(){ System.out.println("调用成功"); // ServletActionContext.getRequest() testService.doService(); s = new Student(); s.setId(1); s.setName("Tom&杰瑞"); return "success"; } public Student getS() { return s; } public void setS(Student s) { this.s = s; } public TestService getTestService() { return testService; } public void setTestService(TestService testService) { this.testService = testService; } }
index.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <!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>Insert title here</title> </head> <body> <table> <tr> <td>${s.id}</td> <td>${s.name}</td> </tr> </table> </body> </html>
相关文章推荐
- java activiti5 在线web流程设计器整合视频教程 SSM和独立部署
- JavaWeb项目:Shiro实现简单的权限控制(整合SSM)
- JavaWeb框架-SSH-整合流程!
- java activiti5 在线web流程设计器整合视频教程 SSM和独立部署
- java activiti5 在线web流程设计器整合视频教程 SSM和独立部署
- MyEclipse6.5整合flex实现与java简单通信过程中遇到的问题和注意事项
- java activiti5 在线web流程设计器整合视频教程 SSM和独立部署
- java spring mvc+mysql 整合写的一个简单的登录
- Java爬虫初体验:简单抓取IT之家热评(整合Spring Boot+Elasticsearch+Redis+Mybatis)
- C_C++_Java_简单题_for的流程
- java spring mvc+mysql 整合写的一个简单的登录
- 支付宝java后台接入简单流程
- 《Thinking in Java》第五篇笔记 就Java中的操作流程简单说几句
- java activiti5 在线web流程设计器整合视频教程 SSM和独立部署
- Activiti-master java activiti5 在线web流程设计器整合视频教程
- java web ssh三大框架整合简单入门
- unity调用Android通过编译 java jar包出现问题解决和简单支付流程
- 天易19----java-xfireWebservice和spring整合的简单实现
- Java+Flex整合应用简单示例
- JavaWeb框架-SSM-普通Web项目整合流程!