-
Ssi整合首先需要导入相关的jar包
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/activation.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/antlr-2.7.6.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-3.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-commons-3.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-tree-3.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/c3p0-0.9.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.aopalliance-1.0.0.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.apache.commons.logging-1.1.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.apache.log4j-1.2.15.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-collections-3.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-fileupload-1.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-io-2.0.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-lang3-3.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-logging-1.1.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/freemarker-2.3.19.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate-jpa-2.0-api-1.0.1.Final.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/javassist-3.11.0.GA.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/javassist-3.12.0.GA.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jta-1.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/log4j-1.2.17.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mail.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mysql-connector-java-5.0.4-bin.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ognl-3.0.6.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/slf4j-api-1.6.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/slf4j-log4j12-1.7.2.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-aop-3.2.0.RELEASE.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-aspects-3.2.0.RELEASE.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-beans-3.2.0.RELEASE.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-context-3.2.0.RELEASE.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-core-3.2.0.RELEASE.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-expression-3.2.0.RELEASE.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-jdbc-3.2.0.RELEASE.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-orm-3.2.0.RELEASE.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-test-3.2.0.RELEASE.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-tx-3.2.0.RELEASE.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-web-3.2.0.RELEASE.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts2-core-2.3.15.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts2-json-plugin-2.3.15.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts2-spring-plugin-2.3.15.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/xwork-core-2.3.15.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ibatis-2.3.4.726.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/Oracle+11g+11.1.0.6.0+JDBC_ojdbc6.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-dbcp.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-pool.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/javax.servlet.jar"/>
2.首先配置struts2的内容:
2.1在web.xml中配置struts2 的拦截器
<filter>
<filter-name>struts2</filter-name>
<filter-class>
<!-- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter -->
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.2在src目录下创建struts的配置文件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="struts2" extends="struts-default" >
<!—注意-->
<!—此处 class="test"是指该action在spring中ID号-->
<action name="test" class="test">
<result name="success">/index.jsp</result>
</action>
</package>
<!-- 登录模块,模块方便开发-->
<include file="/struts2/registe/struts-registe.xml" />
<!—将struts对象交给Spring管理,非常重要,当然也有其他方法-->
<constant name="struts.objectfactory" value="spring" />
</struts>
3.下面是我的项目的文档结构src下的文件可以测试
3.1
创建测试文件进行测试TestStruts2Action
package com.hzl.ssi.testStruts2Action;
/**
* @author hanzl
* 作用:测试struts2的作用
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hzl.ssi.model.UserModel;
import com.hzl.test.service.TestService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class TestStruts2Action extends ActionSupport implements Action {
private String acStr="我的ssi整合中Struts2的Action";
public String getAcStr() {
return acStr;
}
public void setAcStr(String acStr) {
this.acStr = acStr;
}
public String execute() {
String id="001";
System.out.println("正在执行 execute()方法");
return SUCCESS;
}
}
3.2在浏览器里输入localhost/ssi/test.action测试能否跳转到index页面
Index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix ="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
${acStr}
</body>
</html>
3.3 Struts2配置成功运行结果:
4.接着要整合struts2和Spring
4.1在web.xml创建Spring 的监听器以及管理Spring的配置文件applicationContext.xml,我的Spring的文件是放在默认路径下的 。当然也可放在别处。
配置如下:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
4.2接着就是配置applicationContext.xml了,目的是吧Struts2的Action交给Spring去管理
这事需要在struts.xml中加上一句话
<constant name="struts.objectfactory" value="spring" />
4.3接下来就是在application.xml中的具体配置了
由于我的项目已经配置完毕,测试struts2和Spring整合时只需要删除其他的jdbc等,还有service等,多动手才会更明白。
<?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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!—配置测试Action 配置完之后将Struts.xml中的Action的 class属性替换即可,这在前面有标注-->
<bean id="test" class="com.hzl.ssi.testStruts2Action.TestStruts2Action" >
<property name="testService" ref="testService"/>
</bean>
<bean id="testService" class="com.hzl.test.service.TestServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="com.hzl.ssi.model.UserDao" >
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<!-- 方便查找类路径下 jdbc.properties 并读取到内存中-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 数据源配制 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.user}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常” -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:ibitas.xml</value>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 引入子文件 -->
<import resource="classpath:spring/**/spring*.xml" />
<import resource="classpath:spring/**/**/spring*.xml" />
<import resource="classpath:spring/**/**/**/spring*.xml" />
</beans>
到这里基本Struts2和Spring整合基本完毕了。
5.接着要把ibitas整合上去这里主要是在applicationContext中配置
5.1首先要把连接数据库的属性文件放入到src目录下jdbc.properties
文件内容:
注意:这里用的Oracle数据库
jdbc.driver = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.user = hanzl
jdbc.password =password
5.2创建ibitas核心文件 ibatis.xml 内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- 启用命名空间 -->
<!-- <setting useStatementNamespaces="true"/> -->
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="false" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />
<!—引入子文件方便模块化编程 -->
<sqlMap resource="com/hzl/ssi/model/ibitas_userinfo.xml" />
<sqlMap resource="com/hzl/ssi/regist/ibitas_register.xml" />
</sqlMapConfig>
5.3在applicationContext.xml中配置数据源等信息全部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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="test" class="com.hzl.ssi.testStruts2Action.TestStruts2Action" >
<property name="testService" ref="testService"/>
</bean>
<bean id="testService" class="com.hzl.test.service.TestServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="com.hzl.ssi.model.UserDao" >
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<!-- 方便查找类路径下 jdbc.properties 并读取到内存中-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 数据源配制 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.user}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常” -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:ibitas.xml</value>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 引入子文件 -->
<import resource="classpath:spring/**/spring*.xml" />
<import resource="classpath:spring/**/**/spring*.xml" />
<import resource="classpath:spring/**/**/**/spring*.xml" />
</beans>
5.4编写测试文件测试
5.4.1需要的文件TestStruts2Action.java
package com.hzl.ssi.testStruts2Action;
/**
* @author hanzl
* 作用:测试struts2的作用
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hzl.ssi.model.UserModel;
import com.hzl.test.service.TestService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class TestStruts2Action extends ActionSupport implements Action {
private String acStr="我的ssi整合中Struts2的Action";
//private ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
//TestService testService=(TestService) act.getBean("testService");
TestService testService;
private List<UserModel> list=new ArrayList<UserModel>();
public List<UserModel> getList() {
return list;
}
public void setList(List<UserModel> list) {
this.list = list;
}
private UserModel u=new UserModel();
public UserModel getU() {
return u;
}
public void setU(UserModel u) {
this.u = u;
}
public TestService getTestService() {
return testService;
}
public void setTestService(TestService testService) {
this.testService = testService;
}
public String getAcStr() {
return acStr;
}
public void setAcStr(String acStr) {
this.acStr = acStr;
}
public String execute() {
System.out.println("正在执行 execute()方法");
try {
list=testService.Queryforlist();
for(UserModel u:list){
//u.setSex(list.get())
System.out.println(u.getUserid()+" "+u.getUsername()+" "+u.getSex());
}
System.out.println("成功查到.................");
} catch (Exception e) {
e.printStackTrace();
}
ServletActionContext.getRequest().setAttribute("list", list);
return SUCCESS;
}
}
5.4.2需要的文件TestService.java
package com.hzl.test.service;
import java.util.List;
import com.hzl.ssi.model.UserModel;
public interface TestService {
public void service() throws Exception;
//根据ID查询
public UserModel selectByid(String id);
//查询list
public List<UserModel> Queryforlist();
}
5.4.3需要的文件TestServiceImpl.java
package com.hzl.test.service;
import java.util.List;
import com.hzl.ssi.model.UserDao;
import com.hzl.ssi.model.UserModel;
public class TestServiceImpl implements TestService {
private UserDao userDao=null;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void service() throws Exception{
UserModel personInsert = new UserModel();
personInsert.setUserid("001");
personInsert.setUsername("小韩");
personInsert.setSex("男");
userDao.insertObject(personInsert);
System.out.println("this is service function");
}
public UserModel selectByid(String id) {
return userDao.selectbyid(id);
}
public List<UserModel> Queryforlist(){
return userDao.Queryforlist();
}
}
5.4.4 需要的文件UserDao.java
package com.hzl.ssi.model;
import java.sql.SQLException;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
public class UserDao extends SqlMapClientDaoSupport {
public Object insertObject (UserModel userModel) throws SQLException {
return getSqlMapClientTemplate().insert("userModel.insertPerson", userModel);
}
public UserModel selectbyid(String id){
return (UserModel) getSqlMapClientTemplate().queryForObject("userModel.selectByid",id);
}
public List<UserModel> Queryforlist(){
return getSqlMapClientTemplate().queryForList("userModel.listUserModel");
}
}
5.4.5需要的文件ibitas_userinfo.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="userModel">
<!-- Use type aliases to avoid typing the full classname every time. -->
<typeAlias alias="userModel" type="com.hzl.ssi.model.UserModel"/>
<!-- Result maps describe the mapping between the columns returned
from a query, and the class properties. A result map isn't
necessary if the columns (or aliases) match to the properties
exactly. -->
<resultMap id="userModel" class="com.hzl.ssi.model.UserModel">
<result property="userid" column="userid" />
<result property="username" column="username" />
<result property="sex" column="sex" />
</resultMap>
<!-- Insert example, using the Person parameter class -->
<insert id="insertPerson" parameterClass="userModel">
insert into userinfo
(userid, username, sex )
values
(#userid#, #username#, #sex# )
</insert>
<!-- 根据ID查询 -->
<select id="selectByid" parameterClass="java.lang.String">
select userid,username,sex from userinfo where userid=#userid#
</select>
<select id="listUserModel" resultMap="userModel">
select * from userinfo
</select>
</sqlMap>
注意: 将该文件引入到ibatis核心文件ibatis.xml中
我这创建的数据库的表是userinfo
6.进行整合完后测试
测试结果:
成功 完整工程 下载地址
https://download.csdn.net/download/hanzl1/10545561
百度文库
https://wenku.baidu.com/view/58f5cb4f1fb91a37f111f18583d049649b660e8a