首頁 > 娛樂

Struts2框架(三):一個Action內包含多個請求處理方法的處理

由 予象科技 發表于 娛樂2023-01-17

簡介獲取ActionContext例項,透過它來訪問Servlet APIActionContext context = ActionContext

struts2是什麼

Struts2框架(三):一個Action內包含多個請求處理方法的處理

一個Action內包含多個請求處理方法的處理

Struts1提供了DispatchAction,從而允許一個Action內包含多個請求處理方法。Struts2也提供了類似的功能。處理方式主要有以下三種方式:

一、 動態方法呼叫:

DMI:Dynamic Method Invocation 動態方法呼叫。

動態方法呼叫是指:

表單元素的action不直接等於某個Action的名字,而是以如下形式來指定對應的動作名:

則使用者的請求將提交到名為”userOpt”的Action例項,Action例項將呼叫名為”login”方法來處理請求。同時login方法的簽名也是跟execute()一樣,即為public String login() throws Exception。

注意:要使用動態方法呼叫,必須設定Struts2允許動態方法呼叫,透過設定struts.enable.DynamicMethodInvocation常量來完成,該常量屬性的預設值是true。

1. 示例:

修改使用者登入驗證示例,多增加一個註冊使用者功能。

1。 修改Action類:

package

org。qiujy。web。struts2。action;

import

com。opensymphony。xwork2。ActionContext;

import

com。opensymphony。xwork2。ActionSupport;

/**

*

@author

qiujy

*

@version

1。0

*/

public

class

LoginAction

extends

ActionSupport{

private

String userName;

private

String password;

private

String msg; //結果資訊屬性

/**

*

@return

the msg

*/

public

String getMsg() {

return

msg;

}

/**

*

@param

msg the msg to set

*/

public

void

setMsg(String msg) {

this

。msg = msg;

}

/**

*

@return

theu serName

*/

public

String getUserName() {

return

userName;

}

/**

*

@param

userName the userName

public

void

setUserName(String userName) {

this

。userName = userName;

}

/**

*

@return

the password

*/

public

String getPassword() {

return

password;

}

/**

*

@param

password the password to set

*/

public

void

setPassword(String password) {

this

。password = password;

}

/**

*處理使用者請求的login()方法

*

@return

結果導航字串

*

@throws

Exception

*/

public

String login()

throws

Exception{

if

(“test”。equals(

this

。userName) && “test”。equals(

this

。password)){

msg = “登入成功,歡迎” +

this

。userName;

//獲取ActionContext例項,透過它來訪問Servlet API

ActionContext context = ActionContext。

getContext

();

//看session中是否已經存放了使用者名稱,如果存放了:說明已經登入了;

//否則說明是第一次登入成功

if

null

!= context。getSession()。get(“uName”)){

msg =

this

。userName + “:你已經登入過了!!!”;

}

else

{

context。getSession()。put(“uName”,

this

。userName);

}

return

this

SUCCESS

}

else

{

msg = “登入失敗,使用者名稱或密碼錯”;

return

this

ERROR

}

}

public

String regist()

throws

Exception{

//將使用者名稱,密碼新增到資料庫中

//。。。

msg = “註冊成功。”;

return

this

SUCCESS

}

}

2。 struts。xml檔案:沒有什麼變化,跟以前一樣配置。

<!DOCTYPE struts PUBLIC

“-//Apache Software Foundation//DTD Struts Configuration 2。0//EN”

“http://struts。apache。org/dtds/struts-2。0。dtd”>

<!—— 定義處理請求URL為login。action的Action ——>

<!—— 定義處理結果字串和資源之間的對映關係 ——>

/success。jsp

/error。jsp

3。 頁面:

index。jsp

<%@ page language=“java” pageEncoding=“UTF-8”%>

使用者登入頁面

使用者入口


<

form

action

=

"manage/userOpt!login.action"

method

=

"post"

>

使用者名稱:
密碼:

regist。jsp

<%@ page language=“java” pageEncoding=“UTF-8”%>

使用者註冊頁面

使用者註冊


使用者名稱:
密碼:

4。 執行結果:

二、 為Action配置method屬性:

將Action類中的每一個處理方法都定義成一個邏輯Action方法。

<!DOCTYPE struts PUBLIC

“-//Apache Software Foundation//DTD Struts Configuration 2。0//EN”

“http://struts。apache。org/dtds/struts-2。0。dtd”>

/success。jsp

/error。jsp

/success。jsp

/error。jsp

如上,把LoginAction中的login和regist方法都配置成邏輯Action。要呼叫login方法,則相應的把index。jsp中表單元素的action設定為“manage/userLogin。action”;要呼叫regist方法,把regist。jsp中表單元素的action設定為“manage/userRegist。action”。

三、 使用萬用字元對映(wildcard mappings)方式:

在struts。xml檔案中配置元素時,它的name、class、method屬性都可支援萬用字元,這種萬用字元的方式是另一種形式的動態方法呼叫。

當我們使用萬用字元定義Action的name屬性時,相當於用一個元素action定義了多個邏輯Action:

class=“org。qiujy。web。struts2。action。UserAction” method=“{1}”>

/success。jsp

/error。jsp

如上,定義一系列請求URL是user_*。action模式的邏輯Action。

同時method屬性值為一個表示式{1},表示它的值是name屬性值中第一個*的值。

例如:使用者請求URL為user_login。action時,將呼叫到UserAction類的login方法;使用者請求URL為user_regist。action時,將呼叫到UserAction類的regist方法。

Tags:actionMSGjsplogin