You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by albert kao <al...@gmail.com> on 2022/02/02 21:14:08 UTC
How to pass value from AbstractInterceptor to ActionSupport classes
My application likes to pass value from AbstractInterceptor to two
ActionSupport classes (codes below).
When the user presses the Back button of a browser, hasValue() return true
for class Action1.
When the user presses the Back button of a browser, hasValue() return null
for class Action2.
My log show that request.setAttribute(KEY, Boolean.TRUE); //line A are
executed for class Action1 and Action2.
Where is the bug?
Is it related to type="tiles" and type="redirectAction" in struts.xml?
Please help.
Thanks.
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class MyInterceptor extends AbstractInterceptor {
public static final String KEY = "KEY";
@Override
public String intercept(ActionInvocation invocation) throws Exception {
HttpServletRequest httpServletRequest = (HttpServletRequest)
ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
final ActionContext context = invocation.getInvocationContext();
session = context.getSession();
HttpServletRequest request = (HttpServletRequest)
context.get(StrutsStatics.HTTP_REQUEST);
HttpSession httpSession = request.getSession(true);
synchronized (httpSession) {
httpServletRequest.setAttribute(KEY, Boolean.TRUE); //line A
}
}
}
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.ScopedModelDriven;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
public class CoreAction extends ActionSupport implements Preparable,
SessionAware, ApplicationAware, RequestAware {
public boolean hasValue() {
HttpServletRequest httpServletRequest = (HttpServletRequest)
ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
return
Boolean.TRUE.equals(httpServletRequest.getAttribute(MyInterceptor.KEY);
}
}
public class Action1 extends CoreAction implements
ScopedModelDriven<Model1> {
//...
}
public class Action2 extends CoreAction implements
ScopedModelDriven<Model2> {
//...
}
Struts version is 2.5.26.
<http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.
www.avg.com
<http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
Re: How to pass value from AbstractInterceptor to ActionSupport classes
Posted by Lukasz Lenart <lu...@apache.org>.
Hi,
I'm not sure if I understood your problem but the simplest way to pass
a value from an interceptor into an action is just by calling setter
Here you have an example how *Aware interfaces work (which is a better
option than do it as you did with
ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST))
https://github.com/apache/struts/blob/master/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java#L127-L130
Regards
--
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/
śr., 2 lut 2022 o 22:14 albert kao <al...@gmail.com> napisał(a):
>
> My application likes to pass value from AbstractInterceptor to two
> ActionSupport classes (codes below).
> When the user presses the Back button of a browser, hasValue() return true
> for class Action1.
> When the user presses the Back button of a browser, hasValue() return null
> for class Action2.
> My log show that request.setAttribute(KEY, Boolean.TRUE); //line A are
> executed for class Action1 and Action2.
> Where is the bug?
> Is it related to type="tiles" and type="redirectAction" in struts.xml?
> Please help.
> Thanks.
>
>
>
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpSession;
>
> import org.apache.struts2.StrutsStatics;
> import com.opensymphony.xwork2.ActionContext;
> import com.opensymphony.xwork2.ActionInvocation;
> import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
>
>
> public class MyInterceptor extends AbstractInterceptor {
> public static final String KEY = "KEY";
>
> @Override
> public String intercept(ActionInvocation invocation) throws Exception {
> HttpServletRequest httpServletRequest = (HttpServletRequest)
> ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
>
> final ActionContext context = invocation.getInvocationContext();
> session = context.getSession();
> HttpServletRequest request = (HttpServletRequest)
> context.get(StrutsStatics.HTTP_REQUEST);
> HttpSession httpSession = request.getSession(true);
>
> synchronized (httpSession) {
> httpServletRequest.setAttribute(KEY, Boolean.TRUE); //line A
> }
> }
> }
>
>
> import org.apache.struts2.interceptor.ApplicationAware;
> import org.apache.struts2.interceptor.RequestAware;
> import org.apache.struts2.interceptor.SessionAware;
> import org.apache.struts2.interceptor.ScopedModelDriven;
> import com.opensymphony.xwork2.ActionSupport;
> import com.opensymphony.xwork2.Preparable;
>
> public class CoreAction extends ActionSupport implements Preparable,
> SessionAware, ApplicationAware, RequestAware {
>
> public boolean hasValue() {
> HttpServletRequest httpServletRequest = (HttpServletRequest)
> ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
> return
> Boolean.TRUE.equals(httpServletRequest.getAttribute(MyInterceptor.KEY);
> }
> }
>
> public class Action1 extends CoreAction implements
> ScopedModelDriven<Model1> {
> //...
> }
>
> public class Action2 extends CoreAction implements
> ScopedModelDriven<Model2> {
> //...
> }
>
>
>
> Struts version is 2.5.26.
>
> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> Virus-free.
> www.avg.com
> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org