Action类的书写方式:
1、方式一:POJO方式(不用继承任何父类,也不用实现任何接口):
优点:使得Struts2框架的代码侵入性更低。
public class HelloAction {//一个action中有多个方法 public String add(){ System.out.println("添加"); return "success"; } public String delete(){ System.out.println("删除"); return "success"; } public String update(){ System.out.println("修改"); return "success"; } public String selete(){ System.out.println("查询"); return "success"; } }
2、方式二:实现Action接口:
(1)选择第一个类进行继承:
实现了execute(),提供了Action的方法规范。
import com.opensymphony.xwork2.Action; public class HelloAction implements Action { @Override public String execute() throws Exception { return null; } }
(2)Action接口提供了一些字符串,可以在返回结果时使用:
public interface Action { String SUCCESS = "success"; String NONE = "none"; String ERROR = "error"; String INPUT = "input"; String LOGIN = "login"; String execute() throws Exception; }
3、方式三:继承ActionSupport类:
查看ActionSupport类的源码可知,ActionSupport实现了一系列接口:
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable { protected static Logger LOG = LoggerFactory.getLogger(ActionSupport.class); private final ValidationAwareSupport validationAware = new ValidationAwareSupport(); private transient TextProvider textProvider; private Container container;