struts中動態(tài)方法調(diào)用使用通配符
一、DMI動態(tài)方法調(diào)用的其中一種改變form表單中action屬性的方式已經(jīng)講過了。還有兩種,一種是改變struts.xml配置文件中action標簽中的method屬性,來指定執(zhí)行不同的方法處理不同的業(yè)務邏輯;另外一種是使用通配符的方式。改變method屬性的方式需要配置多個action,而且這些action定義的絕大部分都是相同的,所以這種定義是相當冗余的。因此,使用通配符就可以在一個action標簽中代替多個邏輯處理的Action。
二、示范:(和之前的動態(tài)方法調(diào)用改變form表單action屬性差不多,在struts.xml配置文件上進行了小小的修改。)
要求還是沒有變,點擊不同的提交按鈕提交同一個表單,把不同的業(yè)務交給相同的Action處理類去處理。
?、笔紫蕊@示一個表單,表單中有兩個提交按鈕,但分別代表不同的業(yè)務。當點擊登錄時用戶登錄;當點擊注冊時用戶注冊。

⒉用戶登錄:

?、秤脩糇裕骸 ?br />

具體的代碼如下:
⑴、登錄注冊頁面(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(){
/*動態(tài)修改表單中的action屬性值,實現(xiàn)把注冊的請求提交給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="登錄">
<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方法并且調(diào)用。如果用戶點擊了注冊按鈕,那么也和點擊登錄按鈕是一樣流程了。具體可以自己寫一個小例子感受一下。
⑶、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中動態(tài)方法調(diào)用使用通配符,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Java中使用同步回調(diào)和異步回調(diào)的示例詳解
這篇文章主要介紹了Java中使用同步回調(diào)和異步回調(diào)的相關資料,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
SpringBoot 中大文件(分片上傳)斷點續(xù)傳與極速秒傳功能的實現(xiàn)
這篇文章主要介紹了SpringBoot 中大文件(分片上傳)斷點續(xù)傳與極速秒傳功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07

