亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

基于J2EE的SSH 整合應用及操作示例二(CRUD操作

系統 1972 0
基于J2EE的SSH
整合應用及操作示例二(CRUD操作及配置)
2009-10-08 09:25:22
原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。 http://gaochaojs.blog.51cto.com/812546/210131
CRUD
是指在做計算處理時的增加 (Create) 、查詢 (Retrieve) (重新得到數據)、更新 (Update) 和刪除 (Delete) 幾個單詞的首字母簡寫。主要被用在描述軟件系統中數據庫或者持久層的基本操作功能。
In computing, CRUD is an acronym for create, retrieve, update, and delete. It is used to refer to the basic functions of a database or persistence layer in a software system.
C reate new records
R etrieve existing records
U pdate existing records
D elete existing records.
在響應新增部門請求中,通過導入 Struts 的配置文件 (struts-config.xml) 來完成業務流程的部署。它把 depAdd.jsp depAdd.do 連接起來。 depAdd.jsp 上產生客戶請求, depAdd.do 則對請求產生響應、處理 jsp 頁面上的數據。當點擊 depAdd.jsp 上的保存按鈕
( <div style= "float: right; padding: 10px 25px 0 0;" > <div class = "right_button" > <a href= "#" onclick= "check();return false;" ><bean:message key= "go.add" /> </a> </div>
</div>)
先檢查頁面數據的正確性,校驗通過后將輸入數據用 Set 方法存入到 userinfoForm 這個 FormBean 中。
function check() { var userName = document.userinfoForm.userName.value; var password = document.userinfoForm.password.value; var length = document.userinfoForm.password.value.length; var repassword = document.userinfoForm.repassword.value; var tel = document.userinfoForm.tel.value; var department = document.userinfoForm.department.value; if (userName=="") { alert('部門管理員姓名不能為空!') return false ; }

if (password=="") { alert('密碼不能為空!') return false ; } if (length<6||length>10) { alert('密碼必須大于6個字符小于10個字符!') return false ; } if (repassword=="") { alert('重復密碼不能為空!') return false ; } if (password!=repassword) { alert('密碼輸入不一致!') return false ; } if (tel!="") { No = "0123456789()+-" for (i=0; i<tel.length; i++) { var Checkstr = tel.charAt(i); if (No.indexOf(Checkstr)== -1) { alert( "聯系電話格式不正確!" ); return false ; } } } if (department=="") { alert('部門管理員所屬部門不能為空!') return false ; }

else { document.userinfoForm.submit(); }

}
然后根據 struts-config.xml 調用 depAdd.do ( 這將在 Spring 配置文件中指定相應的 Action) 進行業務處理。在 depAdd.do 中頁面數據將從 userinfoForm 中讀取。 depAdd.do 執行成功后將顯示 /ok.jsp 頁面。 depAdd.do 對應的 Action (DepAddAction) Spring 的配置文件 (applicationContext.xml) 中指定。要把 depAdd.do DepAddAction 對應起來 , 首先要在 struts-config.xml 中配置 Delegating RequestProcessor 。其次,需要在 application Context.xml 中定義名字為“ /depAdd ”的受管 JavaBean 。每次對 DepAddAction 請求時, Delegating Request Processor 將充當代理。同時, DepAddAction 使用到受管 Java Beansm Service 。要使用 UserinfoService ,需要在 DepAddAction 中生成 UserinfoService get() set() 方法,并且 application Context.xml 中對“ /depAdd ”進行 Dep Add Action 的依賴注入。因為 DepAddAction 并沒有直接去操作數據訪問 Userinfo DAO 。而是通過調用業務邏輯層 UserinfoService 中的方法來實現業務邏輯的。 DepAddAction 中部分代碼如下:
String userName = userinfoForm.getUserName(); String pwd = userinfoForm.getPassword(); String rePwd = userinfoForm.getRepassword(); String tel = userinfoForm.getTel(); String dep = userinfoForm.getDepartment();

// 生成userinfo對象 Userinfo userinfo = new Userinfo();

// 將從表單中獲得的值賦給該對象 userinfo.setUserName(userName); userinfo.setPassword(pwd); userinfo.setTel(tel); userinfo.setDepartment(dep); userinfo.setUserType( "dep" ); // 所有新增用戶類型一律為dep // 數據入庫
userinfoService.save(userinfo);
如果 depAdd.do 要對應另一個 Action ,則只要修改 applicationContext.xml 即可,這將有利于系統的更新。同樣,如果另一個 .do 要對應 DepAddAction ,也只要在 applicationContext.xml 中配置即可,這將有利于代碼的重用。在本系統中, Hibernate Spring 共用一個配置文件 applicationContext.xml Hibernate applicationContext.xml 中讀取和數據庫有關的信息。數據庫信息包括數據庫連接、與數據庫結構相對應的映射文件。在新增部門請求中,涉及到的數據庫表為 userinfo 表,它所對應的映射文件為 Userinfo.hbm.xml 。為了訪問數據庫表 userinfo ,只有 Userinfo.hbm.xml 映射文件是不夠的,還需要數據訪問類 UserinfoDAO 、數據類 AbstractUserinfo,Userinfo 。數據類 Userinfo 的實現較為簡單,它只是 Java 對象與數據庫表之間的對應,主要用于在各應用層間傳遞數據,在它的基礎上要實現的就是數據訪問類 UserinfoDAO 。系統在生成 UserinfoDAO 的同時,也將 UserinfoDAO 作為 JavaBean 配置到 applicationContext.xml 中。 UserinfoDAO 中是對 userinfo 表進行保存、查詢、刪除或修改等基本數據操作,在 applicationContext.xml 中需要 userinfoService 進行 UserinfoDAO 及其代理的依賴注入。這樣做,使得當 UserinfoDAO 變化時,只需修改 applicationContext.xml userinfoService 實現新的注入,指向新的實現就可以了,由此解除了數據訪問層和業務層的緊密耦合。數據訪問類 UserinfoDAO 繼承于輔助類 Hibernate-DaoSupport ,借助于 getHibernateTemplate() 獲得對 Hibernate 資源的操作,極大的方便了 Hibernate 框架的使用。在 UserinfoDAO 中定義了對數據庫表 userinfo 的操作函數。如下面代碼,即是 UserinfoService 中調用的 saveData 方法。
public class UserinfoDAO extends HibernateDaoSupport implements IUserinfoDAO { private static final Log log = LogFactory.getLog(UserinfoDAO. class );

protected void initDao() { // do nothing }

public void save(Userinfo transientInstance) { log.debug( "saving Userinfo instance" ); try { getHibernateTemplate().save(transientInstance); log.debug( "save successful" ); } catch (RuntimeException re) { log.error( "save failed" , re); throw re; }
}
部門添加模塊中 applicationContext.xml 中的代碼如下:
< ! -- 創建事務管理類 -- > <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> < property name ="sessionFactory" > < ref local ="sessionFactory" /> </ property > </ bean >

< ! -- 創建用戶事務代理類 -- > <bean id="userinfoDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> < property name ="transactionManager" > < ref bean ="transactionManager" /> </ property > < property name ="target" > < ref local ="UserinfoDAO" /> </ property > < property name ="transactionAttributes" > < props > < prop key ="save*" > PROPAGATION_REQUIRED </ prop > < prop key ="modify*" > PROPAGATION_REQUIRED </ prop > < prop key ="delete*" > PROPAGATION_REQUIRED </ prop > < prop key ="get" > PROPAGATION_REQUIRED,readOnly </ prop > < prop key ="*" > PROPAGATION_REQUIRED,readOnly </ prop > </ props > </ property > </ bean >


< ! --進行UserinfoDAO及其代理的依賴注入-- > < bean id ="UserinfoService" class ="com.dpt.ec.service.impl.UserinfoService" > < property name ="userinfoDAO" > < ref bean ="userinfoDAOProxy" /> </ property > </ bean >


< ! --調度管理#新增部門管理員 -- > <bean name="/depAdd" class="com.dpt.ec.web.action.admin.DepAddAction" singleton="false"> < property name ="userinfoService" > < ref bean ="UserinfoService" /> </ property > </ bean > 部門添加模塊中struts-config.xml中的代碼如下: < form-beans >


< form-bean name ="userinfoForm" type ="com.dpt.ec.web.form.UserinfoForm" /> </ form-beans >

< ! -- 調度管理#新增加部門管理員-- > <action path="/depAdd" name="userinfoForm" type="org.springframework.web.struts.DelegatingActionProxy" scope="request" validate="false"> < forward name ="success" path ="/ok.jsp" /> < forward name ="warning" path ="error.pub" /> < forward name ="failure" path ="error.sys" /> </ action >

在本項目的開發過程中運用這樣的技術方法大大提高了開發效率、增加了程序的可讀性、減少了模塊間的耦合性,這樣使得系統更加容易更新和維護。

基于J2EE的SSH 整合應用及操作示例二(CRUD操作及配置)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产成人精品一区二区不卡 | 国产一区二区免费播放 | 高清不卡一区二区 | 久久这里只有精品国产 | 国产亚洲精品国产福利在线观看 | 欧美黑人激情性久久 | 啪啪免费网站入口链接 | 久久99精品久久久久久噜噜丰满 | 久久精品资源站 | 中文字幕在线观看亚洲日韩 | 欧美中文字幕视频 | 午夜论坛 | 日韩经典欧美精品一区 | 欧美日韩视频在线播放 | 蜜桃日本一道无卡不码高清 | 成人久久久久久 | 亚洲一区日韩 | 色综合天| 99视频全部免费精品全部四虎 | 久久久久国产一级毛片高清片 | 中文字幕视频在线免费观看 | 国产免费不卡v片在线观看 国产免费不卡视频 | 国产精品午夜在线观看 | 亚洲成人在线视频 | 欧美激情特级黄aa毛片 | 亚洲另在线日韩综合色 | 被公侵犯肉体中文字幕一区二区 | 伊人久久香蕉 | 四虎影视884aa·com| 不卡免费在线视频 | 国产末成年女av片 | 国产成人a一在线观看 | 四虎网站在线播放 | 国产91九色在线播放 | 久久综合九色综合网站 | 亚洲国产精品久久久久婷婷软件 | 久久国产亚洲观看 | 噜噜狠狠 | 亚洲热在线 | 日韩国产成人精品视频人 | 一区二区成人国产精品 |