Struts 2 - 了解 OGNL 和 params 拦截器之间的工作

我是 Struts 2 的新手.我正在从 Struts2 In Action 一书中学习它.我很难理解 OGNL 中的一些概念,如下 -

I am new to Struts 2. I am studying it from the book Struts2 In Action. I am having difficulty in understanding some concepts in OGNL which are as follows-

  1. 我们知道params 拦截器将数据从请求参数移动到ValueStack 中的action 对象.现在在阅读时,我遇到了一句话,上面写着——这项工作的棘手部分是将参数的名称映射到 ValueStack 上的实际属性.这就是 OGNL 的用武之地.params 拦截器解释请求参数名称作为 OGNL 表达式以在 ValueStack 上定位正确的目标属性".

  1. We know that params interceptor moves the data from the request parameters to the action object in ValueStack. Now while reading, I came upon a line that says- "The tricky part of the job is mapping the name of the parameter to an actual property on the ValueStack. This is where OGNL comes in. The params interceptor interprets the request parameter name as an OGNL expression to locate the correct destination property on the ValueStack".

问题 1) 这里的解释"是什么意思?是 params 拦截器将请求参数转换为一些 OGNL 表达式,然后 OGNL 表达式提供到 ValueStack 中的属性的映射,还是意味着其他什么?

Question 1) Here, what does "interprets" mean? Is it that params interceptor translates the request parameter into some OGNL expression and then OGNL expression provides mapping to the properties in ValueStack OR does it mean something else?

当 result 开始渲染过程时,Struts 2 标签通过使用 OGNL 表达式引用特定值从 ValueStack 中检索数据.

When result start its rendering process, the Struts 2 tags retrieve data from the ValueStack by referencing specific values with OGNL expressions.

问题2)所以标签采用OGNL表达式,但是数据是如何移动的?早些时候,params 拦截器是移动数据的拦截器,但现在没有 params 拦截器.那么数据是如何被移动的呢?

Question 2) So the tags take OGNL expressions, but how is the data being moved? Earlier, params interceptor was the one that moved the data but now there is no params interceptor. So how is the data being moved?

推荐答案

答案#1

参数名称是 OGNL 表达式.这是 ConventionOverConfiguraiton 的一个例子.如果我们同意使参数的名称成为可以访问 javabeans 属性的有效 OGNL 表达式,那么很容易将该名称作为表达式传递给 OGNL.这当然是在内部完成的;除非您正在破解 Struts 2 代码的那一部分,否则您实际上不需要知道它是如何工作的.

The parameter names ARE OGNL expressions. It's a case of ConventionOverConfiguraiton. If we agree to make the names of the parameters valid OGNL expressions that can access a javabeans property, then it's easy to simply hand that name over to OGNL as an expression. This is done internally of course; you don't really need to know how it works unless you are hacking on that part of the Struts 2 code.

答案 #2

动作对象位于 ValueStack 的顶部.ValueStack 是可用的,因为它作为 ThreadLocal ActionContext 的一部分存在,可以从在同一线程上执行的任何代码中获得.由于 Web 应用程序使用单个线程来处理请求,我们知道结果层将能够到达 ValueStack 以检索数据,再次使用标记中的名称作为 OGNL 表达式.

The action object is sitting on top of the ValueStack. The ValueStack is avaible, via it's existence as part of the ThreadLocal ActionContext, from any code executing on the same thread. Since a web app uses a single thread to handle the processing of a request, we know that the Result layer will be able to get to the ValueStack to retrieve the data, again going using the name in the tag as a OGNL expression.

注意:

所有这一切的关键部分是 ValueStack 可用于在同一线程上执行的任何代码.这允许所有处理单个请求的代码都可以访问 ValueStack,它们可以通过 ThreadLocal ActionContext 获得(如果您不理解,请阅读 Java 的 ThreadLocal 类).

The key part of all of this is the fact that the ValueStack is available to any code executing on the same thread. This allows all code processing a single request to have access to the ValueStack, which they can obtain via the ThreadLocal ActionContext ( read about Java's ThreadLocal class if you don't understand ).

params 拦截器然后可以尝试使用参数名称作为 OGNL 表达式将数据写入 ValueStack(服务器作为 OGNL 上下文 - 如果您不理解,请再次阅读有关 OGNL API 的信息).然后,处理响应呈现的 Result 类中的代码可以将标签库中的各种名称和值解释为 OGNL 表达式,以从 ValueStack 读取数据.

The params interceptor can then try to use the param name as an OGNL expression to write data to the ValueStack ( which servers as the OGNL context -- again read about the OGNL API if you don't understand ). Then the code in the Result classes that handle the rendering of the response can interpret the various names and values from the tag libraries as OGNL expressions to READ data from the ValueStack.

相关文章