你好,游客 登录 注册 搜索
背景:
阅读新闻

Jfinal中的validator理解详解

[日期:2017-03-13] 来源:Linux社区  作者:neu-student [字体: ]

为了验证账号密码不为空,需要在控制器下的login()方法前添加验证器:

1 @Before(LoginValidator.class)
2     public void login() {

而validator是实现了Interceptor(拦截器)接口。validator的用法如下:

 1 public class LoginValidator extends Validator {
 2     @Override
 3     protected void validate(Controller controller) {
 4         
 5         //验证输入的账号和密码是否是空的
 6         validateRequiredString("account", "account", "请输入账号");
 7         validateRequiredString("password", "password", "请输入密码");
 8     }
 9     @Override
10     protected void handleError(Controller controller) {
11         String actionKey = getActionKey();
12         if (actionKey.equals("/user/login")) {
13             controller.renderJsp("/view/user/login.jsp");
14         }
15     }
16 }

在上面的代码中,可以查看下面的源码可以看到,是先执行validate(validator.controller),然后执行handleError(validator.controller)

final public void intercept(Invocation invocation) {
        Validator validator = null;
        try {
            validator = getClass().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
        validator.controller = invocation.getController();
        validator.invocation = invocation;
        
        try {
            validator.validate(validator.controller);
        } catch (ValidateException e) {
            // should not be throw, short circuit validate need this
            LogKit.logNothing(e);
        }
        
        if (validator.invalid) {
            validator.handleError(validator.controller);
        } else {
            invocation.invoke();
        }
    }

知道先后顺序后,看方法中的代码分别作了什么:

validateRequiredString("account", "account", "请输入账号"):
1 protected void validateRequiredString(String field, String errorKey, String errorMessage) {
2         if (StrKit.isBlank(controller.getPara(field)))
3             addError(errorKey, errorMessage);
4     }

其中的isBlank(controller.getPara(field))只是判断得到的参数是否为空,而addError(errorKey, errorMessage):

1 protected void addError(String errorKey, String errorMessage) {
2         invalid = true;
3         controller.setAttr(errorKey, errorMessage);
4         if (shortCircuit) {
5             throw new ValidateException();
6         }
7     }

可以看到,他把validateRequiredString(String field, String errorKey, String errorMessage)中的错误key,和错误信息通过controller.setAttr()存储了起来,实际就是

存储到了request域中。

以上验证器中的第一个方法看完。

第二个方法中的代码:

protected void handleError(Controller controller) {
        String actionKey = getActionKey();
        if (actionKey.equals("/user/login")) {
            controller.renderJsp("/view/user/login.jsp");
        }
    }

第一行和if判断actionkey是否来自登录页面,如果是,就将前面存储的信息发送到指定页面

总结:先判断填入的用户名和密码是否为空,如果为空,将信息存储到request域中,发送回登录页面。

Jfinal学习之路---Controller使用 http://www.linuxidc.com/Linux/2014-07/104323.htm

JFinal开发8个常见问题 http://www.linuxidc.com/Linux/2015-02/113421.htm

JFinal的详细介绍请点这里
JFinal的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-03/141728.htm

linux
相关资讯       JFinal  Jfinal validator  Jfinal详解 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款