在Struts 2中使用拦截器进行身份验证后的java登录重定向
我有一个登录页面。登录请求可以来自多个操作类。验证用户后,我必须将其重定向到上一个action类(登录请求来自该类)。我使用拦截器来做这件事。但我错过了一些东西,它无法正确重定向
这是我的代码:
public class SetTargetInterceptor extends MethodFilterInterceptor implements
Interceptor {
private static final long serialVersionUID = 1L;
public String doIntercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
HttpServletRequest request = (HttpServletRequest) invocation
.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
if (action instanceof TargetAware) {
TargetAware targetAwareAction = (TargetAware) action;
if (targetAwareAction.getTarget() == null)
targetAwareAction.setTarget(getCurrentUri(request));
}
return invocation.invoke();
}
private static String getCurrentUri(HttpServletRequest request) {
String uri = request.getRequestURI();
String[] arr = org.apache.commons.lang3.StringUtils.split(uri, "/");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
if (arr != null && arr.length > 0) {
int len = arr.length;
uri = arr[len - 1];
}
String queryString = request.getQueryString();
if (queryString != null && !queryString.equals(""))
uri += "?" + queryString;
return uri;
}
public void init() { /* do nothing */
}
public void destroy() { /* do nothing */
}
Mystruts.xml
:
<interceptors>
<interceptor name="setTargetInterceptor"
class="com.markEffy.aggregator.interceptors.SetTargetInterceptor"></interceptor>
<interceptor-stack name="newStack">
<interceptor-ref name="setTargetInterceptor"/>
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<action name="login"
class="com.markEffy.aggregator.web.LoginAction"
method="login">
<result name="success" type="redirect">
<param name="location">${target}</param>
<param name="parse">true</param>
</result>
<action name="reload"
class="com.markEffy.aggregator.web.PathFinderAction"
method="execute">
<interceptor-ref name="newStack"/>
<result name="success">/results.jsp</result>
</action>
PathFinderAction
可以请求登录loginAction
用于验证用户
# 1 楼答案
您缺少将使用动态参数作为目标操作URL的全局结果
您可以从拦截器或实现
TargertAware
的操作返回此结果。target
属性应该有一个由结果调用的getter