Jfinal的初了解(三)

在进行后端开发的过程当中,都会需要使用到字段非空判断,请求的字段如果为空的话,则不走Controller。

本文记录一下在JFinal当中配置自定义注解非空判断。

首先 新建一个注解类
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface EmptyInterface {
    String[] value();
}

创建自定义的Handler 继承自JFinal的Handler
/**
 * Created by Pencilso on 2017/5/4.
 */
public class HandlerInter extends Handler {
    @Override
    public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
        String urlPara[] = {null};
        Action action = JFinal.me().getAction(target, urlPara);
        EmptyInterface annotation = action.getMethod().getAnnotation(EmptyInterface.class);
        if (annotation != null) {
            noEmpty(annotation, target, request, response, isHandled);
        } else next.handle(target, request, response, isHandled);
    }

    public void noEmpty(EmptyInterface annotation, String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
        String header = request.getHeader("Content-Type"); //取出head头
        if (header != null && header.indexOf("multipart/form-data") != -1) { //判断是否是form-data 否则有可能会报错  之前线上出现过一次log信息
            request = new MultipartRequest(request);
            ((MultipartRequest) request).getFiles();
        }
        String[] value = annotation.value();
        for (String v : value) {
            String parameter = request.getParameter(v);
            if (parameter == null || parameter.trim().length() == 0) {
                Utils.outEmpty(response, request, v);
                isHandled[0] = true;
                break;
            }
        }
        if (!isHandled[0])
            next.handle(target, request, response, isHandled);
    }
}

新建一个Utils类
 /**
 *
 * @param response 响应
 * @param request   请求
 * @param param 为空的字段
 */
public static void outEmpty(HttpServletResponse response, HttpServletRequest request, String param) {
    try {
        response.setHeader("Content-Type", "application/json;charset=UTF-8"); //设置返回头是json格式
        PrintWriter writer = response.getWriter();
        request.getInputStream().close();
        writer.println(ErrorInfo.Json(CodeUtils.ONERROR, param + " can 'not null").toString());  //输出错误信息,我这里将其封装了起来 封装了一个JSON的数据
        writer.flush();
        writer.close();
    } catch (Exception e) {
    }
}
打开JFinal的Config类,在configHandler(Handlers me) 方法下 将自定义的Handler拦截器加入。
使用方法
单参使用可以直接 @EmptyInterface("field") 多参需要大括号扩起来。
为空测试
正常测试

起初本想通过拦截全局Controller来进行字段非空验证,后来发现 一个Clear就将其给清除掉了。虽然Clear可以指定清除某一个拦截器,但不是很方便。

虽然官方也有Validator 配置,不过感觉不好使。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,149评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,323评论 19 139
  • 在进行后端开发的过程当中,都会需要使用到字段非空判断,请求的字段如果为空的话,则不走Controller。本文记录...
    Pencilso阅读 5,913评论 0 0
  • 近一段时间,上海阴雨连绵,气温下降,寒意阵阵,日子过得很无趣。 老家也是如此,雨时大时小,到处湿漉漉,气温也很低,...
    别山举水阅读 6,632评论 72 77
  • 有人说,旅行就是一群人从自己待腻的地方去往另一群人待腻的地方。以前的我是个死宅,虽然去过很多很多城市,可是于我而...
    叶子璐阅读 1,943评论 7 4