我有一个登录页面。登录请求可以来自多个操作类。一旦验证了用户,我就必须将其重定向到上一个操作类(登录请求已经来自)。我正在使用拦截器来做这件事。但我错过了一些东西,它无法正确重定向。这是我的代码。
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 */
}
我的struts.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用于验证用户。
答案 0 :(得分:0)
您缺少一个全局结果,该结果将使用动态参数作为目标操作的URL。
<global-results>
<result name="return" type="redirect">${target}</result>
</global-results>
您可以从拦截器或实现TargertAware
的操作返回此结果。 target
属性应该有结果调用的getter。