用拦截器实现登录验证功能authorizationinterceptor
package com.interceptor;
import com.opensymphony.xwork2.actioninvocation;
import com.opensymphony.xwork2.interceptor.*;
import com.opensymphony.xwork2.*;
import java.util.*;
public class authorizationinterceptor extends abstractinterceptor {
private string ignoreactions;
@override
public string intercept(actioninvocation invocation) throws exception {
actioncontext ctx = invocation.getinvocationcontext();
map session = ctx.getsession();
string user = (string) session.get("username");
boolean ignore = false;
string currentaction = invocation.getproxy().getactionname();
string[] actions = ignoreactions.split(",");
for (string action : actions) {
if (currentaction.matches(action.trim())) {
ignore = true;
break;
}
}
if (user != null || ignore == true) {
return invocation.invoke();
} else {
return action.login;
}
public string getignoreactios() {
return ignoreactions;
}
public void setignoreactions(string ignoreactions) {
this.ignoreactions = ignoreactions;
}
在struts.xml文件里面配置该拦截器
<interceptors>
<interceptor name="authorization" class="com.interceptor.authorizationinterceptor" />
<interceptor-stack name="mystack">
<interceptor-ref name="authorization">
<param name="ignoreactions"> validate_code,register.*,.*login.*,upload,connector</param>
</interceptor-ref>
<interceptor-ref name="defaultstack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="mystack"/>
原帖地址:http://www.cnblogs.com/jobscn/archive/2011/11/22/2258343.html