拦截器代码
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.Map;
/**
* Created by allme on 2016/5/25.
*/
public class LoginInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
try{
Map session = actionInvocation.getInvocationContext().getSession();
String username = (String) session.get("username");
String userpwd = (String) session.get("userpwd");
if("".equals(username)||"".equals(userpwd)||username==null||userpwd==null){
System.out.println("what "+username);
System.out.println("what "+userpwd);
return "login";
}
else{
return actionInvocation.invoke();
}
}catch (Exception e){
e.printStackTrace();
}finally {
return null;
}
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="register" extends="struts-default">
<action name="useraction" class="cn.edu.zucc.action.UserAction">
<result name="showuser">/showuser.jsp</result>
</action>
</package>
<package name="login" extends="struts-default">
<interceptors>
<interceptor name="Login" class="cn.edu.zucc.interceptot.LoginInterceptor" />
<interceptor-stack name="loginstack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="Login"></interceptor-ref>
<interceptor-ref name="timer"></interceptor-ref>
</interceptor-stack>
</interceptors>
繁星coding
相关分类