生活随笔
收集整理的這篇文章主要介紹了
ssh 三者集合的思想
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、首先應該明白,在SSH中,spring做了兩次集成,第一次是與struts做了集成,第二次是與hibernate集成。
二、下面來談第一次集成,與struts集成,通過修改struts-config.xml 中的action type 為 org.springframework.web.struts.DelegatingActionProxy,將action的控制轉到spring 容器,當轉到spring容器, spring會在spring的配置文件中找與action 的path屬性同名的bean從而執行相應的類的操作.
?< action ?? ?????attribute ="loginForm" ?? ?????input ="login.jsp" ?? ?????name ="loginForm" ?? ?????path ="/login" ?? ?????scope ="request" ?? ?????type ="org.springframework.web.struts.DelegatingActionProxy" ?> ?? ?? < bean ?name ="/login" ?class ="com.ssh.action.LoginAction" ?singleton ="false" > ?? < property ?name ="dao" > ?? < ref ?bean ="UsersDAOImp" /> ?? property > ?? bean > ??
xml 代碼
?三、第二次集成,與hibernate集成, DAO 繼承了org.springframework.orm.hibernate3.support.HibernateDaoSupport類,所以可以簡單的利用sping提供的一些功能。 HibnernateDaoSupport實現了HibernateTemplate與SessionFactory的關聯。HibernateTemplate(用HibnernateDaoSupport里的getHibernateTemplate()可以獲得)對Hibernate Session進行了封裝,可省去獲得Session實例、事務啟動與提交/回滾以及異常處理等一系列步驟,所以非常簡單。
java 代碼
public ?class ?UsersDAOImp?extends ?HibernateDaoSupport?implements ?UserDao?{ ?? ?? ????private ?static ?final ?Log?log?=?LogFactory.getLog(UsersDAOImp.class ); ?? ?? ????protected ?void ?initDao()?{ ?? ?????????? ????} ?? ???? ?? ??? ? ?? public ?void ?save(Users?transientInstance)?{ ?? ????????log.debug("saving?Users?instance" ); ?? ????????try ?{ ?? ????????????getHibernateTemplate().save(transientInstance); ?? ????????????log.debug("save?successful" ); ?? ????????}?catch ?(RuntimeException?re)?{ ?? ????????????log.error("save?failed" ,?re); ?? ????????????throw ?re; ?? ????????} ?? ????} ?? ???? ?? ????? ? ?? ????public ?void ?delete(Users?persistentInstance)?{ ?? ????????log.debug("deleting?Users?instance" ); ?? ????????try ?{ ?? ????????????getHibernateTemplate().delete(persistentInstance); ?? ????????????log.debug("delete?successful" ); ?? ????????}?catch ?(RuntimeException?re)?{ ?? ????????????log.error("delete?failed" ,?re); ?? ????????????throw ?re; ?? ????????} ?? ????} ?? ???? ?? ????? ? ?? ????public ?Users?findById(?java.lang.Integer?id)?{ ?? ????????log.debug("getting?Users?instance?with?id:?" ?+?id); ?? ????????try ?{ ?? ????????????Users?instance?=?(Users)?getHibernateTemplate() ?? ????????????????????.get("com.ssh.entity.Users" ,?id); ?? ????????????return ?instance; ?? ????????}?catch ?(RuntimeException?re)?{ ?? ????????????log.error("get?failed" ,?re); ?? ????????????throw ?re; ?? ????????} ?? ????} ?? ???? ?? ???? ?? ????? ? ?? ????public ?List?findByExample(Users?instance)?{ ?? ????????log.debug("finding?Users?instance?by?example" ); ?? ??????? ?? ????????try ?{ ?? ????????????List?results?=?getHibernateTemplate().findByExample(instance); ?? ????????????log.debug("find?by?example?successful,?result?size:?" ?+?results.size()); ?? ????????????return ?results; ?? ????????}?catch ?(RuntimeException?re)?{ ?? ????????????log.error("find?by?example?failed" ,?re); ?? ????????????throw ?re; ?? ????????} ?? ????}???? ?? ???? ?? ????? ? ?? ????public ?List?findByProperty(String?propertyName,?Object?value)?{ ?? ??????log.debug("finding?Users?instance?with?property:?" ?+?propertyName ?? ????????????+?",?value:?" ?+?value); ?? ??????try ?{ ?? ?????????String?queryString?=?"from?Users?as?model?where?model." ? ?? ????????????????????????????????+?propertyName?+?"=??" ; ?? ?????????return ?getHibernateTemplate().find(queryString,?value); ?? ??????}?catch ?(RuntimeException?re)?{ ?? ?????????log.error("find?by?property?name?failed" ,?re); ?? ?????????throw ?re; ?? ??????} ?? ????}?? <property name="dao">
</property>
代碼不全。
四、當然,你得首先把spring環境搭起來,通過在struts-config.xml 增加? xml 代碼
<plug-in classname="org.springframework.web.struts.ContextLoaderPlugIn"></plug-in>
< plug-in ?className ="org.springframework.web.struts.ContextLoaderPlugIn" > ?? ????< set-property ?property ="contextConfigLocation" ? ?? ????????????value ="/WEB-INF/applicationContext.xml" ?/> ?? plug-in > ??
?????????
?
?
總結
以上是生活随笔 為你收集整理的ssh 三者集合的思想 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。