Struts 2 中的 HTTP Referer 标头 [英] HTTP Referer header in Struts 2

查看:42
本文介绍了Struts 2 中的 HTTP Referer 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Struts2下获取Referer header?现在我正在使用 ActionSupport 类,但似乎无法获得 ServletActionContext 对象或实现 ServletRequestAware 接口?(Struts2 中的 ServletRequestAware 在哪里?哪个 jar?)

How can I get the Referer header under Struts2? Right now I'm using an ActionSupport class and I can't seem to get a ServletActionContext object or implement the ServletRequestAware interface? (Where is ServletRequestAware in Struts2? which jar?)

我正在尝试设置自动重定向到存储在会话变量中的页面引用.当有人请求 OAuth 身份验证时,我将引用者存储在会话中,然后将它们发送到 Twitter.当他们单击允许时,twitter 会将它们发送到我的 OAuth 回调 url.我确实在那里工作(持久访问令牌),然后想将它们发送给我存储在他们会话中的引用者.

I'm trying to set up an automatic redirect to a page's referer, stored in a session variable. When someone requests OAuth authentication, I store the referer in session and then send them to twitter. When they click allow, twitter sends them to my OAuth callback url. I do work there (persist access token) and then would like to send them to the referer I've stored in their session.

正如我确定您可以说的,我对 Struts 非常陌生.我确实花了三个多小时阅读 Java 文档、谷歌搜索以及其他试图避免对我产生愤怒的菜鸟畏缩

As I'm sure you can tell, I'm very new to Struts. I did spent over three hours reading Java docs, googling and otherwise trying to avoid wrath against me the noob cringe

谢谢!

推荐答案

在动作中引用 HTTP 元素被认为是不酷的",但它变得有必要....

Its considered "uncool" to reference HTTP elements in actions, but it becomes necessary so....

org.apache.struts2.interceptor.
                         ServletRequestAware.setServletRequest(HttpServletRequest request);

实现接口,创建一个 HttpServletRequest 成员变量,然后在上面实现的 setter 中将您的成员变量设置为请求.现在你有了请求,你可以做你的request.getHeader("referer").

implement the interface, create a HttpServletRequest member variable and then set your member variable to the request in the implemented setter above. Now you have the request and you can do your request.getHeader("referer").

有些人还使用静态方法 org.apache.struts2.ServletActionContext.getRequest() 来获取请求.它被认为是不好的形式,因为它会使单元测试变得更加困难,但我一直看到它.

Some people also use the static method org.apache.struts2.ServletActionContext.getRequest() to get the request. Its considered bad form because it can make unit testing more difficult, but I see it all the time.

这篇关于Struts 2 中的 HTTP Referer 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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