本文应用SSH框架版本
Struts Version-struts-2.3.12-all.zip
Spring Version-spring-framework-3.0.1.RELEASE-A.zip
Hibernate Version-hibernate-3.2.5.ga.zip
下载地址
http://struts.apache.org/download
http://www.springsource.com/download/community
http://sourceforge.net/projects/hibernate/files/hibernate3/
一、加入struts相关配置
new一个web project 并且导入struts相关jar包
ognl-3.0.6.jar
struts2-core-2.3.12.jar
xwork-core-2.3.12.jar
commons-logging-api-1.1.jar
commons-lang3-3.1.jar
commons-fileupload-1.2.2.jar
freemarker-2.3.19.jar
commons-logging-1.1.1.jar
commons-io-2.0.1.jar
javassist-3.11.0.GA.jar (本jar包在struts2-blank-2.2.1.war示例工程中的web-inf/lib下可找到)
注: Jar包如果找不到的话可以去struts2-blank-2.2.1.war示例工程中的web-inf/lib下将里面的jarcopy 过去即可
src包下建立一个class继承ActionSupport类。并且写好一个action方法,并且在src包下面建立struts.xml配置该action
public class UserLogin extends ActionSupport{ public String login(){ System.out.println("经过了"); return SUCCESS; } }
ClassPath下的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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" extends="struts-default"> <action name="login" class="com.struts.action.UserLogin" method="login"> <result name="success">/WEB-INF/pages/index.jsp</result> </action> </package> </struts>
更改web.xml加入struts相关配置
<!-- struts 跳转action配置 start --> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>com.*</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- struts 跳转action配置 end -->
说明:到此为止已经搭建完成了具备了一个struts2基本功能的web 项目。大家可以发布项目然后访问一下自己的action。看看有没有日志输出
二、加入spring相关配置
导入Jar包
将下载下来的jar包目录为spring-framework-3.0.1.RELEASE-A.zip包解压。找到里面的spring-framework-3.0.1.RELEASE-A\dist目录下的所有jar包copy到项目lib目录下
classpath下准备好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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="UserLogin" class="com.struts.action.UserLogin"> </bean> </beans>
在web.xml中键入spring配置
<!-- spring bean 容器配置start --> <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> <!-- spring bean 容器配置end -->
整合spring和struts
导入struts下载包的struts2-spring-plugin-2.3.12.jar包即可
说明:到此我们就可以将struts.xml中配置class写为
<action name="login" class="UserLogin"method="login">这里的class和spring的bean配置的id关联
到此为止我们就整合了两个框架。启动服务,访问一下action。发现还是输出日志。正常。
三、加入hibernate相关配置
导入Jar包
将hibernate解压包下的hibernate3.jar和lib文件夹下的文件全部copy到项目中。另外还要加入你的jdbc驱动。(这个要根据你的数据库而定了)
在classpath下面建立xml文件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"> <hibernate-configuration> <session-factory> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:orcl </property> <!-- 数据库连接设置 --> <property name="eclipse.connection.profile">oracle</property> <property name="connection.username">weibo</property> <property name="connection.password">weibo</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <!-- show_sql 生成SQL语句 --> <property name="show_sql">true</property> <property name="defaultAutoCommit">true</property> <!-- oracle特有的提交更改 --> <!-- SQL dialect 方言 --> <property name="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </property> <!-- 添加实体类的映射文件--> <mapping resource="com/struts/model/Student.hbm.xml" /> <!-- Annotation方式配置 <mapping class="entity.Login"/> --> </session-factory> </hibernate-configuration>
在classpath下建立applicationContext-hibernate.xml
注:以下代码中的dataSource没有用到。大家也可以试试LocalSessionFactoryBean的另外一个构造函数。这个我不多说大家去看API或者源码
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 配置dataSource start --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" /> <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="user" value="weibo" /> <property name="password" value="weibo" /> <property name="minPoolSize" value="2" /> <property name="maxPoolSize" value="50" /> <property name="initialPoolSize" value="10" /> <property name="maxIdleTime" value="60" /> <property name="acquireIncrement" value="2" /> </bean> <!-- 配置dataSource end --> <!-- 配置sessionFactory start--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:/hibernate.cfg.xml</value> </property> </bean> <!-- 配置sessionFactory end--> <!-- 配置hibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
加入hibernateTemplate的注入
<bean id="UserLogin" class="com.struts.action.UserLogin"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean>
修改UserLogin类
public class UserLogin extends ActionSupport{ private HibernateTemplate hibernateTemplate; public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } public String login(){ Student stu = new Student(); stu.setName("zhanglie"); hibernateTemplate.save(stu); System.out.println("经过了"); return SUCCESS; }
映射文件和POJO
package com.struts.model; public class Student { private String id; private String name; private Integer age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.struts.model.Student" table="STUDENT"> <id name="id" column="id" type="java.lang.String"> <generator class="uuid"/> </id> <property name="name" type="java.lang.String" column="name"> </property> <property name="age" type="java.lang.Integer" column="age"> </property> </class> </hibernate-mapping>
访问你配置的action以完成插入操作
源码打包下载:
http://pan.baidu.com/share/link?shareid=403237&uk=1997312776