【问题标题】:Java servlet - implementing command patternJava servlet - 实现命令模式
【发布时间】:2014-04-19 08:38:24
【问题描述】:

我正在尝试了解如何在我的 java servlet 中实现“命令设计”。以下是 servlet 的示例:

protected void processRequest(HttpServletRequest ...) {

    String action = request.getParameter("action"); 
    String destination = null;

    } if (action.equals("Home")) 
        destination = "Home";
    } else if (action.equals("Add")) {
         String var = request...

         try {
            db.add(var);
         } (DatabaseException e) {
            request.setAttribute("errorMessage", e.getMessage());
         }
         ...
    }
 }

我的理解是,每个 if 语句都应该转换为一个单独的类,并带有一个执行函数,例如处理对象的添加。我不明白请求是如何处理的,它们是否在执行函数中处理并从那里返回到 Servlet/JSP(我捕获的错误被放入请求中并在我的 JSP 文件中使用)?

我希望有人能给我一个基本结构并告诉我如何开始实施它。

【问题讨论】:

    标签: java jsp servlets request command-pattern


    【解决方案1】:

    如何在我的 java servlet 中实现“命令设计”。

    什么是命令模式?

    在面向对象的编程中,命令模式是一种行为设计模式,其中对象用于表示和封装以后调用方法所需的所有信息。此信息包括方法名称、拥有该方法的对象以及方法参数的值。

    来源:wekipedia

    要在您的案例 servlet 的 HttpServletRequest 操作参数是 home 和 add 中实现 Command Design Pattern,我正在考虑两个类似 删除、更新等

    你说,

    我的理解是每个 if 语句都应该转换为 具有执行函数的单独类,例如处理 添加对象。

    好的,创建一个interface,执行方法返回类型为String,即视图名称。喜欢:

    public interface Action {
       public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception
    }
    

    为所有动作创建具体的动作类:

    public class HomeAction implements Action {
        public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
             //perform your home related action here..
             return "home";
        }
    }
    

    然后你说,

    我捕获的错误被放入请求中并在我的 JSP 文件中使用。

    所以实现添加动作类,如:

    public class AddAction implements Action {
        public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
    
             //get request parameters here..
    
             try {
                   //perform add action which throws exception..
                   return "success";
             }catch(DatabaseException e) {
                   request.setAttribute("errorMessage", e.getMessage());// to show exception msg in jsp.
                  //log exception to console or file here..
                  return "failure";
             }
        }
    }
    

    更新和删除的相同实现,然后在您的 Servlet 中将所有操作保留在地图中,例如:

    //Declare one map in your servlet to hold all concrete action objects.
    private Map<String,Action> actionMap = new HashMap<String,Action>();
    

    将所有动作对象放入构造函数中的actionMap或Servlet#init(ServletConfig config)中,例如:

    actionMap.put("home", new HomeAction());
    actionMap.put("add", new AddAction());
    actionMap.put("update", new UpdateAction());
    actionMap.put("delete", new DeleteAction());
    

    在Servlet#service(HttpServletRequest request, HttpServletResponse response) 中执行没有if 和else 条件的操作

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            String actionKey = request.getParameter("action");
            Action action = actionMap.get(actionKey);
            String view = action.execute(request, response);
    
            //Here, if view is if failure then, forward to jsp, to available request attributes in jsp. 
            //      if view is success redirect to jsp..
    
    }
    

    【讨论】:

    • 查看HomeAction和AddAction类的执行方法的返回类型。
    • 始终使用参数化类型,例如private Map&lt;String,Action&gt; actionMap = new HashMap&lt;String,Action&gt;();。避免使用原始类型。
    • 太棒了,正是我想要的!
    • 在 Servlets 中命令模式的一个更漂亮的实现是 Frontman,看看bibeault.org/frontman
    • @Nestor Hernandez Loli,你有工作链接吗,因为它坏了
    【解决方案2】:

    您可以创建任意多个命令的视图。例如home.jsp、add.jsp等

    创建一个控制器 servlet,其唯一目的是检查命令并根据命令分派更正后的视图。

    基本上我说的是一个非常著名的MVC [Model-View-Controller] 模式。

    欲了解更多信息,请查看Model–view–controller。

    【讨论】:

      猜你喜欢
      • 2011-01-13
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      相关资源
      最近更新 更多