日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統(tǒng) 2111 0
基于J2EE的SSH
整合應(yīng)用及操作示例二(CRUD操作及配置)
2009-10-08 09:25:22
原創(chuàng)作品,允許轉(zhuǎn)載,轉(zhuǎn)載時(shí)請務(wù)必以超鏈接形式標(biāo)明文章 原始出處 、作者信息和本聲明。否則將追究法律責(zé)任。 http://gaochaojs.blog.51cto.com/812546/210131
CRUD
是指在做計(jì)算處理時(shí)的增加 (Create) 、查詢 (Retrieve) (重新得到數(shù)據(jù))、更新 (Update) 和刪除 (Delete) 幾個(gè)單詞的首字母簡寫。主要被用在描述軟件系統(tǒng)中數(shù)據(jù)庫或者持久層的基本操作功能。
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.
在響應(yīng)新增部門請求中,通過導(dǎo)入 Struts 的配置文件 (struts-config.xml) 來完成業(yè)務(wù)流程的部署。它把 depAdd.jsp depAdd.do 連接起來。 depAdd.jsp 上產(chǎn)生客戶請求, depAdd.do 則對請求產(chǎn)生響應(yīng)、處理 jsp 頁面上的數(shù)據(jù)。當(dāng)點(diǎn)擊 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>)
先檢查頁面數(shù)據(jù)的正確性,校驗(yàn)通過后將輸入數(shù)據(jù)用 Set 方法存入到 userinfoForm 這個(gè) 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個(gè)字符小于10個(gè)字符!') return false ; } if (repassword=="") { alert('重復(fù)密碼不能為空!') 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( "聯(lián)系電話格式不正確!" ); return false ; } } } if (department=="") { alert('部門管理員所屬部門不能為空!') return false ; }

else { document.userinfoForm.submit(); }

}
然后根據(jù) struts-config.xml 調(diào)用 depAdd.do ( 這將在 Spring 配置文件中指定相應(yīng)的 Action) 進(jìn)行業(yè)務(wù)處理。在 depAdd.do 中頁面數(shù)據(jù)將從 userinfoForm 中讀取。 depAdd.do 執(zhí)行成功后將顯示 /ok.jsp 頁面。 depAdd.do 對應(yīng)的 Action (DepAddAction) Spring 的配置文件 (applicationContext.xml) 中指定。要把 depAdd.do DepAddAction 對應(yīng)起來 , 首先要在 struts-config.xml 中配置 Delegating RequestProcessor 。其次,需要在 application Context.xml 中定義名字為“ /depAdd ”的受管 JavaBean 。每次對 DepAddAction 請求時(shí), Delegating Request Processor 將充當(dāng)代理。同時(shí), DepAddAction 使用到受管 Java Beansm Service 。要使用 UserinfoService ,需要在 DepAddAction 中生成 UserinfoService get() set() 方法,并且 application Context.xml 中對“ /depAdd ”進(jìn)行 Dep Add Action 的依賴注入。因?yàn)? DepAddAction 并沒有直接去操作數(shù)據(jù)訪問 Userinfo DAO 。而是通過調(diào)用業(yè)務(wù)邏輯層 UserinfoService 中的方法來實(shí)現(xiàn)業(yè)務(wù)邏輯的。 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 // 數(shù)據(jù)入庫
userinfoService.save(userinfo);
如果 depAdd.do 要對應(yīng)另一個(gè) Action ,則只要修改 applicationContext.xml 即可,這將有利于系統(tǒng)的更新。同樣,如果另一個(gè) .do 要對應(yīng) DepAddAction ,也只要在 applicationContext.xml 中配置即可,這將有利于代碼的重用。在本系統(tǒng)中, Hibernate Spring 共用一個(gè)配置文件 applicationContext.xml Hibernate applicationContext.xml 中讀取和數(shù)據(jù)庫有關(guān)的信息。數(shù)據(jù)庫信息包括數(shù)據(jù)庫連接、與數(shù)據(jù)庫結(jié)構(gòu)相對應(yīng)的映射文件。在新增部門請求中,涉及到的數(shù)據(jù)庫表為 userinfo 表,它所對應(yīng)的映射文件為 Userinfo.hbm.xml 。為了訪問數(shù)據(jù)庫表 userinfo ,只有 Userinfo.hbm.xml 映射文件是不夠的,還需要數(shù)據(jù)訪問類 UserinfoDAO 、數(shù)據(jù)類 AbstractUserinfo,Userinfo 。數(shù)據(jù)類 Userinfo 的實(shí)現(xiàn)較為簡單,它只是 Java 對象與數(shù)據(jù)庫表之間的對應(yīng),主要用于在各應(yīng)用層間傳遞數(shù)據(jù),在它的基礎(chǔ)上要實(shí)現(xiàn)的就是數(shù)據(jù)訪問類 UserinfoDAO 。系統(tǒng)在生成 UserinfoDAO 的同時(shí),也將 UserinfoDAO 作為 JavaBean 配置到 applicationContext.xml 中。 UserinfoDAO 中是對 userinfo 表進(jìn)行保存、查詢、刪除或修改等基本數(shù)據(jù)操作,在 applicationContext.xml 中需要 userinfoService 進(jìn)行 UserinfoDAO 及其代理的依賴注入。這樣做,使得當(dāng) UserinfoDAO 變化時(shí),只需修改 applicationContext.xml userinfoService 實(shí)現(xiàn)新的注入,指向新的實(shí)現(xiàn)就可以了,由此解除了數(shù)據(jù)訪問層和業(yè)務(wù)層的緊密耦合。數(shù)據(jù)訪問類 UserinfoDAO 繼承于輔助類 Hibernate-DaoSupport ,借助于 getHibernateTemplate() 獲得對 Hibernate 資源的操作,極大的方便了 Hibernate 框架的使用。在 UserinfoDAO 中定義了對數(shù)據(jù)庫表 userinfo 的操作函數(shù)。如下面代碼,即是 UserinfoService 中調(diào)用的 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 中的代碼如下:
< ! -- 創(chuàng)建事務(wù)管理類 -- > <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> < property name ="sessionFactory" > < ref local ="sessionFactory" /> </ property > </ bean >

< ! -- 創(chuàng)建用戶事務(wù)代理類 -- > <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 >


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


< ! --調(diào)度管理#新增部門管理員 -- > <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 >

< ! -- 調(diào)度管理#新增加部門管理員-- > <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 >

在本項(xiàng)目的開發(fā)過程中運(yùn)用這樣的技術(shù)方法大大提高了開發(fā)效率、增加了程序的可讀性、減少了模塊間的耦合性,這樣使得系統(tǒng)更加容易更新和維護(hù)。

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


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 白水县| 安吉县| 太仆寺旗| 抚顺市| 铁岭市| 乌拉特前旗| 都匀市| 乐至县| 禹州市| 天气| 亚东县| 海口市| 霍林郭勒市| 天津市| 郓城县| 日照市| 民丰县| 肇源县| 盐亭县| 康乐县| 乐清市| 泗水县| 南漳县| 滦南县| 黔南| 五大连池市| 元朗区| 泽普县| 竹北市| 安西县| 太谷县| 鄂托克旗| 手游| 冕宁县| 南漳县| 嘉祥县| 石首市| 沅江市| 临夏县| 瑞安市| 江安县|