`
CoderDream
  • 浏览: 471291 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

【张冰Struts2学习笔记】0201_Action接口与ActionSupport类

阅读更多
 

Action 接口与ActionSupport

1.      Action 接口

所在包名:

com.opensymphony.xwork2

描述:

public interface Action

All actions may implement this interface, which exposes the execute() method. However, as of XWork 1.1, this is not required and is only here to assist users. You are free to create POJOs that honor the same contract defined by this interface without actually implementing the interface.

简单翻译:

所有的 Action 都可以实现这个接口,该接口暴露了一个 execute() 方法。

然而,在 XWork 1.1 中,这些(实现接口)不是必须的,它只是用来协助用户的。你可以自由的创建一个 POJO 来替代实现这个接口,只需要实现一个 execute() 方法,而不需要真正的实现这个接口。 

 

代码清单 1 Action.java

 

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;

}
 

我们可以看到,Action 接口非常简单,除了定义了5 个常量以外,还声明了一个方法。要实现这个接口,只需要实现这个方法即可。

代码清单 2 LoginAction.java

 

package com.coderdream.action;
 
import com.opensymphony.xwork2.Action;
 
public class LoginAction implements Action {
      private String username ;
      private String password ;
 
      public String getUsername() {
           return username ;
      }
 
      public void setUsername(String username) {
           this .username = username;
      }
 
      public String getPassword() {
           return password ;
      }
 
      public void setPassword(String password) {
           this .password = password;
      }
 
      public String execute() {
           if ("aaa" .equalsIgnoreCase(username .trim())
                      && "123" .equalsIgnoreCase(password .trim())) {
                 return SUCCESS ;
           }
           else {
                 return INPUT ;
           }
      }
 
} 
 

Action 中常量的简单释义:

 

  英文注释 中文释义
static String ERROR  The action execution was a failure. Show an error view, possibly asking the user to retry entering data. Action执行失败。显示一个错误的视图(页面),这个页面可以要求用户再次输入相关数据。
static String INPUT  The action execution require more input in order to succeed. This result is typically used if a form handling action has been executed so as to provide defaults for a form. The form associated with the handler should be shown to the end user.
This result is also used if the given input params are invalid, meaning the user should try providing input again. 
Action的执行成功需要更多的输入。这个结果是一个典型的结果,它表明如果一个表单通过提供默认的表单来操作一个Action。这个表单会显示给最终用户。
这个结果也用于用户输入无效,意味着用户需要再次输入。
static String LOGIN  The action could not execute, since the user most was not logged in. The login view should be shown.  Action不能执行,因为用户没有登录。已登录的画面会被关闭。
static String NONE  The action execution was successful but do not show a view. This is useful for actions that are handling the view in another fashion like redirect.  Action执行成功,但是不会显示一个视图。通常产生这种情况的原因是被其他视图重定向了。
static String SUCCESS  The action execution was successful. Show result view to the end user. Action执行成功。显示结果视图(页面)给用户。

 

struts.xmlresult 就可以直接使用Action 接口中的常量了:

代码清单 3 struts.xml

<?xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts >
      <package name = "loginTest" extends = "struts-default" >
           <action name = "login" class = "com.coderdream.action.LoginAction" >
                 <result name = "success" >/loginSuc.jsp
            </result >
                 <result name = "input" >/loginFail.jsp
            </result >
           </action >
      </package >
</struts > 

2.      ActionSupport

public class ActionSupport

extends Object

implements Action , Validateable , ValidationAware , TextProvider , LocaleProvider , Serializable

Provides a default implementation for the most common actions. 
See the documentation for all the interfaces this class implements for more detailed information.

为最常用的 Action 的提供一个默认的实现。 
  

下面是ActionSupport 类的代码片段,从中我们可以看到该类提供了execute() 方法的空实现,直接返回success

代码清单 4 ActionSupport.java

 

public class ActionSupport implements Action , Validateable , ValidationAware , TextProvider , LocaleProvider , Serializable {
 
    public String input() throws Exception {
        return INPUT ;
    }
   
    public String doDefault() throws Exception {
        return SUCCESS ;
    }
 
    public String execute() throws Exception {
        return SUCCESS ;
    }
 
    public void validate() {
    }
 
    public Object clone() throws CloneNotSupportedException {
        return super .clone();
    }
    public void pause(String result) {
    }
 
} 

 

LoginAction 继承了com.opensymphony.xwork2.ActionSupport 类。 LoginAction 覆写了父类(ActionSupport )中的execute() 方法,当然也可以直接使用该类的默认实现,该默认实现返回SUCCESS

代码清单 5 LoginAction.java

 

package com.coderdream.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class LoginAction extends ActionSupport {
 
      private static final long serialVersionUID = -8047473058927169093L;
      private String username ;
      private String password ;
 
      public String getUsername() {
           return username ;
      }
 
      public void setUsername(String username) {
           this .username = username;
      }
 
      public String getPassword() {
           return password ;
      }
 
      public void setPassword(String password) {
           this .password = password;
      }
 
      public String execute() {
           if ("aaa" .equalsIgnoreCase(username .trim())
                      && "123" .equalsIgnoreCase(password .trim())) {
                 return SUCCESS ;
           } else {
                 return INPUT ;
           }
      }
 
} 

3.      三种方式小结

1、 Login1Action POJO ,包含一个execute() 方法,返回值为String

代码清单 1 Login1Action.java

 

public String execute() {
      if ("aaa".equalsIgnoreCase(username.trim())
                 && "123".equalsIgnoreCase(password.trim())) {
           return "success";
      } else {
           return "failure";
      }
}
 

返回的多个值对应与struts.xml 中配置的返回结果集对应:

代码清单 2 struts.xml 片段

<action name="login1" class="com.coderdream.action.Login1Action">
      <result name="success">/login1Suc.jsp</result>
      <result name="failure">/login1Fail.jsp</result>
</action>

 

2 Login2Action 实现了接口com.opensymphony.xwork2.Action

       Login2Actionstruts.xml 中可直接使用Action 中定义的常量;

代码清单 3 Action.java

 

static String ERROR
      The action execution was a failure.
      返回错误
static String INPUT
      The action execution require more input in order to succeed.
      返回一个输入页面
static String LOGIN
      The action could not execute, since the user most was not logged in. 、
      返回登录页面
static String NONE
      The action execution was successful but do not show a view.
      返回空(例如在上传下载时)
static String SUCCESS
      The action execution was successful.
      返回成功页面
 

返回值为Action 中定义的静态常量:

代码清单 4 Login2Action.java

 

public String execute() {
      if ("aaa".equalsIgnoreCase(username.trim())
                 && "123".equalsIgnoreCase(password.trim())) {
           return SUCCESS;
      }
      else {
           return INPUT;
      }
}
 

struts.xml 中配置的返回结果集为静态常量对应的字符串:

代码清单 5 struts.xml 片段

<action name="login2" class="com.coderdream.action.Login2Action">
      <result name="success">/login2Suc.jsp</result>
      <result name="input">/login2Fail.jsp</result>
</action>
 

3 Login3Action 继承自

       com.opensymphony.xwork2.ActionSupport

       该类实现了接口com.opensymphony.xwork2.Action 接口;

       (该类另外还实现了Validateable , ValidationAware, TextProvider,

       LocaleProvider, Serializable 接口)

       Login3Action 的代码和struts.xml 中配置与Login2Action 一致;

   

4com.opensymphony.xwork2.ActionSupport 类有execute() 方法的实现,直接返回SUCCESS ,也就是说任何继承自ActionSupport 的类可以不覆写execute() 方法,永远返回成功。

 

4.      源代码

附件。

 

 

我的联系方式: 85337464

我的博客: http://coderdream.iteye.com

 

1
0
分享到:
评论
1 楼 orclight 2011-12-07  
学习了!

相关推荐

Global site tag (gtag.js) - Google Analytics