Struts 2 - 理解OGNL和params拦截器之间的工作 [英] Struts 2 - Understanding the working between OGNL and params interceptor

查看:110
本文介绍了Struts 2 - 理解OGNL和params拦截器之间的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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



    问题1)这里,解释是什么意思?是不是params拦截器将请求参数转换为某个OGNL表达式,然后OGNL表达式提供了对ValueStack中属性的映射,还是它意味着什么?


  2. 结果开始渲染过程,Struts 2标签通过引用具有OGNL表达式的特定值从ValueStack中检索数据。



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



解决方案

答案#1



参数名称是OGNL表达式。这是ConventionOverConfiguraiton的案例。如果我们同意将参数的名称设置为可以访问javabeans属性的有效OGNL表达式,那么很容易将该名称简单地作为表达式传递给OGNL。这当然是在内部完成的;除非你正在攻击Struts 2代码的那部分,否则你真的不需要知道它是如何工作的。



答案#2



操作对象位于ValueStack之上。 ValueStack可以通过它作为ThreadLocal ActionContext的一部分存在,来自在同一线程上执行的任何代码。由于Web应用程序使用单个线程来处理请求的处理,因此我们知道Result层将能够到达ValueStack来检索数据,再次使用标记中的名称作为OGNL表达式。



注意:



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



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


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. 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".

    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?

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

    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?

解决方案

Answer #1

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.

Answer #2

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.

Note:

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 ).

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.

这篇关于Struts 2 - 理解OGNL和params拦截器之间的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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