这是我的Interceptor
代码。我的目标是维护所有 URL 的会话,一旦注销完成,用户就无法访问任何 URL。
import java.util.Map;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class AuthenticationInterceptor implements Interceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
public void destroy() {
// TODO Auto-generated method stub
System.out.println("inside the destroy() of interceptor");
}
public void init() {
// TODO Auto-generated method stub
System.out.println("inside the init() of interceptor of new");
}
public String intercept(ActionInvocation ai) throws Exception {
// TODO Auto-generated method stub
System.out.println("inside the interceptor()......new");
if(ai.getAction() instanceof LogoutAction){
Map session = ai.getInvocationContext().getSession();
if (session.get("user")!=null){
System.out.println("inside logout of the session");
return ai.invoke();
}
else{
return "login";
}
}
else
return ai.invoke();
}
这是我的注销操作代码:
package com.uttara.reg;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class LogoutAction extends ActionSupport implements SessionAware {
private Map session;
public void setSession(Map s) {
session = s;
}
@Override
public String execute() throws Exception {
System.out.println("inside execute() of LA");
if(session.get("user")!=null){
session.remove("user");
return "ridirect";
}
return "failure";
}
}
这是登录完成后我的另一个操作文件用户将注册用户我的问题是如何在此处检查会话
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport implements SessionAware {
private static final long serialVersionUID = 1L;
public RegisterAction() {
System.out.println("inside the Register action const.");
}
private RegBean bean;
private Map session;
public RegBean getBean() {
return bean;
}
public void setBean(RegBean bean) {
this.bean = bean;
}
@Override
public String execute() throws Exception {
System.out.println("inside execute method");
System.out.println(bean);
Model m = new Model();
String result = m.register(bean);
if(result.equals(SUCCESS))
return SUCCESS;
else{
addActionError(getText(result));
return "failure";
}
}
@Override
public void validate(){
System.out.println("inside validate method");
}
public void setSession(Map session) {
// TODO Auto-generated method stub
System.out.println("inside setSession");
this.session = session;
}
}