最近在學(xué)習(xí)ssh框架時(shí),照著網(wǎng)上做了一個(gè)商城系統(tǒng),之前在一些需要用戶存在的操作中,都是在每一個(gè)action中寫重復(fù)的代碼,這樣做現(xiàn)在想起來(lái)并不好,想起了spring的aop,于是想通過(guò)aop來(lái)給每個(gè)需要用戶操作的Action驗(yàn)證用戶登錄狀態(tài)。
為溫江等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及溫江網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、溫江網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
想法是這樣的:
1. 用戶登錄時(shí)把userId放入session中
2. 通過(guò)spring 寫一個(gè)advice來(lái)獲取session中的userId,判斷用戶登錄狀態(tài),如果userId不符合,則拋出自定義異常
3. 通過(guò)struts中配置來(lái)捕獲異常,跳轉(zhuǎn)界面
以下是代碼:
advice代碼:
public class IsUserLoginAdvice{ public void isUserLogin() throws UserNotFoundException{ // TODO Auto-generated method stub int id=0; Map sessionMap=ActionContext.getContext().getSession(); System.out.println(sessionMap); try { //這里在一開(kāi)始時(shí)userId是不存在的可能會(huì)拋出NullPointException,catch起來(lái) id=(int) sessionMap.get("userId"); //在用戶注銷時(shí)我把session中的userId設(shè)為0 if(id==0){ throw new UserNotFoundException(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new UserNotFoundException(); } } }
struts.xml:
這里通過(guò)全局異常映射來(lái)處理這個(gè)異常:
<package name="struts-global" namespace="/" extends="struts-default"> <global-results> <result name="userNotFound">/web_resource/error_jsp/user_not_found.jsp </result> </global-results> <global-exception-mappings> <exception-mapping result="userNotFound" exception="com.lsj.market.exception.UserNotFoundException"></exception-mapping> </global-exception-mappings> </package>
全局異常有個(gè)name屬性,給那些想要共享該異常捕獲的package繼承,這樣就可以共享該異常捕獲行為:
<package name="com.lsj.market.action.user" extends="struts-global">
applicationContext.xml:
<!-- aop設(shè)置 --> <aop:config proxy-target-class="true"> <aop:aspect ref="isUserLoginAdvice"> <aop:pointcut id="isUserLoginPointcut" expression="execution (* com.lsj.market.action..GetUser*.*(..)) or execution (* com.lsj.market.action..*Update*Action*.*(..)) or execution (* com.lsj.market.action..*Delete*Action*.*(..)) or execution (* com.lsj.market.action..GetMarketCar*.*(..)) or execution (* com.lsj.market.action..MarketCar*.*(..)) or execution (* com.lsj.market.action..ToFlower*.*(..)) or execution (* com.lsj.market.action..Flower*Add*.*(..))"/> <aop:before method="isUserLogin" pointcut-ref="isUserLoginPointcut"/> </aop:aspect> </aop:config> <!-- 聲明advice Bean --> <bean id="isUserLoginAdvice" class="com.lsj.market.aop.IsUserLoginAdvice"></bean>
其中pointcut可以通過(guò)or 來(lái)連接多個(gè)切入點(diǎn),這里有這么多切入點(diǎn)是因?yàn)榈谝淮巫?,沒(méi)想到用aop,各個(gè)Action的命名沒(méi)有考慮太多,導(dǎo)致現(xiàn)在必須配置這么多個(gè)切入點(diǎn)表達(dá)式- -!!!
還有一個(gè),如果struts交由spring管理時(shí),即struts.xml中配置了這一句:
<constant name="struts.objectFactory" value="spring" />
在生成代理類時(shí)會(huì)發(fā)生錯(cuò)誤,無(wú)法捕捉到拋出的異常,在網(wǎng)上查了后發(fā)現(xiàn)需要在struts.xml加入這一句,struts就可以捕捉到該異常了:
<!-- 總是確保使用spring的自動(dòng)裝備策略 --> <constant name="struts.objectFactory.spring.autoWire.alwaysRespect" value="true" />
剛剛還想刪除這一句配置后把異常發(fā)上來(lái),但是發(fā)現(xiàn)刪除后居然還可以運(yùn)行?!
算了還是寫上來(lái),以后遇到這個(gè)問(wèn)題,還可以看一下博客。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前名稱:springaopaction中驗(yàn)證用戶登錄狀態(tài)的實(shí)例代碼
當(dāng)前路徑:http://aaarwkj.com/article0/ihhjoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、網(wǎng)站營(yíng)銷、網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)