数据在 Struts2 中如何传输?
我试图了解数据在 Struts2 中的路径,数据如何放置在 ValueStack 上?如何找出 ValueStack 中当前存在哪个对象?我还可以从不同范围的应用程序、会话、请求、页面访问哪些其他数据?如何决定我的变量应该具有的范围?
I am trying to understand the path data takes within Struts2, how is data placed on the ValueStack? How can I find out which object is currently present in the ValueStack? What other data can I access from different scopes application, session, request, page? How to decide the scopes my variables should have?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是很多问题。
值栈只是一种数据结构,有点像映射和栈的组合。命名对象(通过 OGNL 中的
#
标签访问)位于映射中(例如请求范围),用于搜索属性/方法的对象位于堆栈中。
标记是查找值堆栈中内容的最简单方法。您还可以使用 JSP 中的“裸”数组表示法访问堆栈上的任意对象,例如"[0]"
是最顶层的对象,"[1]"
是下一个,等等。这在现实生活中几乎不是一个好主意。您可以访问每个范围内的任何内容。
您自己的对象几乎应该始终通过操作本身放置在值堆栈上,或者如果您正在实现 ModelDriven ,则通过模型放置在值堆栈上。除此之外,它与任何其他 Java EE 应用程序相同——客户端会话期间所需的对象应位于会话范围内,跨应用程序共享的对象应位于应用程序范围内,等等。
值堆栈本身位于请求范围。
That's a lot of questions.
The value stack is just a data structure, sort of a combination of a map and stack. Named objects (accessed via the
#
tag in OGNL) are in the map (like the request scope, say), objects to search through for properties/methods are on the stack.The
<s:debug>
tag is the easiest way to find out what's in the value stack. You can also access arbitrary objects on the stack using "bare" array notation in JSP, like"[0]"
is the top-most object,"[1]"
is the next, etc. This is almost never a good idea in real life.You can access whatever is in each of the scopes.
Your own objects should almost always be placed on the value stack via the action itself, or if you're implementing
ModelDriven
, via the model. Other than that it's the same as any other Java EE application--objects needed for the duration of the client's session should be in session scope, objects shared across the application should be in the application scope, etc.The value stack itself is in the request scope.
我将专门讨论“数据如何传输”元素以及 ValueStack 的堆栈性。至于有关哪些数据可用的信息,可能会随着上下文而变化;拥有“堆栈”的全部目的是支持范围内数据的上下文更改。此外,堆栈上的具体数据可以通过参考资料更好地涵盖,例如在 struts 网站上找到的资料。
Struts 2 具有非常简洁的架构。它很好地将软件的关注点分成不同的组件。其主要特征之一是 Action 是 POJO。作为一个pojo,action的主要职责是承载数据。它是主要的数据传输对象;它的属性接收传入的请求数据,前提是命名全部一致。移动数据以及考虑何时移动数据的任务由另一个组件捕获:拦截器。
数据到 ValueStack 的移动几乎完全是通过拦截器完成的。处理请求时框架要做的第一件事就是将新创建的操作对象放置到值堆栈的顶部。这支持 OGNL 访问您的操作属性。然后,拦截器将数据移动到值堆栈上,并且由于您的操作在那里,它的属性将接收匹配设置器上的数据。其他拦截器也会以类似的方式将内容移动到值堆栈上,例如验证拦截器;如果他们发现错误,该错误消息也会进入堆栈。
除了作为集中式数据容器之外,ValueStack 当然也是一个堆栈。当您考虑诸如迭代器标记之类的东西时,这种堆栈性就会发挥作用,它允许堆栈顶部的属性隐藏堆栈下部的属性。例如,如果您迭代用户集合,则每个用户都会进入堆栈顶部并在迭代主体期间保留在那里。这允许您的 OGNL 属性引用依次命中每个单独用户的属性。更重要的是,如果在堆栈的更下方找到具有类似命名属性的其他内容,它将被顶部的用户对象隐藏。请注意,考虑到这一点,push 标签允许您将您喜欢的任何对象推送到堆栈上,当您需要强制自己的上下文时提供了很好的灵活性。
I will specifically address the "How does data travel" element, and the stackness of the ValueStack. As for the bit about what data is available, that can change with the context;the whole point of having a "stack" is to support contextual changes in the data that is in scope. Furthermore, the specific data on the stack is better covered by reference materials, such as found on the struts website.
Struts 2 has a very clean architecture. It does a great job of separating concerns of the software into distinct components. One of the main halmarks of this is the fact that the Action is a POJO. As a pojo, the action's main duty is the carrying of data. It is the primary data transfer object; it's properties recieve the incoming request data, provided that the naming all lines up. The task of moving data, and thinking about when to move data, is captured in another component: the interceptor.
The movement of data onto the ValueStack is done almost exclusively with interceptors. One of the first things the framework does when processing a request is to place the newly created action object onto the top of the valuestack. This supports the OGNL access to your action's properties. Then, interceptors move data onto the valuestack, and since your action is there, it's properties will receive data on matching setters. Other interceptors will also move stuff onto the valuestack in a similar fashion, such as the validation interceptors; if they find an error, that error message goes onto the stack as well.
In addition to being the centralized data container, the ValueStack is, of course, a stack. This stackness, which allows properties on the top of the stack to hide properties lower down in the stack, comes into play when you consider such things as the iterator tag. If you iterate over a collection of users, for instance, each user goes onto the top of the stack and remains there during the body of the iteration. This allows your OGNL property references to hit the properties of each individual user in turn. More importantly, if something else with a similarly named property was found further down in the stack, it will be hidden by the user object on the top. Note, in consideration of this, the push tag, which allows you to push whatever object you like onto the stack, providing nice flexibility when you need to force your own context.