歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts動態方法調用使用通配符

Struts動態方法調用使用通配符

日期:2017/3/1 9:12:14   编辑:Linux編程

一、DMI動態方法調用的其中一種改變form表單中action屬性的方式已經講過了。還有兩種,一種是改變struts.xml配置文件中action標簽中的method屬性,來指定執行不同的方法處理不同的業務邏輯;另外一種是使用通配符的方式。改變method屬性的方式需要配置多個action,而且這些action定義的絕大部分都是相同的,所以這種定義是相當冗余的。因此,使用通配符就可以在一個action標簽中代替多個邏輯處理的Action。

二、示范:(和之前的動態方法調用改變form表單action屬性差不多,在struts.xml配置文件上進行了小小的修改。)

要求還是沒有變,點擊不同的提交按鈕提交同一個表單,把不同的業務交給相同的Action處理類去處理。

⒈首先顯示一個表單,表單中有兩個提交按鈕,但分別代表不同的業務。當點擊登錄時用戶登錄;當點擊注冊時用戶注冊。

⒉用戶登錄:

⒊用戶注冊:

具體的代碼如下:

⑴、登錄注冊頁面(index.jsp):

<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="js/jquery-1.7.2.js"></script>
<title>index</title>
<script type="text/javascript">
$(function(){
$("input:eq(3)").click(function(){
/*動態修改表單中的action屬性值,實現把注冊的請求提交給Action類 */
$("#form").attr("action","Create");
});

});

</script>

</head>
<body>
<form action="Login" method="post" id="form">
姓名:<input type="text" name="name" /><br><br>
密碼:<input type="password" name="password" /><br><br>
<input type="submit" value="登錄">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" value="注冊">
</form>
</body>
</html>

⑵、struts.xml配置文件的代碼

<?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>
<package name="myP" extends="struts-default">

<action name="*" class="action.Action" method="{1}">
<result name="userLogin">WEB-INF/jsp/userLogin.jsp</result>
<result name="userCreate">WEB-INF/jsp/userCreate.jsp</result>
</action>

</package>

</struts>

解析:

 1、在這份配置文件中,對action標簽中的name屬性配置了一個通配符: "*",後面的method屬性值則為: {1}。

 2、意思就是當用戶在index.jsp頁面點擊了登錄按鈕,那麼就會把表單:中的action="Login"請求傳給struts,因為在struts.xml中進行了通配符的配置,所以就把 "*"當成是"Login",也就是name="Login"。而後面的method值為:{1}代表的是第一個"*",也就是metho="Login"。所以struts會去action.Action類中尋找到Login方法並且調用。如果用戶點擊了注冊按鈕,那麼也和點擊登錄按鈕是一樣流程了。具體可以自己寫一個小例子感受一下。

⑶、Action類的代碼:

package action;

import com.opensymphony.xwork2.ActionSupport;

public class Action extends ActionSupport {



private String name;public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}public String Login(){

System.out.println("用戶登錄");
return "userLogin";
}

public String Create(){

System.out.println("用戶注冊");
return "userCreate";
}

}

--------------------------------------------------------------------------------

當然,通配符的使用不僅僅是只有這麼簡單的一個,還可能是有: "*-*"、"Book_*" 等,這些通配符可以在struts.xml配置文件中action標簽的屬性中使用,如mthod、class屬性,也可以在result標簽中使用,如下:

<!--定義一個通用的action標簽-->
<action name="*">
<!--使用表達式定義Result標簽-->
<result>/WEB-INF/jsp/{1}.jsp</result>
</action>

在上面的action定義中,action的name是一個*,那麼就是可以匹配任意的Action,所有的請求都通過這個action來處理,因為這個action沒有class屬性,所以使用ActionSupport類來處理,因為沒有method屬性,所以默認是execute方法並且返回success字符串,而且在result標簽中name屬性默認是success,所以Action總是直接返回result中指定的jsp資源,因此上面的action定義的含義是:如果用戶請求a.action那麼就跳轉到a.jsp;如果請求b.action就跳轉到b.jsp。

推薦閱讀:

Struts中異步傳送XML和JSON類型的數據 http://www.linuxidc.com/Linux/2013-08/88247.htm

Struts2的入門實例 http://www.linuxidc.com/Linux/2013-05/84618.htm

Struts2學習筆記-Value Stack(值棧)和OGNL表達式 http://www.linuxidc.com/Linux/2015-07/120529.htm

struts2文件上傳(保存為BLOB格式) http://www.linuxidc.com/Linux/2014-06/102905.htm

Struts2的入門實例 http://www.linuxidc.com/Linux/2013-05/84618.htm

Struts2實現ModelDriven接口 http://www.linuxidc.com/Linux/2014-04/99466.htm

遇到的Struts2文件下載亂碼問題 http://www.linuxidc.com/Linux/2014-03/98990.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

Struts2 注解模式的幾個知識點 http://www.linuxidc.com/Linux/2013-06/85830.htm

Struts 的詳細介紹:請點這裡
Struts 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved