您的位置:首页 > 编程语言 > Java开发

struts中需要了解的一些概念

2015-03-17 17:24 281 查看
大家有没有想过,为什么我们要使用框架呢?

有的人可能说方便,那么它方便在哪里呢?

它们可以解决一些共性的问题;

它们可以为你节省不少时间;

它们可以提供你所需要到的解决方式;

1、什么是框架?

框架是软件结构化的一部分。框架尽量会把日常中的共性任务和特殊领域的工作流抽象化。框架主要在两个方面帮助我们开发:

第一、试图自动化所有常见的问题

第二、尽量提供一种优秀的解决方案来优化web应用中常见的工作流

2、什么是struts2 framework?

struts2 framework是基于webwork framework;

struts2 是实现MVC设计模式的第二代应用框架;

关注点:

struts2是如何来实现MVC的?

当处理请求的时候,框架的各个部分是如何紧密的工作的?

struts2的三个核心组件:

The role of the controller is played by the Struts 2 FilterDispatcher。

model is implemented by the Struts 2 action component。

首先我们来看一副图,它描述了struts是如何工作的:



1、什么是OGNL

它是一种允许我们访问数据的一种工具。更具体地说,OGNL是一种允许我们引用或控制ValueStack数据的强大表达式语句。

2、什么是ValueStack

在请求过程中,struts2会使用ValueStack来作为存储区域。在请求过程中数据将会移向ValueStack

3、什么是Interceptor

拦截器在struts2中是用来执行之前或之后的请求处理。他们提供了一种定义了各种各样的工作流和横截任务的组件,以至于他们能够很容易的

4、 MODEL-ACTION

In more technical terms, the model is the internal state of the application. --在更多的技术方面,模型是应用程序的内部状态。

This state is composed of both the data model and the business logic. --这种状态由数据模型和业务逻辑组成。

business logic and data from the database will be involved in the authentication process. --业务逻辑和数据库中存放的数据都会参与到身份验证过程。

Neither the data on its own, nor the business logic on its own, can produce these states. --仅有数据或者仅有业务逻辑都不能生成生成这种状态。

struts2扮演两种角色:

First, an action is an encapsulation of the calls to business logic into a single unit of work. --第一,一个动作调用业务逻辑进入一个单独的工作单元。

Second, the action serves as a locus of data transfer. --第二,动作是一个数据传输的场所。

5、VIEW-RESULT

how do you get all the richness you desire without actually “combining” the ingredients? --如何获得你想要的丰富功能,而不把所有的因素搅在一起呢?

it translates the state of the application into a visual presentation with which the user can interact.

(将应用的状态转换成一种用户可以与之交互的可视化表示)

it becomes even more important to have clean MVC separation of concerns.

(拥有整洁的MVC关注点分离变得更加重要)

One of the interesting aspects of Struts 2 is how well its clean architecture paves the way for new technologies and techniques.

(struts2的一个有趣特性是,整洁的架构对新技术铺平了道路)

This makes it easy for alternative responses, such as XML snippets or XSLT transformations, to be integrated into the framework.

(将其它可选的响应(例如XML代码段或XLST变换)集成进框架就会很容易)

Common choices are between results that represent the semantic outcomes of the action’s processing, such as “success” and “error.”

(多数的选择都是为了表达动作的处理结果过程,例如“成功或失败”)

Struts 2 provides out-of-the-box support for using most common view-layer technologies as results.

(为了使用大多通用的视图框架技术展现结果,Struts2 提供了原生的支持)

Better yet, the clean structure of the architecture ensures that more result types can be built to handle new types of responses.

(更好的是,struts2整洁的架构能确保构造更多的结果类型用来处理新的响应类型)

Since this favored MVC pattern has been around for decades, try visualizing what the MVC playing field would look like if the players were in fact nicely separated yet connectible.

(近10年MVC模式深受人们的喜爱,MVC模式中的成员既能很好的分离,又能很好的连接)

Grab some sweets and continue reading to learn about Struts 2 and the framework request-processing factory.

(现在收获了一些知识,继续阅读以及学习struts2请求处理工厂)

How Struts 2 works



These clean lines are only possible with the help of a few other key architectural components that participate in processing every request.

(这些清晰的线条依赖于其他几个参与处理每一个请求处理关键架构组件的帮助)

We’ll explain in the next paragraphs how the interceptors and the ActionContext aid the action and result in their processing of the request.

(我们将在下一个段落中解释拦截器和动作上下文如何在他们的请求处理过程中帮助这个动作和结果)

INTERCEPTORS

We’ll devote an entire chapter to this important component later in the book.

(我们将在本书的后面花费整个章节来介绍这个重要的组件)

At this time, it is enough to understand that most every action will have a stack of interceptors associated with it.

(与此同时,只需要理解每一个动作都有一组拦截器与之关联)

These interceptors are invoked both before and after the action, though we should note that they actually fire after the result has executed.

(这些拦截器在动作执行之前或之后都被调用,并且需要指出的是它们的结果执行完毕后才被触发)

Interceptors don’t necessarily have to do something both times they fire, but they do have the opportunity.

(拦截器没有必要在两次触发时都做工作,不过他们都有机会)

Some interceptors only do work before the action has been executed, and others only do work afterward.

(有时拦截器仅仅在动作执行前工作,而另外一些仅在动作执行之后工作)

The important thing is that the interceptor allows common, cross-cutting tasks to be defined in clean, reusable components that you can keep separate from your action code.

(最重要的是,拦截器允许常见的、横切的任务在简洁、可重用的组件中定义,从而能够和动作的代码隔离)

They provide an architectural component in which to define various workflow and cross-cutting tasks so that they can be easily reused as well as separated from other architectural concerns.

(他们提供一个架构组件用来定义各种各样的工作流和横切任务,以便能够简单的重用这些组件,并且能够与其它架构关注点分离)

Logging should be done with the invocation of every action, but it probably shouldn’t be put in the action itself.

(日志应该在调用每个动作的时候做记录,但是通过情况下日志不应该被放进动作内部)

Because it’s not part of the action’s own unit of work.

(因为它不是动作工作单元的一部分)

It’s more administrative, overhead if you will.

(如果细分,它应该属于高层系统管理的范畴)

Earlier, we charged a framework with the responsibility of providing built-in functional solutions to common domain tasks such as data validation, type conversion, and file uploads.

(之前,我们指出框架有责任提供一系列内建的功能解决方案来处理常见任务,例如数据验证、类型转换和文件上传)

While these tasks are important, they’re not specifically related to the action logic of the request.

(当这些任务非常重要的时候,但是他们与这些相关动作逻辑请求并没有特别的联系)

Interceptors play a huge role in the Struts 2 framework.

(在struts2框架中拦截器扮演非常重要的角色)

THE VALUESTACK AND OGNL

While interceptors may not absorb a lot of your daily development energies, the ValueStack and OGNL will be constantly on your mind.

(虽然拦截器不能消耗你太多的日常开发精力, 但是你必须时刻牢记ValueStack和OGNL)

In a nutshell, the ValueStack is a storage area that holds all of the data associated with the processing of a request.

(简单的说,ValueStack是一个存储区域,这个储存区域能装所有的被分配的请求数据)

You could think of it as a piece of scratch paper where the framework does its work while solving the problems of request processing.

(你能把他想成一个便签,当处理请求过程中的问题时,框架会在这里工作)

Rather than passing the data around, Struts 2 keeps it in a convenient, central location—the ValueStack.

(Struts2将数据集中保存在一个方便的位置,而不是传来传去 -- 这个地方就是ValueStack)

OGNL is the tool that allows us to access the data we put in that central repository.

(OGNL是一种工具,这种工具允许我们访问放入中心存储的数据)

More specifically, it is an expression language that allows you to reference and manipulate the data on the ValueStack.

(更重要的是,它是一种表达式语言,这种语言允许你引用和操控在ValueStack中的数据)

Developers new to Struts 2 probably ask more questions about the ValueStack and OGNL than anything else.

(新的开发者面对struts2问的ValueStack和OGNL的问题要比其它别的要多)

If you’re coming from Struts 1, you’ll find that these are a couple of the more exotic features of the new framework.

(如果你接触过struts1,你将会发现这个新框架更多新奇的特性)

The ActionContext contains all of the data that makes up the context in which an action occurs.

(这个ActionContext包含所有的数据,而这些数据能使上下文的动作发生)

This includes the ValueStack but also includes stuff the framework itself will use internally, such as the request, session, and application maps from the Servlet API.

(ActionContext包含ValueStack,当同时也包含使用在内部的框架本身,例如:请求、session、来自Servlet API的应用图)

The framework provides many elegant ways to interact with that data without actually touching the ActionContext, or the ValueStack, yourself.

(这个框架提供许多优雅的方式来和这些数据交互,这些数据实际可能没有接触ActionContext、ValueStack、框架本身)

Summary

These tasks are sometimes referred to as cross-cutting concerns because they occur again and again across the execution of a disparate set of application-specific actions.

(这些任务有时被称为横切关注点,因为他们在程序动作的执行中一次又一次的出现)

In Struts 2, the interceptor provides an architectural mechanism for removing cross-cutting concerns from the core MVC components.

(在Struts2中,拦截器提供了一种架构方面的机制,将这样的横切关注点从核心的MVC组件中分离出来)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: