actionhelper.java

来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 54 行

JAVA
54
字号
package forum;

import java.util.HashMap;

/**
 * A helper class from which to look up and retrieve an instance
 * of an Action class to process a specific request.
 *
 * @author    Simon Brown
 */
public class ActionHelper {

  /** the collection of actions that we know about */
  private static HashMap actions = new HashMap();

  /**
   * Called on class loadup to populate the actions collection.
   */
  static {
    actions.put("ViewTopic", "forum.ViewTopicAction");
    actions.put("Login", "forum.LoginAction");
    actions.put("Logout", "forum.LogoutAction");
    actions.put("NewResponse", "forum.NewResponseAction");
    actions.put("ProcessNewResponse", "forum.ProcessNewResponseAction");
    actions.put("DeleteResponse", "forum.DeleteResponseAction");
  }

  /**
   * Given the name/type of request, this method returns tha Action
   * instance appropriate to process the request.
   *
   * @param name    the name (type) of the request
   * @return  an instance of Action (could be null if no mapping has been defined)
   */
  public static Action getAction(String name) {

    System.out.println("ActionHelper : Getting Action for " + name);

    Action action = null;

    try {
      // instantiate the appropriate class to handle the request
      Class c = Class.forName((String)actions.get(name));
      action = (Action)c.newInstance();
    } catch (Exception e) {
      e.printStackTrace();
    }

    System.out.println("ActionHelper : Found " + action.getClass().getName());

    return action;
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?