




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、回顧:Struts第二天:Struts配置# 通配符、動態方法調用# 全局跳轉配置、配置的默認值、常量配置Struts核心業務# 請求數據的自動封裝 (param 攔截器)# 類型轉換自動類型轉換(struts提供)類型轉換器$ 自定義局部- ActionClassNperties$ 自定義全局-perties# 數據處理|-ActionContext |- ServletActionContext# 文件上傳與下載Jsp: <input type=file name=file1 >Action: (上傳,
2、攔截器完成的)Privat File file1; / 拿到上傳的文件對象Private String file1FileName; / 文件名Private String file1ContentType;/類型目標:攔截器(國際化)Ognl表達式語言與Struts標簽1. 攔截器1.1 概述ü 基本概念Intercetor, 即為攔截器。1) 在Struts2中,把每一個功能都用一個個的攔截器實現;用戶想用struts的哪個功能的時候,可以自由組裝使用。2)Struts2中,為了方法用戶對攔截器的引用,提供了攔截器棧的定義,里面可以包含多個攔截器。 文件夾(文件, 文件2) 攔截
3、器棧(攔截器,攔截器2)3)Struts2中,如果用戶沒有指定執行哪些攔截器,struts2有一個默認執行的棧,defaultStack; 一旦如果用戶有指定執行哪些攔截器,默認的攔截器棧就不會被執行攔截器的設計,就是基于組件設計的應用!ü 攔截器配置舉例struts-default.xml文件中,定義了struts提供的所有攔截器!/1. 定義攔截器以及攔截器棧<interceptors> 1.1 攔截器定義 <interceptor name="" class="" /> 1.2 攔截器棧的定義 <interc
4、eptor-stack name="defaultStack">引用了上面攔截器(1.1) </interceptor-stack></interceptors>2. 默認執行的攔截器(棧)<default-interceptor-ref name="defaultStack"/>ü API|- Interceptor 攔截器接口 |- AbstractInterceptor 攔截器默認實現的抽象類; 一般用戶只需要繼承此類即可繼續攔截器開發|- ActionInvocation 攔截器的執行狀態,調用
5、下一個攔截器或Action攔截器與過濾器區別:.1.2 自定義一個攔截器案例步驟:1. 寫攔截器類 (看生命周期)2. 配置/* * 自定義攔截器 * author Jie.Yuan * */public class HelloInterceptor implements Interceptor/ 啟動時候執行public HelloInterceptor()System.out.println("創建了攔截器對象");/ 啟動時候執行Overridepublic void init() System.out.println("執行了攔截器的初始化方法"
6、);/ 攔截器業務處理方法 (在訪問action時候執行? 在execute之前執行?)Overridepublic String intercept(ActionInvocation invocation) throws Exception System.out.println("2. 攔截器,業務處理-開始");/ 調用下一個攔截器或執行Action (相當于chain.doFilter(.)/ 獲取的是: execute方法的返回值String resultFlag = invocation.invoke();System.out.println("4. 攔
7、截器,業務處理-結束");return resultFlag;Overridepublic void destroy() System.out.println("銷毀.");<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-/Apache Software Foundation/DTD Struts Configuration 2.3/EN""/dtds/
8、struts-2.3.dtd"><struts><package name="hello" extends="struts-default"><!- 【攔截器配置】 -><interceptors><!- 配置用戶自定義的攔截器 -><interceptor name="helloInterceptor" class="cn.itcast.a_interceptor.HelloInterceptor"></intercep
9、tor><!- 自定義一個棧: 要引用默認棧、自定義的攔截器 -><interceptor-stack name="helloStack"><!- 引用默認棧 (一定要放到第一行)-><interceptor-ref name="defaultStack"></interceptor-ref><!- 引用自定義攔截器 -><interceptor-ref name="helloInterceptor"></interceptor-ref&g
10、t;</interceptor-stack></interceptors><!- 【執行攔截器】 -><default-interceptor-ref name="helloStack"></default-interceptor-ref><!- Action配置 -><action name="hello" class="cn.itcast.a_interceptor.HelloAction"><result name="succes
11、s"></result></action></package></struts>1.2 攔截器執行流程UML (時序圖)啟動:創建所有攔截器、執行init()訪問:先創建Action,再執行攔截器,最后:攔截器放行,執行execute();1.3 攔截器案例需求:登陸后,顯示列表!案例準備:Struts jar文件DbUtils組件數據庫連接池/ 驅動包-> login.jsp<body> <form method="post" action="$pageContext.req
12、uest.contextPath /user_login.action"> 用戶名:<input type="text" name="admin.userName"><br/> 密碼:<input type="text" name="admin.pwd"><br/> <input type="submit" value="登陸"><br/> </form> </body&
13、gt;à UserAction.javapublic class UserAction extends ActionSupport / -1. 封裝請求數據-private Admin admin;public Admin getAdmin() return admin;public void setAdmin(Admin admin) this.admin = admin;/ -2. 調用的Service-private AdminService adminService = new AdminService();/ 登陸public String login() try Admi
14、n userInfo = adminService.login(admin);/ 判斷if (userInfo = null)/ 登陸失敗return "input"/ 登陸成功:數據保存在session中ActionContext.getContext().getSession().put("userInfo", userInfo);/ 登陸成功return "loginSuccess" catch (Exception e) return ERROR;/ 列表public String list() try / 查詢全部List&
15、lt;Admin> list = adminService.getAll();/ 保存到requestActionContext.getContext().getContextMap().put("listAdmin", list);return "list" catch (Exception e) return ERROR;public String add() return null;à list.jsp<body> <h1>歡迎你,$userInfo.userName </h1> <tabl
16、e align="center" border="1"> <tr> <td>序號</td> <td>編號</td> <td>用戶名</td> <td>密碼</td> </tr> <%-taglib uri=" prefix="c" -%> <!- 用struts標簽迭代數據 -> <%taglib uri="/struts-tags" prefix=&
17、quot;s" %> <s:iterator var="admin" value="#request.listAdmin" status="st"> <tr> <td> <s:property value="#st.count"/> </td> <td> <s:property value="#admin.id"/> </td> <td> <s:property va
18、lue="#admin.userName"/> </td> <td> <s:property value="#admin.pwd"/> </td> </tr> </s:iterator> </table> </body>à 自定義攔截器public class UserCheckInterceptor extends AbstractInterceptor/* * 攔截器業務處理方法 */public String intercept(Acti
19、onInvocation invocation) throws Exception / 拿到當前執行的方法名:判斷,只有當前方法名不是login,就進行驗證/ 獲取ActionContext對象ActionContext ac = invocation.getInvocationContext();/ 獲取action的代理對象 ActionProxy proxy = invocation.getProxy(); / 獲取當前執行的方法名 String methodName = proxy.getMethod(); / 判斷 if (!"login".equals(meth
20、odName) / 先獲取當前登陸的用戶 Object obj = ac.getSession().get("userInfo"); if (obj = null) / 沒有登陸 return "input" else / 當前用戶有登陸 return invocation.invoke(); else / 說明當前用戶正在登陸 return invocation.invoke(); à 配置攔截器<?xml version="1.0" encoding="UTF-8" ?><!DOCT
21、YPE struts PUBLIC"-/Apache Software Foundation/DTD Struts Configuration 2.3/EN""/dtds/struts-2.3.dtd"><struts><package name="user" extends="struts-default"><!- 【攔截器配置】 -><interceptors><interceptor name="
22、;loginCheck" class="erceptor.UserCheckInterceptor"></interceptor><interceptor-stack name="myStack"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="loginCheck"></interceptor-ref
23、></interceptor-stack></interceptors><!- 【執行攔截器:第一種寫法: 當前包下所有的acntion都執行myStack棧】<default-interceptor-ref name="myStack"></default-interceptor-ref> -><!- 全局配置 -><global-results><result name="error">/error.jsp</result></gl
24、obal-results><action name="user_*" class="cn.itcast.action.UserAction" method="1"><!-第二種寫法: 只是在這一個Action中執行myStack棧 <interceptor-ref name="defaultStackt"></interceptor-ref><interceptor-ref name="loginCheck"></intercep
25、tor-ref>-><!- 第三種寫法:執行用戶棧(與第二種寫法一樣, 只在當前aciton中執行自定義棧) -><interceptor-ref name="myStack"></interceptor-ref><!- 1. 登陸失敗 -><result name="input">/login.jsp</result><!- 2. 登陸成功 -><result name="loginSuccess" type="redir
26、ectAction">user_list</result><!- 3. 列表展示 -><result name="list">/WEB-INF/list.jsp</result></action></package></struts>2. Struts2中的國際化回顧:Servlet 中國際化:1. 寫資源文件基礎名.properties 【默認的語言環境的配置】 基礎名_語言簡稱_國家簡稱.properties2. 讀取資源文件,再使用程序:ResourceBundleJs
27、p: jstl提供的格式化與國際化標簽庫。Struts2中國際化:1. 寫資源文件 (同servlet)2. 讀資源文件程序:ResourceBundle (同servlet)JSP: 1)jstl表親啊 (同servlet)2)struts標簽獲取資源文件內容區別:Struts2加載資源文件更加簡單!通過常量加載即可!再在jsp頁面直接使用!à1. 寫資源文件Mperties 默認的語言環境; 找不到配置就找它Msg_en_US.properties 美國-à2. 加載<constant name="struts.custom.i18n.res
28、ources" value="cn.itcast.config.msg"></constant>à3. 使用: 標簽name值直接寫配置文件中的key<s:text name="title"></s:text>另外一點,(推薦)加載資源文件通過常量加載還可以在頁面加載, 這樣用:<s:i18n name="cn.itcast.config.msg"><s:text> 標簽必須放到標簽體中。</s:i18n>3. Ognl表達式語言概述l
29、OGNL表達式 OGNL是Object Graphic Navigation Language(對象圖導航語言)的縮寫,它是一個開源項目。 Struts2框架使用OGNL作為默認的表達式語言。l OGNL優勢 1、支持對象方法調用,如xxx.doSomeSpecial(); 2、支持類靜態的方法調用和值訪問,表達式的格式: 類全名(包括包路徑)方法名 | 值名,例如: java.lang.Stringformat('foo %s', 'bar') 或tutorial.MyConstantAPP_NAME; 3、支持賦值操作和表達式串聯,如price=
30、100, discount=0.8, calculatePrice(),這個表達式會返回80; 4、訪問OGNL上下文(OGNL context)和ActionContext; 5、操作集合對象。l 總結 OGNL 有一個上下文(Context)概念,說白了上下文就是一個MAP結構,它實現了 java.utils.Map 的接口。 OgnlContext對象分析:ü Struts框架默認就支持Ognl表達式語言。(struts必須引用的包:ognl.jar)ü 作用頁面取值用。El表達式語言,用于頁面取值,jsp頁面取值的標準。(默認直接可以使用) (應用范圍更廣。)Ogn
31、l表達式語言, struts標簽默認支持的表達式語言。 必須配置struts標簽用,不能離開struts標簽直接用。OgnlContext對象(了解)OgnlContext對象是ognl表達式語言的核心。源碼類:public class OgnlContext extends Object implements Map.硬編碼方式,了解OgnlContext對象:/ OgnlContext用法public class OgnlDemo1 /* * 1. Ognl表達式語言語言取值,取非根元素的值,必須用#號 * throws Exception */Testpublic void testOg
32、nl() throws Exception / 創建一個Ognl上下文對象OgnlContext context = new OgnlContext();/ 放入數據User user = new User();user.setId(100);user.setName("Jack");/ 【往非根元素放入數據, 取值的時候表達式要用"#"】context.put("user", user);/ 獲取數據(map)/ 先構建一個Ognl表達式, 再解析表達式Object ognl = Ognl.parseExpression("
33、;#");Object value = Ognl.getValue(ognl, context, context.getRoot();System.out.println(value);/* * 2. Ognl表達式語言語言取值,取根元素的值,不用帶#號 * throws Exception */Testpublic void testOgn2() throws Exception / 創建一個Ognl上下文對象OgnlContext context = new OgnlContext();/ 放入數據User user = new User();user.setI
34、d(100);user.setName("Jack");/ 【往根元素放入數據】context.setRoot(user);/ 獲取數據(map)/ 先構建一個Ognl表達式, 再解析表達式Object ognl = Ognl.parseExpression("vince");Object value = Ognl.getValue(ognl, context, context.getRoot();System.out.println(value);/* * 3.Ognl對 靜態方法調用的支持 * throws Exception
35、*/Testpublic void testOgn3() throws Exception / 創建一個Ognl上下文對象OgnlContext context = new OgnlContext();/ Ognl表單式語言,調用類的靜態方法/Object ognl = Ognl.parseExpression("Mathfloor(10.9)");/ 由于Math類在開發中比較常用,所以也可以這樣寫Object ognl = Ognl.parseExpression("floor(10.9)");Object value = Ognl.getValue
36、(ognl, context, context.getRoot();System.out.println(value);ValueStack對象ValueStack, 即值棧對象。值棧對象:是整個struts數據存儲的核心,或者叫中轉站。用戶每次訪問struts的action,都會創建一個Action對象、值棧對象、ActionContext對象; 然后把Action對象放入值棧中; 最后再把值棧對象放入request中,傳入jsp頁面。(key: struts.valueStack); 開發者只需要通過ActionContext對象就可以訪問struts的其他的關鍵對象。 (ActionCo
37、ntext是給開發者用的,便于學習與使用。)ActionRequest|-ValueStack|-ActionActionContextValueStack只要記住值棧中有List 和Map兩個東西,Map是存放request,session,application等域對象的地方,而list是存放各種常量,變量|-ObjectStack(把動作和相關對象壓入ObjectStack)是一個List|-ContextMap(把各種各樣的映射關系壓入ContextMap中)問題:OgnlContext與ValueStack對象的關系?值棧中包含有OgnlContext對象Ognl是一個Map結構的數
38、據存儲中心Struts標簽Struts標簽取值,就使用了Ognl表達式語言。對ValueStack的深入理解ValueStack是一個接口,而OgnlValueStack是strtus2中的缺省實現。ValueStack中的數據,分兩個部分存放:root(CompoundRoot)和context(OgnlContext)(這與OGNL中的概念一致),同時ValueStack暴露相關的接口:void setValue(String expr, Object value);Object findValue(String expr);用來通過OGNL表達式對ValueStack中的數據進行操作!V
39、alueStack中的root對象是CompoundRoot,CompoundRoot繼承了ArraryList,提供了額外的方法:push()和pop()方法,用來對root對象中所包含的數據進行存取!在Struts2中,一個請求在最終到達Action的方法之前,Action對象本身會被壓入ValueStack(實際上就是放到ValueStack的CompoundRoot中),所以Action對象是CompoundRoot中的一個元素。看圖看下面的代碼:public class UserAction private String username;
40、60; private Integer age; private boolean valid; /查看用戶的詳細信息 public String detail() username = "張三" age = 18;
41、0; valid = true; return "detail" 不加#號取根元素值在Action中,給Action的username/age/valid賦值。Detail頁面如下:username:<s:property value="username"/> <br/>valid:<s:property value="valid"/> <br/>age:<s:propert
42、y value="age"/> <br/> 上述JSP頁面將能正確將它們的值取出。<s:property value=”ognl表達式”/>。在s:property標簽中的OGNL表達式,最終會交給ValueStack來解釋。username就是一個OGNL表達式,意思是調用root對象的getUsername()方法。Struts2將自動搜索CompoundRoot中有哪些元素(從第0個元素開始搜索),檢測這些元素是否有getUsername()方法,如果第0個元素沒有getUsername()方法,將繼續搜索第1、2、3個元素是否
43、有getUsername()方法。再看下面的例子:public class UserAction private String username; private String name; /查看用戶的詳細信息 public String detail() username = "張三"
44、name = "王五" User u = new User(); u.setUsername("趙毅"); ActionContext.getContext().getValueStack().push(u); return "detail"
45、; 這個例子通過ValueStack的接口方法push(object)往ValueStack中推入User的實例對象。在上面這個UserAction的代碼中,我們直接調用ActionContext.getContext().getValueStack().push()方法,把一個User對象(這個對象擁有getUsername()和setUsername()方法)直接壓入到ValueStack中,這時候,在ValueStack的CompoundRoot中將有兩個元素:第0個元素是剛剛壓入的user對象趙毅,而第1個元素是userAction對象張三,如果在J
46、SP中使用下面的表達式來取值:<s:property value=”username”/> ,那么輸出的值將是“趙毅”!道理上面已經講過了,struts2將會從第0個元素開始搜索CompoundRoot中的對象,第0個元素正是剛剛壓入的那個user對象!如果在JSP中使用<s:property value=”name”/>來取值,將取出“王五”,因為第0個元素user對象沒有name屬性,所以,會繼續搜索第1個元素userAction對象,在這個對象中就有name屬性了!存儲的自定義字段即通過ValueStack的Set方法設置的東西,都會放到棧的HashMap<
47、K,V>中深入理解ActionContextActionContext.getContext().getValueStack();我們知道ActionContext有一個getContext方法用來獲取OgnlContext對象,OgnlContext中有getValueStack方法,可以通過該方法獲得值棧對象(ValueStack)OgneContext.getValueStack();獲得值棧,但是這個方法只是方便調試用;實際上,我們獲得ValueStack,只能用request.getAttribute(“struts.valueStack”);再來重點觀察OgnlContext
48、這個非常重要的容器:OgnlContext實現了Map接口,這里_values私有屬性存放的是Map的鍵值對信息,我們展開里面的table節點觀察里面存放了什么秘密:6、我只是展開了部分,從中我們可以看出Struts2放置請求表單參數、請求對象本身、session對象、application對象和application對象屬性等等到OGNL棧(事實上放在OgnlContex).Struts 2 places request parameters and request, session, and application attributes on the OGNL stack (in fact
49、 the OGNL context). 小結: 1、Struts2數據傳輸DataTransfer的核心對象是OgnlValueStack、OgnlContext,OgnlValueStack持有OgnlContext和root對象,注意:OgnlContext和root對象是ognl的核心要素【參考我的轉載 2、OgnlContext這個容器存放了所有本次Web請求響應的所有相關信息對象,root的存放的多個根對象。 3、Struts2 Ognl表達式的解析主要依賴OgnlValueStack去完成。表達式中如果沒有帶#,如:“
50、”,那么OgnlValueStack將去根對象中去遍歷每個對象看是否某個對象的方法是否匹配"emp.getName()",匹配則執行,否則跑出OgnlExcpetion異常。如果帶有#,則去Ognl上下文中去尋找是否有滿足屬性的key,有責返回。 4、Struts2的ActionContext類只是為了訪問ValueStack而提供的一個Facade【門面設計模式】,為程序員訪問各種信息提供一個一致的界面。 5、使用Ognl表達式從OgnlValueStack中取出值: 代碼:深入理解Struts遍歷在Actio
51、n中定義,直接在棧頂的元素一,獲取Map中的對象不同屬性studentMap=new HashMap<String,Student>();studentMap.put("student1",new Student(new Long(1),"20034140201","張三1","男",25);studentMap.put("student2",new Student(new Long(2),"20034140202"
52、;,"張三2","女",26);studentMap.put("student3",new Student(new Long(3),"20034140202","張三3","男",27); 用法:(這是遍歷對象的,實例后.屬性值)· <s:iterator value="studentMap" id="column"&
53、gt; · <tr> · <td><s:property value="#column"/></td> · <td><s:property value="value.id"/></td>
54、 · <td><s:property value="value.num"/></td> · <td><s:property value=""/></td> · <
55、;td><s:property value="value.sex"/></td> · <td><s:property value="value.age"/></td> · </tr> · </s:i
56、terator> 二獲取Map中數組的值· arrayMap=new HashMap<String,String>(); · arrayMap.put("arr1", new String"1","2003401","leejie&qu
57、ot;,"male","20"); · arrayMap.put("arr2", new String"2","2003402","huanglie","male","25"); ·
58、 arrayMap.put("arr3", new String"3","2003403","lixiaoning","male","21"); 用法· <s:iterator value="arrayMap" id="column"> ·
59、60; <tr> · <td><s:property value="#column"/></td> · <td><s:property value="value0"/></td> ·
60、160; <td><s:property value="value1"/></td> · <td><s:property value="value2"/></td> · <td><s:property value="va
61、lue3"/></td> · <td><s:property value="value4"/></td> · </tr> · </s:iterator> 三迭代Map中含有List· li
62、stMap=new HashMap<String,List<Student>>(); /定義一個Map,存放List數據 · List<Student> list1=new ArrayList<Student>(); /定義一個list,存放Student對象·
63、0; list1.add(new Student(new Long(1),"20034140201","張三1","男",25); · list1.add(new Student(new Long(2),"20034140202","張三2",&q
64、uot;男",25); · list1.add(new Student(new Long(3),"20034140203","張三3","男",25); · listMap.put("class1", list1); &
65、#160; · · List<Student> list2=new ArrayList<Student>(); · list2.add(new Student(new Long(1),"20034140301","李四1","男",20); · list2.add(new Student(new Long(2),"20034140302","李四2","男",21); · li
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 護士護理業務查房:提升技能與優化護理實踐
- 四川應用技術職業學院《開發基礎》2023-2024學年第二學期期末試卷
- 江蘇省宜興市丁蜀區重點名校2024-2025學年初三下學期3月10日周中測數學試題試卷含解析
- 云南師范大學《產品U界面設計》2023-2024學年第二學期期末試卷
- 浙江省龍游第二高級中學2025屆高三一輪第五次階段性過關物理試題試卷含解析
- 浙江宇翔職業技術學院《金融綜合實訓》2023-2024學年第二學期期末試卷
- 山西林業職業技術學院《冶金資源工程》2023-2024學年第二學期期末試卷
- 云南醫藥健康職業學院《寄生蟲學及檢驗》2023-2024學年第二學期期末試卷
- 中國科學技術大學《運動技能學習與控制》2023-2024學年第二學期期末試卷
- 內江師范學院《工程軟件應用》2023-2024學年第二學期期末試卷
- 第一屆山東省職業能力大賽濟南市選拔賽制造團隊挑戰賽項目技術工作文件(含樣題)
- 高中歷史選擇性必修第3冊試卷
- 老干工作業務培訓
- GB/T 44744-2024糧食儲藏低溫儲糧技術規程
- 加工制作合同(儲存罐)
- 2023-2024學年廣東省深圳市寶安區八年級(下)期末英語試卷
- 雙碳全景系列培訓第一章碳達峰、碳中和
- DB11T 594.2-2014 地下管線非開挖鋪設工程施工及驗收技術規程第2部分 頂管施工
- DB11∕T 1832.17-2021 建筑工程施工工藝規程 第17部分:電氣動力安裝工程
- 出租屋轉租補充協議書范文范本
- 2024年礦山救護工(高級技師)技能鑒定理論考試題庫(含答案)
評論
0/150
提交評論