如何从 HttpServletRequest 对象访问 Struts 值堆栈?
我遇到过一些代码,通过简单地调用 HttpServletRequest 对象上的 getAttribute() 来访问存储在 struts 值堆栈中的值。我认为这是不可能的,它记录在哪里?
来自操作类的代码(它不会将其添加到类中,仅添加到值堆栈中):
private PaginatedChunk searchResults;
public PaginatedChunk getSearchResults() {
return searchResults;
}
public void setSearchResults(PaginatedChunk searchResults) {
this.searchResults = searchResults;
}
这是自定义标记中的代码,用于从请求中提取值(并且它有效!?):
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
PaginatedChunk searchResults = (PaginatedChunk) request.getAttribute("searchResults");
有人可以向我解释一下这是如何实现的吗 ?作品?我认为值堆栈无法通过请求直接访问。
我们运行的是struts2 v2.1.8.1
I have come across some code that is accessing a value that is stored in the struts value stack by simply calling getAttribute() on the HttpServletRequest object. I didnt think this was possible and where is it documented?
Code from the action class (it does not add it to class only to the value stack):
private PaginatedChunk searchResults;
public PaginatedChunk getSearchResults() {
return searchResults;
}
public void setSearchResults(PaginatedChunk searchResults) {
this.searchResults = searchResults;
}
This is the code in a custom tag that pulls the value from the request (and it works!?):
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
PaginatedChunk searchResults = (PaginatedChunk) request.getAttribute("searchResults");
Can someone explain to me how this works? I thought the value Stack wasn't directly accessible through the request.
We are running struts2 v2.1.8.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
乍一看我肯定同意。你所展示的内容看起来并不直观。但真正发生的是从值堆栈访问请求对象,而不是反之亦然。您从值堆栈中的 pageContext 开始,并且您仍在最后处理仍然可以访问值堆栈的内容(它也可以访问请求)。这是它是如何发生的:
看这里( http:// www.docjar.com/html/api/org/apache/struts2/ServletActionContext.java.html )我们看到“pageContext”来自哪里:
getContext().get() 返回一个对象,我们知道它是一种PageContext 的。但什么是实现类呢?
创建一个简单的操作:
然后在 JSP 中:
在我的控制台上,我看到打印:
所以我们实际上正在处理 StrutsRequestWrapper (现在我们知道我们实际上在处理什么,这很容易)...
http://massapi.com/source/struts-2.2.1/src/core/src/main/java/org/apache/struts2/dispatcher/StrutsRequestWrapper.java.html
寻找在上面的链接中我们看到 getAttribute 确实被重写了。
从第 65 行开始,我们看到它尝试从请求中获取值,如果失败,它会继续从值堆栈中获取值。
这是在哪里记录的?我想到目前为止,它已记录在这里;)
At first glance I would definitely agree. What you show does not look intuitive. But what is really happening is gaining access to the request object from the value stack, not really vice versa. You start with pageContext in the value stack, and you are still working with something at the end which still has access to the value stack (it also has access to the request too). Here is how it happens:
Looking here ( http://www.docjar.com/html/api/org/apache/struts2/ServletActionContext.java.html ) we see where "pageContext" comes from:
getContext().get() returns an Object, which we know is a kind of PageContext. But what is the implementing class?
Creating a simple action:
Then in the JSP:
On my console I see printed:
So we are really dealing with a StrutsRequestWrapper (Now that we know what we're actually dealing with it's easy)...
http://massapi.com/source/struts-2.2.1/src/core/src/main/java/org/apache/struts2/dispatcher/StrutsRequestWrapper.java.html
Looking at the above link We see that getAttribute is indeed overridden.
Starting at line 65, we see it tried to get the value out of the request, and then if that fails it goes on to get it out of the value stack.
Where is this documented? I guess it's documented here, as of now ;)