Struts2 ValueStack 如何处理多个请求

2022-01-16 00:00:00 java jsp struts2 ognl valuestack

我了解 ValueStack 是在 Struts2 中引入的,而 Struts1 模型的另一个变化是现在为每个请求实例化一个新的 ActionObject.所以我们可以定义实例变量而不用担心多线程问题.拦截器和 JSP 从 ActionObjects 访问实例变量的方式是通过 ValueStack.但是 ValueStack 的实现方式(或至少被 Struts2 框架使用)是由 ValueStack 实现的.使访问更容易,这样我们就不需要遍历整个对象树.我有以下问题.

I understand ValueStack was introduced in Struts2 and one more change from Struts1 model is now a new ActionObject is instantiated for each request. So we can define instance variables without worrying about multi threading issues. The way interceptors and JSPs access the instance variables from the ActionObjects is through the ValueStack. But the way ValueStack is implemented (or at least used by Struts2 framework) is by ValueStack. to make the access easier so that we don't need to traverse the whole object tree. I have following questions.

  1. 如果我有嵌入对象(对象的多个层次结构)怎么办?在这种情况下访问机制如何表现?

  1. What if I have embedded objects (multiple hierarchy of objects) ? how does the access mechanism behave in such a case?

如果假设有 2 个客户端同时向同一个 actin 发出请求,并且由于 2 个客户端提供的输入返回 2 个不同的结果,Action 执行的结果是不同的.假设我的 Action Class 有一种获得最优价格的方法,并且基于我的后端服务中的逻辑,对于 2 个不同的请求,结果分别为 10 和 12.现在 ActionClass 有一个名为 price 的成员变量,该值将存储在其中,生成的 JSP showResults.jsp 将访问该变量(使用标签库)以显示价格.struts2 框架如何保证 client1 和 client2 得到正确的响应,并且在 JSP 上显示响应时价格不会混乱,因为据我了解 ValueStack 只是先进先出(堆栈逻辑)时尚.因此,它可能最终向两个客户端请求返回 10,因为相同的变量在值堆栈上存储了两次(具有相同的名称)但具有不同的值.

If let us say 2 clients made requests to the same actin at the same time and the result of Action execution are different because the inputs provided by 2 clients came back with 2 different results. Let us say my Action Class has a method to get best price and based on the logic in my backend service the results come out as 10 and 12 for 2 different requests. Now ActionClass has a member variable called price in which this value will be stored and the resultant JSP showResults.jsp will access this variable (using a tag lib) to show the price. How does struts2 framework guarantee that client1 and client2 get the right response back and prices are not jumbled while the response is shown on the JSP because from what I understand the ValueStack just goes in first in first out (stack logic) fashion. So it might possibly end up returning 10 to both client requests as the same variable is stored twice (with the same name) on the value stack but with different values.

ValueStack 何时从列表中销毁对象?

When does the ValueStack destroy the object from its list?

推荐答案

  1. 嵌入式对象或换句话说嵌套 bean 直接通过其访问器或通过在评估 OGNL 表达式时使用该访问器的 OGNL 访问.Struts2 在 valueStacktop 上放置一个动作 bean,因此动作属性是按名称检索的,但嵌套 bean 使用 OGNL dot 解析符号.更多关于 OGNL 您可以在文档中找到.

  1. Embedded objects or in other words nested beans are accessed directly via their accessors or via OGNL that used that accessors when evaluating OGNL expression. Struts2 places an action bean on the top of the valueStack, so the action properties are retrieved by name, but nested beans are resolved using OGNL dot notation. More about OGNL you can find in the documentation .

每个动作实例都有自己的上下文和valueStack,所以客户端不会互相干扰,并且有自己的值,除非值在application范围.

Each action instance has it's own context and valueStack, so the clients do not interfere with each other and have its own values unless the values are maintained in the application scope.

valueStack 由调度程序创建并放入操作上下文.它也可以在需要时由一些拦截器重新创建.您不必担心它的值,因为它们在操作结束时会被销毁.

The valueStack is created by the dispatcher and put to the action context. It also could be recreated by some interceptors when needed. You should not worry about its values because they are destroyed at the action end.

相关文章