controller的 生成验证码方法:
public void verify() {
VerifyCodeKit.createImage(getResponse(), 2);
getSession().setAttribute("verifyCode", VerifyCodeKit.getVerityCode(2));
renderNull();
}
SessionInterceptor 代码:
public class SessionInterceptor implements Interceptor {
@Inject
SysRoleFuncService sysRoleFuncService;
@Inject
LoginService loginService;
String[] excludes=WebContant.EXCLUDES.split(",");
String[]not_login_excludes=WebContant.NOT_LOGIN_EXCLUDES.split(",");
@Override
public void intercept(Invocation inv) {
Controller controller = inv.getController();
controller.getSession();
LoginAccount account = null;
String actionKey = inv.getActionKey();
String sessionId = controller.getCookie(LoginAccountUtil.SESSION_KEY);
其中如果 actionKey 为 doLogin 则跳转到对应的登陆方法中 代码为:
public Ret aopLogin(String userCode, String password, boolean keepLogin,HttpServletRequest req) throws LoginException {
//验证数据
if ((userCode == null) || (userCode.trim().length() == 0) || (password == null)
|| (password.trim().length() == 0)) {
throw new LoginException("请输入用户名和密码");
}
//验证用户
SysUser user = sysUserService.findByUserCode(userCode.toLowerCase());
if (user == null) {
throw new LoginException("用户不存在");
}
//验证码
checkVerifyCode(user, req);
checkVerifyCode 验证验证码 但是 SessionInterceptor 中不加 controller.getSession();
则第一次则获取不到验证码,第二次以后可以;加上controller.getSession(); 后 第一次就能获取到验证码 不知道为什么? 求答疑解惑
jfinal 版本为4.5
项目:JFinal
getSession() 方法可以创建 session 对象,如果不调用它的话, session 对象是始终不存在的
你在 SessionInterceptor 中调用 controller.getSession() 后会创建 session,后续在用的时候与之有关联,可能是这个相关性
还是那句话,不要猜谜,要单步调试,看代码到怎么在做什么