實現了Action處理類之后,就可以在struts.xml中配置該Action,從而讓Struts 2框架知道哪個Action處理哪個請求,即建立用戶請求和Action類之間的對應關系。
Action基本配置
Struts 2使用package包來組織Action,在struts.xml中通過使用package下的action元素來配置Action。在配置Action時,需要指定action元素的name和class屬性。
- name屬性:指定Action的名字,即指明該Action所處理的請求的URL,例如,若name屬性值為login,則請求該Action的URL是login.action
- class屬性:指定Action的實現類,該屬性不是必須的,如果沒有指定class屬性的值,則默認使用ActionSupport類。
Action基本配置代碼如下:
1
2
3
|
< package name = "default" namespace = "/" extends = "struts-default" > < action name = "example" class = "com.example.struts.action.expAction" > </ package > |
Action只是一個邏輯控制器,不直接對用戶請求生成任何相應。因此,Action處理完用戶請求后需要將指定的視圖資源呈現給用戶,即配置Action時,應該配置邏輯視圖和物理視圖資源之間的映射。
配置邏輯視圖和物理視圖之間的映射關系是通過<result>元素來定義的,每個<result>元素定義邏輯視圖和物理視圖之間的一個映射:
1
2
3
4
5
|
< package name = "default" namespace = "/" extends = "struts-default" > < action name = "example" class = "com.example.struts.action.expAction" > < result name = "success" >/success.jsp</ result > < result name = "error" >/error</ result > </ package > |
動態方法調用
有時一個Action內需要包含多個控制處理邏輯。例如,對于同一個表單,當用戶通過不同的提交按鈕進行提交時,系統需要使用Action的不同方法進行處理用戶請求,此時就需要讓Action中包含多個控制處理邏輯。
Struts 2框架允許一個Action中包含多個處理邏輯。在Struts 2中請求一個Action中的不同處理邏輯方法的方式成為DMI(Dynamic Method Invocation,動態方法調用),其請求格式如下:
1
|
(ActionName)!(methodName).action |
-
ActionName是Action的名字,即struts.xml中配置的Action的name屬性值;
-
methodName是Action實現類中處理邏輯的方法名。
動態方法調用示例
1
2
|
//訪問product中的edit()方法 product!edit.action |
productList.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>商品列表</title> </head> <body> <table border= "1" > <tr> <th>商品ID</th> <th>商品名稱</th> <th>數量</th> <th colspan= "2" >操作</th> </tr> <tr> <td> 1001 </td> <td>小米手機</td> <td> 128 </td> <td><a href= "product!edit.action?productId=1001" rel= "external nofollow" >編輯</a></td> <td><a href= "product!del.action?productId=1001" rel= "external nofollow" >刪除</a></td> </tr> <tr> <td> 1002 </td> <td>佳能相機</td> <td> 100 </td> <td><a href= "product!edit.action?productId=1002" rel= "external nofollow" >編輯</a></td> <td><a href= "product!del.action?productId=1002" rel= "external nofollow" >刪除</a></td> </tr> </table> </body> </html>``` |
上述代碼中,商品列表中的每個商品使用超鏈接進行編輯、刪除操作。超鏈接中href屬性值采用動態方法調用的方式進行鏈接請求,并將產品ID作為參數傳遞給Action。
ProductAction.java代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package com.qst.chapter03.action; import com.opensymphony.xwork2.ActionSupport; public class ProductAction extends ActionSupport { private int productId; public int getProductId() { return productId; } public void setProductId( int productId) { this .productId = productId; } // 編輯商品 public String edit() { System.out.println( "編輯商品" + productId); // ...省略一些編輯商品的業務 return "edit" ; } // 刪除商品 public String del() { System.out.println( "刪除商品" + productId); // ...省略一些刪除商品的業務 return "del" ; } } |
上述代碼創建了兩個業務方法edit()和del()方法。當用戶單擊不同的鏈接時,系統將交給對應的方法處理。
接下來編寫edit.jsp和del.jsp頁面:
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <title>編輯商品</title> </head> <body> ${param.productId}商品編輯 </body> </html> |
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <title>刪除商品</title> </head> <body> ${param.productId}商品刪除成功! </body> </html> |
在struts.xml中配置ProductAction代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> < struts > <!-- 指定Struts2處于開發階段,可以進行調試 --> < constant name = "struts.devMode" value = "true" /> < constant name = "struts.enable.DynamicMethodInvocation" value = "true" /> <!-- Struts2的Action都必須配置在package里,此處使用默認package --> < package name = "default" namespace = "/" extends = "struts-default" > <!-- 定義一個名為user的Action,實現類為com.qst.chapter03.action.LoginAction --> < action name = "product" class = "com.qst.chapter03.action.ProductAction" > < result name = "edit" >/edit.jsp</ result > < result name = "del" >/del.jsp</ result > </ action > </ package > </ struts > |
上述配置文件配置了常量struts.enable.DynamicMethodInvocation的值為true,這樣Struts 2才會開啟動態方法調用,否則默認不會開啟動態方法調用。
使用method屬性及通配符
除了動態方法調用之外,Struts 2還提供了另一種處理方法,即將Action處理類定義成多個邏輯Action。此時,在配置<action>元素時,需要指定name、class和method屬性。這樣就可以讓Action調用指定方法,而不是execute()方法來處理用戶請求。
例如可以將ProductAction類定義成兩個邏輯Action,即將該類中的edit()和del()方法映射成不同的Action,示例代碼如下:
1
2
3
4
5
6
|
< action name = "editproduct" class = "com.qst.chapter03.action.ProductAction" method = "edit" > < result name = "edit" >/edit.jsp</ result > </ action > < action name = "delproduct" class = "com.qst.chapter03.action.ProductAction" method = "del" > < result name = "del" >/del.jsp</ result > </ action > |
上述代碼定義了editproduct和delproduct兩個邏輯Action,這兩個Action對應的處理類都是ProductAction,但處理邏輯不同。分別對應的是edit()和del()方法。
上面的這種方式雖然能夠實現,但兩個定義絕大部分是相同的,帶來冗余問題。Struts 2還提供了通配符“ * ”來解決這個問題。利用通配符在定義Action的name屬性時使用模式字符串(即用“ * ”代表一個或多個任意字符串),接下來就可以在class、method屬性以及<result>子元素中使用{N}的形式代表前面第N個星號“ * ”所匹配的子串。
* 通配符
1
2
3
4
5
6
7
8
9
10
|
< struts > <!-- 演示通配符的使用方法 --> < package name = "product" extends = "struts-default" > < action name = " * product" class = "com.qst.chapter03.action.ProductAction" method = "{1}" > < result name = "edit" >/edit.jsp</ result > < result name = "del" >/del.jsp</ result > </ action > </ package > </ struts > |
上述代碼Action的name屬性值為“ * product”,使用了通配符,此時定義的不是一個普通的Action,而是定義了一系列的邏輯Action,只要用戶請求的URL符合“ * product.action”的模式,都可以通過ProductAction處理。此外,必須要指定method屬性,method屬性用于指定用戶請求的方法。在method屬性中使用表達式{1},代表該表達式就是name屬性值中第一個“ * ”指代的值。通過上述配置規則可以達到與動態調用同樣的運行效果。
此外Struts 2允許在class屬性和method屬性中同時使用表達式,例如:
<action name = " *_* " class = "com.qst.chapter03,action.{1}Action" method = " {2} ">
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.jianshu.com/p/502de2ddd323?utm_source=tuicool&utm_medium=referral