struts 中继承ActionSupport类

来源:互联网 发布:go和java 编辑:程序博客网 时间:2024/04/29 16:29

理论上Struts 2.0的Action无须实现任何接口或继承任何类型,但是,我们为了方便实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重载(Override)此类里的String execute()方法。

由JavaDoc可知,ActionSupport类实现了接口。

com.opensymphony.xwork2.Action。

com.opensymphony.xwork2.LoaleProvider。

com.opensymphony.xwork2.TextProvider。

com.opensymphony.xwork2.Validateable。

com.opensymphony.xwork2.ValidationAware。

com.uwyn.rife.continuations.ContinuableObject。

java.io.Searializable。

java.lang.Cloneable。

例如下例所示:

public class HelloWorld extends ActionSupport {
private String message;

 public String getMessage()  {       return message;  }    public String execute()  {      message = " Hello World, Now is " +       DateFormat.getInstance().format( new Date());       return SUCCESS;  }   

}
然后可以配置如下映射:



/HelloWorld.jsp


在默认情况下,当请求HelloWorld.do发生时,Struts 2会根据struts.xml里的Action映射集(Mapping)实例化tutoiral.HelloWorld类,并调用其execute()方法。当然,我们可以通过以下两种方法改变这种默认调用,这个功能(Feature)有点类似Struts 1中的LookupDispathAction。

在sturts.xml中新建Action,并指明其调用的方法。

访问Action时,在Action名后加上”!xxx”(xxx为方法名)。

例如我们可以在tutorial/HelloWorld.java中加入以下方法:

public String aliasAction() {
message =”自定义Action调用方法”;
return SUCCESS;
}
然后可以在sturts.xml中指定method来设置请求的函数名:

0 0