package com.test.filter;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
String username = null;
String password = null;
// 取得请求相关的ActionContext实例
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();
String user = (String) session.get("username");
Map<String, Object> inputMap = ctx.getParameters();
if(inputMap == null){
System.out.println("Map is Null");
}
for(Map.Entry<String,Object> entry :inputMap.entrySet() ){
String key = entry.getKey();
System.out.println("Key:"+key);
Object value = entry.getValue();
if (value instanceof String[]) {
String[] str = (String[]) value;
if(str.length>= 1){
if(str[0] != null && (key=="username"|| key.equals("username"))){
username = str[0];
}
if(str[0] != null && (key=="password"|| key.equals("password"))){
password = str[0];
}
}
/*for(int i=0; i < str.length ; i++){
System.out.println(str[i]);
}*/
}
}
// 如果没有登陆,即用户名不存在,都返回重新登陆
/* System.out.println("user:" + user);
if (user != null) {
System.out.println("test");
return invocation.invoke();
}*/
if(username != null && password != null){
System.out.println("test");
return invocation.invoke();
}
System.out.println("你还没有登录");
ctx.put("tip", "你还没有登录");
return Action.LOGIN; // 返回一个叫login的result结果
}
}
Struts.xml
<package name="default" extends="struts-default" namespace="/"> <interceptors> <interceptor name="authority" class="com.sf.test.filter.LoginInterceptor" /> <!-- 拦截器栈 作用:可能让Struts2默认的拦截器和自定议的拦截器都能使用 --> <interceptor-stack name="mydefault"> <interceptor-ref name="defaultStack" /> <interceptor-ref name="authority" /> </interceptor-stack> </interceptors> <!-- 配置默认的拦截器 --> <default-interceptor-ref name="mydefault" /> <default-action-ref name="index" /> <!-- 定义全局Result --> <global-results> <!-- 当返回login视图名时,转入/login.jsp页面 --> <result name="login">/login.jsp</result> </global-results> <action name="show" class="com.test.action.Login2Action" method="Login2"> <result name="success">/success.jsp</result> <!-- 使用此拦截器--> <interceptor-ref name="mydefault" /> </action>
package包中 配置标签是有顺序的:
result-types
interceptors
default-interceptor-ref
default-action-ref
default-class-ref
global-results
global-exception-mappings
action*(就是所有的action放到最后)
Action
public class Login2Action extends ActionSupport {
private String username;
private String password;
public String Login2() {
if(username.equals("a") && password.equals("b")){
System.out.println("success:"+username);
return "success";
}
return Action.ERROR;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}