开发者

HTTP Referer header in Struts 2

开发者 https://www.devze.com 2022-12-08 22:48 出处:网络
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? (Wher

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?)

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开发者_如何学Go 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.

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

Thanks!


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

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

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").

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消