下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
實驗目的掌握攔截器的使用。實驗內(nèi)容自定義攔截器對發(fā)表留言的操作進行攔截。如果用戶已經(jīng)登錄,則能夠提交留言;否則,不能提交留言,提示用戶需要先登錄。三、實驗過程程序目錄結(jié)構(gòu):Struts配置文件:<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN""/dtds/struts-2.0.dtd"><struts> <constantname="struts.enable.DynamicMethodInvocation" value="true"/> <constantname="struts.devMode"value="false"/> <includefile="struts-default.xml"/> <packagename="StrutsExample"extends="struts-default"> <interceptors> <interceptorname="leaveWordInterceptor"class="erceptor.LeaveWordInterceptor"/> </interceptors><default-interceptor-refname="defaultStack"/> <actionname="word"class="com.action.WordAction"> <resultname="success">success.jsp</result> <resultname="login">login.jsp</result> <interceptor-refname="defaultStack"/> <interceptor-refname="leaveWordInterceptor"/> </action> <actionname="login"class="com.action.LoginAction"> <resultname="success">leaveword.jsp</result> </action> </package></struts>LoginAction.java程序:packagecom.action;importjava.util.Map;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionSupport;importcom.user.User;publicclassLoginActionextendsActionSupport{//處理用戶登錄 privateUseruser; publicUsergetUser(){ returnuser; } publicvoidsetUser(Useruser){ this.user=user; } @SuppressWarnings("unchecked") publicStringexecute() {ActionContextac=ActionContext.getContext(); Map<String,String>session1=(Map<String,String>)ac.getSession(); session1.put("username",user.getUserName());//把登錄用戶名保存在session中,供攔截器訪問 //省略登錄代碼 return"success"; }}WordAction.java程序:packagecom.action;importcom.word.Word;publicclassWordAction{//處理留言信息 privateWordword; publicvoidsetWord(Wordword){ this.word=word; } publicWordgetWord(){ returnword; } publicStringexecute(){ //處理留言代碼 return"success"; }}LeaveWordInterceptor.java程序:packageerceptor;importjava.util.Map;importcom.opensymphony.xwork2.Action;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionInvocation;importerceptor.AbstractInterceptor;publicclassLeaveWordInterceptorextendsAbstractInterceptor{ publicStringintercept(ActionInvocationai)throwsException{ ActionContextac=ai.getInvocationContext();//返回ActionContext的對象,供需要ActionContext時用 Map<String,String>session=(Map<String,String>)ac.getSession(); StringuserName=(String)session.get("username"); if((userName!=null)&&(userName.length()>0)){ returnai.invoke();//放行,把執(zhí)行控制權(quán)轉(zhuǎn)交給WordAction } else{ System.out.println("dksdkdk"); ac.put("sorry","對不起,請先登錄!");//保存一條信息在ac中,以便在login.jsp上顯示 returnAction.LOGIN;//替被它攔截的Action返回LOGIN } }}Leaveword.jsp程序:<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%><%Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><basehref="<%=basePath%>"><title>提交留言頁面</title> <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="Thisismypage"> <!-- <linkrel="stylesheet"type="text/css"href="styles.css"> --></head><body><center>提交留言:<s:formaction="word"> <s:textfieldname="word.title"label="標題"/> <s:textareaname="word.message"label="內(nèi)容"/> <s:submitvalue="提交"></s:submit> </s:form></center></body></html>login.jsp程序:<%@pagecontentType="text/html;charset=UTF-8"language="java"%><%@taglibprefix="s"uri="/struts-tags"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""/TR/html4/loose.dtd"><html><head><title>用戶登錄頁</title></head><bodyvLink="#006666"link="#003366"bgColor="#E0F0F8"> <center> <b>${sorry}</b><br><br> 用戶登錄<br><br> <s:formaction="login"> <s:textfi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 給水管道安裝施工方案
- DB15-T 2849.8-2024 防雷技術(shù)規(guī)范 第8部分:電動汽車充電站樁
- 5《搭石》第二課時 教學設計-2024-2025學年統(tǒng)編版語文五年級上冊
- 12《蒹葭》活動任務型教學設計
- 24 三顧茅廬2024-2025學年九年級語文上冊同步教學設計(河北專版)
- 八年級英語上冊Unit10ImgoingtobeabasketballplayerSectionA知識點和練習人教新目標版
- 2024-2025學年新教材高中政治第一單元基本經(jīng)濟制度與經(jīng)濟體制2.1使市場在資源配置中起決定性作用教案部編版必修2
- 第六屆全國驗光與配鏡職業(yè)技能競賽(眼鏡定配工賽項)理論考試題庫(含答案)
- 2024年江蘇省百萬城鄉(xiāng)建設職工職業(yè)技能競賽水務行業(yè)化學檢驗員考試題庫-上(單選題)
- 班級文化墻的建設與維護計劃
- 回旋鏢飛行原理
- 臥式容器容積計算
- 銀行支行節(jié)能減排工作計劃
- 建筑勞務公司組織機構(gòu)示意圖【精選文檔】
- 專利審查協(xié)作中心筆試重點詞匯(僅供參考)
- Excel股票交易記錄表
- 病毒性心肌炎的護
- 《壓瘡傷口的護理》PPT課件.ppt
- 少數(shù)民族服飾簡筆畫文檔(共48頁)
- 土石方運輸記錄表(空表)
- 新高考背景下學校課程設置與實施
評論
0/150
提交評論