Struts 2 - Understanding the working between OGNL and params interceptor

我们两清 提交于 2019-12-22 19:24:37

问题


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?


回答1:


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.




回答2:


Let us say you write Login page and corresponding Login Action. On page you have a text box which has a name that says that it is a name field of User object. You also have a password field on page that says it maps to pwd field on the User object.

Now Object graph here would mean that it has to tell the params interceptor that the name field is actually a name field of user object in the login action. The action context, valuestack and OGNL will together have the knowledge that: Well maybe there are other actions as well, like FileUploadAction. And these other actions may also have a user object reference. But for current action context, the name field from the login page actually maps to nothing else but the name field of Login Action's user object. It does not belong to the name field of user object of FileUploadAction. They have this knowledge and also behave as if the field itself (actually the entire object graph) sits on value stack and is directly accessible. (A little background on ThreadLocal will be better to understand this.)

The interceptors are chained and it becomes a chain of responsibility on the way forward and back. An interceptor may perform some action is forward direction or the return direction or both. So let us say the the flow is like below:

 Page 1-->Interceptor_1--> Interceptor_2 --> Interceptor_3 -->Action_1

Then the return flow would be like below:

Action_1 --> Interceptor_3  --> Interceptor_2 --> Interceptor_1 --> The result Page

So, to answer your question, params inteceptor will be called in the return flow as well.




回答3:


Actually, the parameter name is evaluated as OGNL expression using OGNL context where the ValueStack is the root object. Here you could find useful information about OGNL in XWork. To your question: mapping means that there exist not null reference in the context returned by OGNL after evaluating the expression. So, the expression itself is a key in the abstract map which is OGNL context.

JSPs are rendered on the server side, so there's no need to move data to the client. But the OGNL expressions are evaluated by the struts tags and values are copied as strings if the values are written to the response out or use the same reference to the object by the key used as OGNL expression. Also you can create a new key or push the object to the top of the value stack.



来源:https://stackoverflow.com/questions/20853135/struts-2-understanding-the-working-between-ognl-and-params-interceptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!