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")
publicList
List
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