拦截器代码
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;
}
}
}
配置文件
/showuser.jsp
/login.jsp
/error.jsp
/admin.jsp
/user.jsp
/login.jsp
/showuser.jsp
/login.jsp
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
如果已经运行到你的
System.out.println("what "+username);部分,那确实证明拦截器已经正确工作了,但最后没有跳到login页面?死马当活马医吧,试试调整下拦截器的使用顺序?
修正:
最终原因@allme 自己已找到,是
finally里return null;的缘故。