尚硅谷公開課--struts2--5--ActionSuport

ActionSuport,默認的Action類。在配置struts2.xml中的action結點時,若是不填寫action的class屬性,則其默認值爲ActionSuport。能夠在struts2的默認配置文件中查看該值,位置在文件中struts-default包配置的最後,也是此默認配置文件的最後: html


<package name="struts-default" abstract="true">
...
...
   <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
</package>
在struts2.xml中添加一個action,內容以下:



<action name="TestActionSuportAction">
	<result>/test-ActionSuportAction.jsp</result>
</action>
添加一個jsp頁面:test-ActionSuportAction.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">
<title>Insert title here</title>
</head>
<body>
	<h4>Test ActionSuport</h4>
</body>
</html>
在index.jsp中添加一個連接



<a href="TestActionSuportAction.do">Test ActionSuport</a>
運行項目


能夠看到咱們沒有指定action的class屬性,可是頁面一樣能夠正常跳轉,打開com.opensymphony.xwork2.ActionSupport的源碼,找到execute函數 java


public String execute() throws Exception {
        return SUCCESS;
    }
此函數返回了一個字符串,其值爲 success ,在action中,雖然沒有配置result的name屬性,但其默認屬性爲success,因此能夠正常跳轉。也就是說上面的action等價於:



<action name="TestActionSuportAction" 
			class="com.opensymphony.xwork2.ActionSupport"
			method="execute">
			<result name="success">/test-ActionSuportAction.jsp</result>			
		</action>
ActionSuport類實現了Action接口,Action接口的內容以下:



package com.opensymphony.xwork2;


public interface Action {

    public static final String SUCCESS = "success";


    public static final String NONE = "none";


    public static final String ERROR = "error";


    public static final String INPUT = "input";


    public static final String LOGIN = "login";



    public String execute() throws Exception;

}
能夠看到SUCCESS常量和execute()方法均在此接口中聲明。


ActionSuport除了實現Action接口外,還實現了其它幾個接口,實現了字段驗證,國際化等功能。限於能力,現只做記錄。 jsp

相關文章
相關標籤/搜索