经典的整合struts2+spring+ibitis实例.docx

上传人:b****6 文档编号:6291034 上传时间:2023-01-05 格式:DOCX 页数:9 大小:68.93KB
下载 相关 举报
经典的整合struts2+spring+ibitis实例.docx_第1页
第1页 / 共9页
经典的整合struts2+spring+ibitis实例.docx_第2页
第2页 / 共9页
经典的整合struts2+spring+ibitis实例.docx_第3页
第3页 / 共9页
经典的整合struts2+spring+ibitis实例.docx_第4页
第4页 / 共9页
经典的整合struts2+spring+ibitis实例.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

经典的整合struts2+spring+ibitis实例.docx

《经典的整合struts2+spring+ibitis实例.docx》由会员分享,可在线阅读,更多相关《经典的整合struts2+spring+ibitis实例.docx(9页珍藏版)》请在冰豆网上搜索。

经典的整合struts2+spring+ibitis实例.docx

经典的整合struts2+spring+ibitis实例

注意:

spring和struts2整合的时候一定在struts2的jar包中把struts2-spring-plugin.jar拷贝上,要不关联不上啊。

看看我的jar包:

struts2的jar,spring的jar,ibatis的jar,oracle驱动,log4j的jar。

目录结构:

 applicationContext.xml可以放在src目录下,也可以放在WEB-INF下···都成,但是在web.xml中注意配置。

名字不能便成别的,认不到。

web.xml:

xmlversion="1.0"encoding="UTF-8"?

>

 xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

 xsi:

schemaLocation="

 

 

  index.jsp

 

 

--加载spring文件-->

 

  contextConfigLocation

  /WEB-INF/applicationContext.xml

 

 

  

   org.springframework.web.context.ContextLoaderListener

  

 

 

--加载struts文件-->

 

  struts2

  

   org.apache.struts2.dispatcher.FilterDispatcher

  

 

 

  struts2

  /*

 

现在从数据持久化层开始做:

PO层(以Dep为例)

Dep.java

packageorg.po;

publicclassDep{

 

 privateIntegerdid;

 privateStringdname;

 publicIntegergetDid(){

  returndid;

 }

 publicvoidsetDid(Integerdid){

  this.did=did;

 }

 publicStringgetDname(){

  returndname;

 }

 publicvoidsetDname(Stringdname){

  this.dname=dname;

 }

 

}

dep.xml:

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEsqlMap     

   PUBLIC"-//ibatis.apache.org//DTDSQLMap2.0//EN"     

   "http:

//ibatis.apache.org/dtd/sql-map-2.dtd">

 

 

  

  

 

 

  

[CDATA[SELECT*FROMdep]]>

 

 

  

[CDATA[SELECT*FROMdepWHEREdid=#did#]]>

 

 

  

[CDATA[INSERTINTOdepVALUES(#did#,#dname#)]]>

 

 

  

[CDATA[UPDATEdepSETdname=#dname#WHEREdid=#did#]]>

 

 

  

[CDATA[DELETEFROMdepWHEREdid=#did#]]>

 

这个是ibatis的起始位置,所有映射文件都可以从这里找到。

config.xml:

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEsqlMapConfig     

   PUBLIC"-//ibatis.apache.org//DTDSQLMapConfig2.0//EN"     

   "http:

//ibatis.apache.org/dtd/sql-map-config-2.dtd">

 

 

 

 

下来就写dao文件了:

(以DepDao.java为例)

packageorg.dao;

importjava.sql.SQLException;

importjava.util.List;

importcom.ibatis.sqlmap.client.SqlMapClient;

publicclassDepDaoimplementsIDao{

 privateSqlMapClientclient;

 publicbooleandelete(Integerid){

  try{

   client.delete("deleteDep",id);

   returntrue;

  }catch(SQLExceptione){

   e.printStackTrace();

  }

  returnfalse;

 }

 @SuppressWarnings("unchecked")

 publicListfindAll(){

  Listlist=null;

  try{

   list=client.queryForList("findAllDep");

  }catch(SQLExceptione){

   e.printStackTrace();

  }

  returnlist;

 }

 publicObjectfindById(Integerid){

  DepDaodep=null;

  try{

   dep=(DepDao)client.queryForObject("findByIdDep",id);

  }catch(SQLExceptione){

   e.printStackTrace();

  }

  returndep;

 }

 publicbooleansave(Objectobj){

  DepDaodep=(DepDao)obj;

  try{

   client.insert("insertDep",dep);

   returntrue;

  }catch(SQLExceptione){

   e.printStackTrace();

  }

  returnfalse;

 }

 publicbooleanupdate(Objectobj){

  DepDaodep=(DepDao)obj;

  try{

   client.update("updateDep",dep);

   returntrue;

  }catch(SQLExceptione){

   e.printStackTrace();

  }

  returnfalse;

 }

 publicSqlMapClientgetClient(){

  returnclient;

 }

 publicvoidsetClient(SqlMapClientclient){

  this.client=client;

 }

}

下来是要写appliactionContext.xml:

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

 xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

 xsi:

schemaLocation="http:

//www.springframework.org/schema/beanshttp:

//www.springframework.org/schema/beans/spring-beans-2.0.xsd">

 

--spring代理的数据源,与数据库连接从这里开始-->

 

  

   oracle.jdbc.driver.OracleDriver

  

  

   jdbc:

oracle:

thin:

@192.1.1.40:

1521:

orcl

  

  

   system

  

  

   orcl

  

 

 

--spring代理ibatis的工厂,创建sqlMapClient,在ibatis中很重要的对象,它是调用数据库操作方法的入口-->

 

  

   

  

  

   classpath:

config.xml

  

 

 

--Dao类-->

 

  

   

  

 

 

  

   

  

 

 

  

   

  

 

 

  

   

  

 

 

 

--Face类-->

 

  

   

  

  

   

  

 

 

用法和以前的差不多··都是注入到spring容器中,不同的是struts的action不容在注入了。

下来是struts的action:

packageorg.action;

importjava.util.List;

importorg.face.Face;

importorg.po.Dep;

importcom.opensymphony.xwork2.ActionSupport;

publicclassDepActionextendsActionSupport{

 privateListdepList;

 privateFaceface;

 publicStringexecute(){

  returnnull;

 }

 publicStringfindAll(){

  depList=face.getDepDao().findAll();

  

  return"error";

 }

 publicFacegetFace(){

  returnface;

 }

 publicvoidsetFace(Faceface){

  this.face=face;

 }

 publicListgetDepList(){

  returndepList;

 }

 publicvoidsetDepList(ListdepList){

  this.depList=depList;

 }

}

struts2.0没有了actionForm,将所有需要的值都放在了action中,只要赋值后,界面就可以直接用,方便。

但是看着挺乱的。

struts.properties:

struts.locale=zh_CN

struts.i18n.encoding=UTF-8

struts.objectFactory=spring

struts.xml

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEstrutsPUBLIC

       "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"

       "http:

//struts.apache.org/dtds/struts-2.0.dtd">

   

   

   

 

 

 

  

   findAll

   /error.jsp

  

  

   findAll

   /error.jsp

  

  

   /edit.jsp

  

  

   findAll

   /error.jsp

  

  

   /list.jsp

  

 

展开阅读全文
相关搜索


copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1