J2EE Design Patterns 2 Front Controller(前端控制器)

表示层请求的统一入口,content retrieval, view management, and navigation。

 

Problem

 

  • Each view is required to provide its own system services, often resulting in duplicate code.

  • View navigation is left to the views. This may result in commingled view content and view navigation.
  • Additionally, distributed control is more difficult to maintain, since changes will often need to be made in numerous places.

 

 

Solution

Use a controller as the initial point of contact for handling a request. The controller manages the handling of the request, including invoking security services such as authentication and authorization, delegating business processing, managing the choice of an appropriate view, handling errors, and managing the selection of content creation strategies.


 

Typically, a controller coordinates with a dispatcher component. Dispatchers are responsible for view management and navigation. Thus, a dispatcher chooses the next view for the user and vectors control to the resource. Dispatchers may be encapsulated within the controller directly or can be extracted into a separate component.

 

An application may use multiple controllers in a system, each mapping to a set of distinct services.

 

 

Controller

The controller is the initial contact point for handling all requests in the system. The controller may delegate to a helper to complete authentication and authorization of a user or to initiate contact retrieval.

Dispatcher

A dispatcher is responsible for view management and navigation, managing the choice of the next view to present to the user, and providing the mechanism for vectoring control to this resource.

A dispatcher can be encapsulated within a controller or can be a separate component working in coordination. The dispatcher provides either a static dispatching to the view or a more sophisticated dynamic dispatching mechanism.

The dispatcher uses the RequestDispatcher object (supported in the servlet specification) and encapsulates some additional processing.

Helper

A helper is responsible for helping a view or controller complete its processing. Thus, helpers have numerous responsibilities, including gathering data required by the view and storing this intermediate model, in which case the helper is sometimes referred to as a value bean. Additionally, helpers may adapt this data model for use by the view. Helpers can service requests for data from the view by simply providing access to the raw data or by formatting the data as Web content.

A view may work with any number of helpers, which are typically implemented as JavaBeans components (JSP 1.0+) and custom tags (JSP 1.1+). Additionally, a helper may represent a Command object, a delegate (see "Business Delegate" on page 248), or an XSL Transformer, which is used in combination with a stylesheet to adapt and convert the model into the appropriate form.

View

A view represents and displays information to the client. The view retrieves information from a model. Helpers support views by encapsulating and adapting the underlying data model for use in the display.

 

Finally, because the command processing is not coupled to the command invocation, the command processing mechanism may be reused with various types of clients, not just with Web browsers. 

Controller拿到request,交给相应的command,处理完之后给Dispatcher分发。

 

 

Multiplexed Resource Mapping Strategy

This is actually a substrategy of Logical Resource Naming Strategy. This strategy maps not just a single logical name, but an entire set of logical names, to a single physical resource. For example, a wildcard mapping might map all requests that end with .ctrl to a specific handler.

A request and mapping might look as shown in Table 7-1.

Table 7-1

Request

Mapping

http://some.server.com/action.ctrl

*.ctrl = servletController

 

 

Consequences

  • Centralizes Control
    A controller provides a central place to handle system services and business logic across multiple requests. A controller manages business logic processing and request handling. Centralized access to an application means that requests are easily tracked and logged. Keep in mind, though, that as control centralizes, it is possible to introduce a single point of failure. In practice, this rarely is a problem, though, since multiple controllers typically exist, either within a single server or in a cluster.

  • Improves Manageability of Security
    A controller centralizes control, providing a choke point for illicit access attempts into the Web application. In addition, auditing a single entrance into the application requires fewer resources than distributing security checks across all pages.
  • Improves Reusability
    A controller promotes cleaner application partitioning and encourages reuse, as code that is common among components moves into a controller or is managed by a controller.

Related Patterns

  • View Helper 
    The Front Controller pattern, in conjunction with the View Helper pattern, describes factoring business logic out of the view and providing a central point of control and dispatch. Flow logic is factored forward into the controller and data handling code moves back into the helpers.

  • Intercepting Filter
    Both Intercepting Filter and Front Controller describe ways to centralize control of certain types of request processing, suggesting different approaches to this issue.
  • Dispatcher View and Service to Worker 
    The Dispatcher View and Service to Worker patterns are another way to name the combination of the View Helper pattern with a dispatcher, and Front Controller pattern. Dispatcher View and Service to Worker, while structurally the same, describe different divisions of labor among components.

 

经过过滤器处理后的请求交给Front Controller,来继续进行一些请求的必要处理。

Controller根据请求的参数将请求发给Helper或command。经过处理后再有Dispatcher分发给不同的View。

好处是分层清晰明确,易于维护。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中文译名:J2EE设计模式<br/>作者:<br/>(美)William Crawford & Jonathan Kaplan <br/><br/>本书一边在持久化和消息通信等领域中描述一些新模式,一边向你说明如何在J2EE应用的上下文中实现许多模式。有助于考虑问题的优雅模式以及实际工作的企业开发人员的智慧结晶,在本书中被有机地结合到了一起。<br/><br/>内容简介<br/>已经有了那么多关于设计模式的书,为什么我们还需要另一本呢?<br/>设计模式是解决方案的构建模块:对一次又一次重现的问题求解技巧的抽象。列举和分类设计模式有助于让你了解你一直在重新发明的“轮子”。<br/>然而,一旦你列出抽象的模式,你就得回到现实世界并且在上下文中看那些模式。像资源池这样的模式是很有趣的,但是除非你已经实现了一个,否则仅仅知道它是一个“设计模式”对你并没有什么帮助。<br/>那就是奉书与众不同的地方。本书一边在持久化和消息通信等领域中描述一些新模式,一边向你说明如何在J2EE应用的上下文中实现许多模式。有助于考虑问题的优雅模式以及实际工作的企业开发人员的智慧结晶,在本书中被有机地结合到了一起。<br/>本书涵盖了如下的主题:<br/>· 表达层模式<br/>· 业务层模式<br/>· 业务层和表达层之间的交互<br/>· 持久化模式<br/>· 并发性模式<br/>· 企业消息通信模式<br/>· 反模式,或者本身已经变成为模式的错误<br/>在本书中提出的许多模式关注于可扩展性、可伸缩性、灵活性和性能——任何J2EE开发人员的主要关心点。有些模式对你来说将会是新的;有些则会是很熟悉的。但是不管你是否曾经见过这些模式,本书在向你说明如何把它们应用到实际的J2EE应用方面是独一无二的。如果你开发J2EE应用的话,那么本书就属于你的书架——并且应该就在你的手中。 <br/><br/>http://www.china-pub.com/computers/common/info.asp?id=24948<br/><br/>书籍图片:<br/>http://www.china-pub.com/computers/ebook20001-25000/24948/zcover.gif<br/>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值