How to pass data from Interceptor to URL and JSP?
I have an authen interceptor that check if user is logged in. If not than it will redirect to the login page, with a query string param "url" indicating the referrer URL. I tried using "actionInvocation.getInvocationContext().getParameters()" for passing values to the redirect UR开发者_Go百科L, but have no luck.
Can anyone suggest what I done wrong? Thanks a lot.
Interceptor code:
public String intercept(ActionInvocation actionInvocation) throws Exception {
Map session = actionInvocation.getInvocationContext().getSession();
Map params = actionInvocation.getInvocationContext().getParameters();
String user = (String) session.get(Constants.KEY_USER);
boolean isAuthenticated = (null!=user);
if (!isAuthenticated) {
params.put("backUrl", "http://www.some_url.com/");
return Action.LOGIN;
}
else {
return actionInvocation.invoke();
}
}
struts.xml parts
<global-results>
<result name="login" type="redirect">/login?url=${backUrl}</result>
Check the order of the interceptors in your struts.xml
file; params interceptor should be placed before login interceptor:
<interceptor-stack name="defaultLoginStack">
...
<interceptor-ref name="params" />
<interceptor-ref name="login" />
...
</interceptor-stack>
Here's an example
Another way to get the query string, is from the attribute javax.servlet.forward.query_string
:
String queryString = (String) actionInvocation.getRequest().
getAttribute("javax.servlet.forward.query_string");
精彩评论