一、导包
1、struts2
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.8.jar
xwork-2.0.3.jar
commons-fileupload-1.2.1.jar
struts2-spring-plugin-2.1.8.jar //整合Struts2与Spring时用
2、Hibernate
slf4j-log4j12.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jta-1.1.jarlog4j.jar
slf4j-api-1.5.8.jar //slf4j-log4j12.jar的实现
hibernate-jpa-2.0-api-1.0.0.Final.jar //Hibernata3.5后 融合annotation
3、Spring
全导了- -
4、辅助的Jar包
mysql-connector-java-5.1.7-bin.jar//Mysql数据库
commons-dbcp-1.2.2.jar
commons-pool.jar
二、单独配置
1、struts2
1)配置web.xml文件。WEB-INF文件夹里面有个web.xml,新加“filter”和“filter-mapping” 节点。web.xml配置如下:
<
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 >
< 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 >
2)添加struts配置文件。 在src源文件下,新建struts.xml(其实会变成WEB-INF/classes目录),模版如下:
<!
DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< package name ="com" extends ="struts-default" >
< action name ="HelloWorld" class ="com.HelloWorld" >
< result > /HelloWorld.jsp </ result >
</ action >
<! —添加的一个action格式 -- >
</ package >
</ struts >
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< package name ="com" extends ="struts-default" >
< action name ="HelloWorld" class ="com.HelloWorld" >
< result > /HelloWorld.jsp </ result >
</ action >
<! —添加的一个action格式 -- >
</ package >
</ struts >
3)启动tomcat,如果启动无异常,则说明配置成功。
4)配置Action
以用户登录为例:
1.首先新建/WEB-INF/user_login.jsp,代码如下:
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
GBK
"
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< title > Login_page </ title >
</ head >
< body >
Hello Struts2 ~~
</ body >
</ html >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< title > Login_page </ title >
</ head >
< body >
Hello Struts2 ~~
</ body >
</ html >
2)在我们已经建好的struts.xml中来配置登录的action。这里定义登录action的名字为login,配置代码如下:
<
package
name
="UserAction"
extends
="struts-default"
>
< action name ="login" class ="com.example.action.UserAction" > //执行Action默认方法
< result > /WEB-INF/User_login.jsp </ result >
</ action >
</ package >
< action name ="login" class ="com.example.action.UserAction" > //执行Action默认方法
< result > /WEB-INF/User_login.jsp </ result >
</ action >
</ package >
3)下面就来编写具体的action类了。代码如下:
package
com.cscw.caishiquan.blog_ssh.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
@Override
public String execute() throws Exception {
return SUCCESS;
}
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
@Override
public String execute() throws Exception {
return SUCCESS;
}
开始测试吧<
2、配置Hibernate
1)创建Hibernate配置文件。在WEB-INF"calsses目录下(工程的src包下)新建hibernate.cfg.xml(整合Spring后可删除)。
<?
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 >
<!-- Database connection settings -->
< property name ="connection.driver_class" > com.mysql.jdbc.Driver </ property >
< property name ="connection.url" > jdbc:mysql://localhost/blog_ssh </ property >
< property name ="connection.username" > root </ property >
< property name ="connection.password" > cscw </ property >
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
< property name ="dialect" > org.hibernate.dialect.MySQLDialect </ property >
<!-- Echo all executed SQL to stdout -->
< property name ="show_sql" > true </ property >
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property> -->
< mapping class ="com.cscw.caishiquan.blog_ssh.pojo.User" /> <!-- 使用的是annotation映射 -->
</ session-factory >
</ hibernate-configuration >
<! DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
< hibernate-configuration >
< session-factory >
<!-- Database connection settings -->
< property name ="connection.driver_class" > com.mysql.jdbc.Driver </ property >
< property name ="connection.url" > jdbc:mysql://localhost/blog_ssh </ property >
< property name ="connection.username" > root </ property >
< property name ="connection.password" > cscw </ property >
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
< property name ="dialect" > org.hibernate.dialect.MySQLDialect </ property >
<!-- Echo all executed SQL to stdout -->
< property name ="show_sql" > true </ property >
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property> -->
< mapping class ="com.cscw.caishiquan.blog_ssh.pojo.User" /> <!-- 使用的是annotation映射 -->
</ session-factory >
</ hibernate-configuration >
到后面整合的时候这个xml都没有作用了,此时只是为测测实体类与数据库的映射。其实可以不做的,当然annotation要@Entity实体类。
3、spring
1)首先修改web.xml文件,增加如下代码:
<
context-param
>
< param-name > contextConfigLocation </ param-name >
< param-value >
classpath:/applicationContext.xml
</ param-value >
</ context-param >
< param-name > contextConfigLocation </ param-name >
< param-value >
classpath:/applicationContext.xml
</ param-value >
</ context-param >
在这里,我们指定了spring配置文件的路径,即WEB-INF/classes/目录下的所有以applicationContext开头命名的xml文件。
2)根目录建立applicationContext.xml,内容可在doc找
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"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" > <!-- 这里会随着整合不断增加 -->
< bean id ="user" class ="com.cscw.spring.test.User" > //类路径
<!-- collaborators and configuration for this bean go here -->
</ bean >
<!-- more bean definitions go here -->
</ beans >
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" > <!-- 这里会随着整合不断增加 -->
< bean id ="user" class ="com.cscw.spring.test.User" > //类路径
<!-- collaborators and configuration for this bean go here -->
</ bean >
<!-- more bean definitions go here -->
</ beans >
3),建立vo ,setter getter
User.java
package
com.cscw.spring.test;
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this .password = password;
}
}
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this .password = password;
}
}
4)测试代码Test.java
Test.java
package com.cscw.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext( " applicationContext.xml " );
User user = (User)ctx.getBean( " user " );
user.setName( " yse " );
user.setPassword( " password " );
System.out.println( "" + user.getName() + user.getPassword());
}
}
package com.cscw.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext( " applicationContext.xml " );
User user = (User)ctx.getBean( " user " );
user.setName( " yse " );
user.setPassword( " password " );
System.out.println( "" + user.getName() + user.getPassword());
}
}
三、开始整合
2、struts2与Spring整合,其实就是把Struts的Action类交给Spring来管理
1) 配置web.xml文件。在web.xml中加入以下代码:
<
listener
>
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
2)在applicationContext.xml中增加如下代码:
<
bean
id
="UserAction"
class
="com.example.action.Useraction"
scope
="prototype"
>
</ bean >
</ bean >
3)接下来修改struts.xml文件,把原来login.action的配置做如下修改:
1、hibernate与spring整合,Spring整合Hibernate主要是对hibernate的Session进行管理,包含Session的创建、提交、关闭的整个生命周期。
<
action
name
="login"
class
="UserAction"
>
//原来的是class="com.example.action.Useraction"
< result > /WEB-INF/web/User_login.jsp </ result >
</ action >
//原来的是class="com.example.action.Useraction"
< result > /WEB-INF/web/User_login.jsp </ result >
</ action >
Spring对事务的管理应用了AOP的技术(暂未包括,待补充)
1)修改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"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
< context:annotation-config /> <!-- annotation -->
< context:component-scan base-package ="com.cscw.caishiquan.blog_ssh" />
<!-- 自动扫描@component,将对象放入容器,准备被@resource注入 -->
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method ="close" >
< property name ="driverClassName" value ="com.mysql.jdbc.Driver" />
< property name ="url" value ="jdbc:mysql://localhost:3306/blog_ssh" />
< property name ="username" value ="root" />
< property name ="password" value ="cscw" />
</ bean >
<!-- 数据库连接 -->
< bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
< property name ="dataSource" ref ="dataSource" />
< property name ="annotatedClasses" > <!-- 也可以pagetoscan -->
< list >
< value > com.cscw.caishiquan.blog_ssh.pojo.User </ value >
</ list >
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
</ prop >
< prop key ="hibernate.show_sql" > true </ prop >
</ props >
</ property >
</ bean >
< bean id ="hibernateTemplate" class ="org.springframework.orm.hibernate3.HibernateTemplate" >
< property name ="sessionFactory" ref ="sessionFactory" ></ property >
</ bean >
< bean id ="UserAction" class ="com.cscw.caishiquan.blog_ssh.action.UserAction" scope ="prototype" >
</ bean >
</ beans >
< 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"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
< context:annotation-config /> <!-- annotation -->
< context:component-scan base-package ="com.cscw.caishiquan.blog_ssh" />
<!-- 自动扫描@component,将对象放入容器,准备被@resource注入 -->
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method ="close" >
< property name ="driverClassName" value ="com.mysql.jdbc.Driver" />
< property name ="url" value ="jdbc:mysql://localhost:3306/blog_ssh" />
< property name ="username" value ="root" />
< property name ="password" value ="cscw" />
</ bean >
<!-- 数据库连接 -->
< bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
< property name ="dataSource" ref ="dataSource" />
< property name ="annotatedClasses" > <!-- 也可以pagetoscan -->
< list >
< value > com.cscw.caishiquan.blog_ssh.pojo.User </ value >
</ list >
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
</ prop >
< prop key ="hibernate.show_sql" > true </ prop >
</ props >
</ property >
</ bean >
< bean id ="hibernateTemplate" class ="org.springframework.orm.hibernate3.HibernateTemplate" >
< property name ="sessionFactory" ref ="sessionFactory" ></ property >
</ bean >
< bean id ="UserAction" class ="com.cscw.caishiquan.blog_ssh.action.UserAction" scope ="prototype" >
</ bean >
</ beans >
之后@component组件标识对象 @resource 在setter方法上注入。
直接就 hibernatetemplate.save(user);
备注:自己总结了整个整合的流程,一些细节可能描述的不清楚,
Hibernate方面用annotation做了数据库与实体类的映射,用xml也可。但相应的applicationContext里的sessionFactory配置得修改。
<
bean
id
="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
< property name ="configLocation" >
< value > classpath:spring/hibernate.cfg.xml </ value >
</ property >
</ bean >
< property name ="configLocation" >
< value > classpath:spring/hibernate.cfg.xml </ value >
</ property >
</ bean >
同时声明式事务管理那块由于还未接触AOP,所以等学了再补。
spring的注入也采用annotation的方式@resource自动匹配。用到了@component,减少了struts.xml里的配置,否则必须在struts.xml里配置bean。
如此,整个SSH的整合配置还是跑的起来的。不足之处,要指点出来哦~