Java前端控制器 [英] Java Front Controller

查看:666
本文介绍了Java前端控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑在我的J2EE应用程序中实现Front Controller。你能用几个链接(带源代码示例)和&任何标准要遵循?

I'm thinking of implementing Front Controller in my J2EE application. Could you please suggest the same with few links (with source code examples) & any standards to follow?

祝你好运

推荐答案

首先,创建一个 Servlet ,它监听某个 url-pattern ,例如 /页/ * 。实现 service()方法以查找与请求方法关联的操作( GET POST 等)和pathinfo(servlet的 url-pattern 之后的URL部分)。

To start, create a Servlet which listens on a certain url-pattern, e.g. /pages/*. Implement the service() method to lookup the action associated with the request method (GET, POST, etc) and pathinfo (the URL part after the servlet's url-pattern).

基本示例:

protected void service(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException {
  View view = new View(request, response);
  Action action = ActionFactory.getAction(request);
  action.execute(view);
  view.navigate();
}

行动界面应该代表一个工作单位。您可以实现它来执行必要的业务逻辑:

The Action interface should represent an unit of work. You can implement it to do the necessary business logic:

public interface Action {
  void execute(View view);
}

ActionFactory 应该维护实现 Action 的类,其类别为 Map< String,Action> ,其中 String key表示请求方法和pathinfo的组合中的更少或更多。然后你可以得到 Action ,如下所示:

The ActionFactory should maintain the classes implementing Action in sort of Map<String, Action> where the String key represents less or more a combination of the request method and pathinfo. You could then get an Action as follows:

public static Action getAction(HttpServletRequest request) {
  return actions.get(request.getMethod() + request.getPathInfo());
}

查看应该表示 Action 可以使用的请求范围上下文。在 navigate()中,您可以将请求转发给JSP进行显示:

The View should represent the request scoped context which the Action can work with. In the navigate() you could forward the request to a JSP for display:

public void navigate() {
  String path = "/WEB-INF" + request.getPathInfo() + ".jsp";
  request.getRequestDispatcher(path).forward(request, response);
}

这应该让你开始(注意我留下了所有明显的检查,例如null为了让这些例子不那么混乱,现在由你自己决定)。

That should get you started (note that I left all obvious checks such as null pointers away to make the examples less cluttered, that's up to you now).

然而,在整个故事中还有更多需要考虑的因素,例如验证,转换,事件处理,输入值映射,本地化,依赖注入等等。这一切都是相当重要的。更好的MVC框架将大部分内容都考虑在内,例如 Sun JSF Apache Struts Spring MVC 条纹,等等。如果你从来没有做过任何一个,那么我强烈建议你在养成一个之前这样做,否则你最终会浪费时间。

There is however more to take account with in the whole story, such as validation, conversion, event handling, input value mappings, localization, dependency injection, etcetera. That's all with all quite a work. The more decent MVC frameworks takes most of this all into account, such as Sun JSF, Apache Struts, Spring MVC, Stripes, etcetera. If you have never done any of them, then I strongly recommend to do so before homegrowing one, otherwise you would end up with a waste of time.

这篇关于Java前端控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆