`

表示层模式:View Helper—视图助手模式

阅读更多

Context
The system creates presentation content, which requires processing of dynamic
business data.
Problem
Presentation tier changes occur often and are difficult to develop and maintain when
business data access logic and presentation formatting logic are interwoven. This makes the
system less flexible, less reusable, and generally less resilient to change.
Intermingling the business and systems logic with the view processing reduces
modularity and also provides a poor separation of roles among Web production and
software development teams.
Forces
Business data assimilation requirements are nontrivial.
Embedding business logic in the view promotes a copy-and-paste type of reuse. This
causes maintenance problems and bugs because a piece of logic is reused in the same or
different view by simply duplicating it in the new location.
It is desirable to promote a clean separation of labor by having different individuals
fulfill the roles of software developer and Web production team member.
One view is commonly used to respond to a particular business request.
Solution
A view contains formatting code, delegating its processing responsibilities to its
helper classes, implemented as JavaBeans or custom tags. Helpers also store the view's
intermediate data model and serve as business data adapters.
There are multiple strategies for implementing the view component. The JSP View
Strategy suggests using a JSP as the view component. This is the preferred strategy, and it is
the one most commonly used. The other principal strategy is the Servlet View Strategy,
which utilizes a servlet as the view (see the section "Strategies" for more information).
Encapsulating business logic in a helper instead of a view makes our application
more modular and facilitates component reuse. Multiple clients, such as controllers and
views, may leverage the same helper to retrieve and adapt similar model state for
presentation in multiple ways. The only way to reuse logic embedded in a view is by
copying and pasting it elsewhere. Furthermore, copy-and-paste duplication makes a system
harder to maintain, since the same bug potentially needs to be corrected in multiple places.
A signal that one may need to apply this pattern to existing code is when scriptlet
code dominates the JSP view. The overriding goal when applying this pattern, then, is the
partitioning of business logic outside of the view. While some logic is best encapsulated
within helper objects, other logic is better placed in a centralized component that sits in
front of the views and the helpers-this might include logic that is common across multiple
requests, such as authentication checks or logging services, for example. Refer to the
"Intercepting Filter" on page 4 and "Front Controller" on page 21 for more information on
these issues.
If a separate controller is not employed in the architecture, or is not used to handle all
requests, then the view component becomes the initial contact point for handling some
requests. For certain requests, particularly those involving minimal processing, this scenario
works fine. Typically, this situation occurs for pages that are based on static information,
such as the first of a set of pages that will be served to a user to gather some information
(see "Dispatcher View" on page 232). Additionally, this scenario occurs in some cases when
a mechanism is employed to create composite pages (see "Composite View" on page 203).
The View Helper pattern focuses on recommending ways to partition your application
responsibilities. For related discussions about issues dealing with directing client requests
directly to a view, please refer to the section "Dispatcher View" on page 232.

分享到:
评论

相关推荐

    J2EE核心模式

    9. **视图助手(View Helper)模式**:为JSP页面提供辅助对象,以保持视图与模型的分离,减少JSP页面中的Java代码。 10. **模型-视图-控制器(Model-View-Controller, MVC)**:一种经典的架构模式,将业务逻辑、...

    J2EE Patterns

    - **视图助手**(View Helper):辅助视图呈现数据。 - **复合视图**(Composite View):组合多个视图组件。 - **服务到工作者**(Service To Worker):将任务分配给后台线程处理。 - **调度视图**(Dispatcher ...

    三层架构+MVC+Linq to SQL 入门实例.rar

    首先,三层架构(Three-Tier Architecture)是一种软件设计模式,将应用程序分为三个独立的层次:表示层(Presentation Layer)、业务逻辑层(Business Logic Layer)和数据访问层(Data Access Layer)。...

    ASP.NET MVC3技术要点

    - **Models**: 存储实体模型类和视图模型类文件,用于表示业务逻辑中的数据结构。 - **Scripts**: 存放JavaScript脚本文件,如jQuery等前端库。 - **Views**: 主要存放视图文件,根据控制器名称来组织其子目录。 ...

    MvcMovie例子ASP.Net+MVC3.0

    2. **MVC模式**:MVC模式将应用分为三个主要部分:模型(Model)负责处理数据和业务逻辑,视图(View)负责展示数据,控制器(Controller)处理用户输入并协调模型和视图。 3. **路由**:ASP.NET MVC中的路由系统是...

    J2EE核心模式三

    ViewHelper(视图协助器)ViewHelper模式要求把业务处理逻辑从视图(View)里排除出去,也就是说,视图里只包含显示用代码,其余的代码则应该放在做为HelperClass(协助器)的业务逻辑层里。ViewHelper(视图协助器...

    ASP.MVC3.0中文版教程.doc

    - **MVC**(Model-View-Controller)模式是软件工程中常用的一种设计模式,将应用程序分为三个核心组件: - **模型**(Model):负责存储数据和业务逻辑。 - **视图**(View):展示数据供用户查看。 - **控制器*...

    CodeIgniter实现更改view文件夹路径的方法

    5. **CI映射(加载)数据到view层的方法**:通常使用`$this->load->vars()`来预先设定全局变量,这些变量在所有后续加载的视图中都可用。 6. **CI框架中类的自动加载问题分析**:CodeIgniter的自动加载机制可以通过...

    MvcMusicStore-Assets

    6. **视图模型(View Models)**:为了更好地分离视图和模型,开发者经常使用视图模型,这是专为视图设计的简单数据类。它们将控制器需要传递给视图的数据进行封装,使视图更专注于呈现。 7. **布局(Layouts)**:...

    asp.net MVC初学者源码

    1. **MVC模式**:模型(Model)负责业务逻辑和数据管理,视图(View)用于显示用户界面,控制器(Controller)处理用户请求并协调模型和视图。这种分离使得代码更易于理解和维护。 2. **路由**:ASP.NET MVC使用...

    mvc2 demo 源码 c#

    MVC(Model-View-Controller)是一种广泛应用的软件设计模式,特别适用于构建可维护、可扩展的Web应用程序。MVC2是微软在ASP.NET框架下对这一模式的一个实现,它为开发者提供了更强大的控制和更高的灵活性。本文将...

    Mvc简单应用

    模型层不关心视图或控制器的细节,只关注数据的处理。 2. **View(视图)**:是用户看到和与之交互的界面。视图根据模型的数据来呈现内容,通常由HTML、CSS和JavaScript组成。在Mvc中,视图不包含任何业务逻辑,仅...

    J2EE应用实例

    - **表示层(Presentation Layer)**: 使用模板机制、页面定义、请求映射和自定义标记库来处理用户界面。页面流程管理确保了用户交互的顺畅。 - **模型层(Model Layer)**: 包括Question Type Catalog模型、...

    asp.net mvc4源码

    ASP.NET MVC4是一种基于模型-视图-控制器(Model-View-Controller)设计模式的开源Web应用程序框架,由微软开发,用于构建动态、数据驱动的Web应用。在本例中,"asp.net mvc4源码"是一个关于酒吧管理系统的项目,...

    asp.net mvc handler

    6. **辅助方法(Helper Methods)**: 助手方法提供了一种在视图中方便地生成HTML的方法,比如Html.ActionLink()用于创建链接,Html.DropDownList()用于创建下拉列表等。 7. ** Areas(区域)**: 如果一个项目变得...

    ASP.NET MVC 1.0 Quickly

    - **分层架构**:推荐使用多层架构来组织代码,例如将业务逻辑与表示层分离,提高系统的灵活性和可维护性。 #### 三、开发实践 - **模型**:模型是应用程序的核心部分,负责处理数据和业务逻辑。在ASP.NET MVC中,...

    mvc4初级教程

    这个框架结合了Model-View-Controller(模型-视图-控制器)设计模式,提供了高度的灵活性和可测试性。在本教程中,我们将深入学习如何使用ASP.NET MVC4来创建一个基础的Guestbook应用,以此来理解其核心概念和工作...

    MVCDemo asp.net MVC

    4. **视图(View)**:使用Razor语法编写视图,展示Model中的数据。理解@符号的作用,以及如何使用辅助方法如`HtmlHelper`和`UrlHelper`。 5. **模型(Model)**:如何创建实体类来表示业务对象,以及如何使用数据注解...

Global site tag (gtag.js) - Google Analytics