您的位置:首页 > 编程语言 > Java开发

  尚硅谷公开课--struts2--5--ActionSuport

2015-08-11 00:00 323 查看
ActionSuport,默认的Action类。在配置struts2.xml中的action结点时,如果不填写action的class属性,则其默认值为ActionSuport。可以在struts2的默认配置文件中查看该值,位置在文件中struts-default包配置的最后,也是此默认配置文件的最后:

<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函数

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接口外,还实现了其它几个接口,实现了字段验证,国际化等功能。限于能力,现只作记录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: