开发者社区> 问答> 正文

Interceptor问题:报错 

@JFinal 你好,想跟你请教个问题:
现在做了一个全局: login Interceptor 

public class LoginInterceptor implements Interceptor  {

public void intercept(ActionInvocation ai) { Controller controller=ai.getController(); UserVO userVO=controller.getSessionAttr("user"); if (userVO==null){ controller.redirect("/login.jsp"); }else { ai.invoke(); } } }

/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new LoginInterceptor());
		
	}

本意是拦截所有页面,只要没登陆,就返回登陆页面,但现在问题是在“登陆页面”中正确输入用户名及密码,还是始终返回这个login.jsp页面,仔细一想,我登陆页面用到了一个UserController(如下图示:),问题是不是出在这里?拦截器连这个UserController一起给拦截了?请问应如何做才能不拦截这个controller?还是把这个LoginInterator降级为Ctroller级,如果这样那要注解的地方不是很多?

public class UserController extends Controller {

@ActionKey("/login")
public void login(){
    System.out.println("login controller");	
    String  name=getPara("user");
    String pwd=getPara("pwd");
    User user= User.dao.findFirst("select *  from  user where name =? and pwd=?", name,pwd);
    if (user!=null){
    	UserVO userVO=new UserVO();
    	userVO.setName(user.getStr("name"));
    	userVO.setPwd(user.getStr(pwd));
    	setSessionAttr("user", userVO);
    	redirect("/gallery");
    }else{
    	render("/login.jsp");
    }
}

}

展开
收起
kun坤 2020-06-04 21:05:44 423 0
1 条回答
写回答
取消 提交回答
  • @ClearInterceptor(ClearLayer.ALL)

    ######

    引用来自“天朝子民鸭梨大”的答案

    @ClearInterceptor(ClearLayer.ALL)
    我在此处加了,果真可以了,但对@ClearInterceptor的用法还是云里雾里的:
    @ClearInterceptor
    public class UserController extends Controller {
    ...
    
    
    }
    ######

    引用来自“andying”的答案

    引用来自“天朝子民鸭梨大”的答案

    @ClearInterceptor(ClearLayer.ALL)
    我在此处加了,果真可以了,但对@ClearInterceptor的用法还是云里雾里的:
    @ClearInterceptor
    public class UserController extends Controller {
    ...
    
    
    }
        JFinal 拦截器分为三个级别:Global、Controller、Action。Global 级将对所有action进行拦截,Controller 级将对在其中的所有action 进行拦截,Action 级只拦截本 Action。     @ClearInterceptor 是用来清除向上一层,或者向上两层的拦截器, JFinal 手册中比这说得清楚,还配有示例代码,建议仔细看手册,能解决绝大多数问题。
    2020-06-08 13:15:54
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载