使用JSR-303进行校验(1),面试宝典

4、spring标签库的用法


<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>   

  

<html>   

<head>   

<title>Reservation Form</title>   

<style>   

.error {   

    color: #ff0000;   

    font-weight: bold;   

}   

</style>   

</head>   

  

<body>   

    <form:form method="post" modelAttribute="vm">   

        <form:errors path="*" cssClass="error" />   

        <table>   

            <tr>   

                <td>Name</td>   

                <td><form:input path="userName" />   

                </td>   

                <td><form:errors path="userName" cssClass="error" />   

                </td>   

            </tr>   

            <tr>   

                <td>email</td>   

                <td><form:input path="email" />   

                </td>   

                <td><form:errors path="email" cssClass="error" />   

                </td>   

            </tr>   

       

            <tr>   

                <td colspan="3"><input type="submit" />   

                </td>   

            </tr>   

        </table>   

    </form:form>   

</body>   

</html>

5、开启spring的Valid功能


<mvc:annotation-driven />

三、自定义校验类型


1、需要验证的实体 Bean


public class LoginVo {

    

    @NotNull

    @IsMobile //自定义的注解

    private String mobile;

    

    @NotNull

    @Length(min=32)

    private String password;

    

    //省略 get set 方法

} 

2、自定义 IsMobile 注解类


import static java.lang.annotation.ElementType.ANNOTATION_TYPE;

import static java.lang.annotation.ElementType.CONSTRUCTOR;

import static java.lang.annotation.ElementType.FIELD;

import static java.lang.annotation.ElementType.METHOD;

import static java.lang.annotation.ElementType.PARAMETER;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;

import java.lang.annotation.Retention;

import java.lang.annotation.Target;

import javax.validation.Constraint;

import javax.validation.Payload;

 

@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })

@Retention(RUNTIME)

@Documented

@Constraint(validatedBy = {IsMobileValidator.class })

public @interface  IsMobile {

    

  //允许为空的属性

    boolean required() default true;

 

    //如果校验不通过返回的提示信息

    String message() default "手机号码格式错误";

 

    Class<?>[] groups() default { };

 

    Class<? extends Payload>[] payload() default { };

} 

3、校验器


import  javax.validation.ConstraintValidator;

import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang3.StringUtils;

import com.imooc.miaosha.util.ValidatorUtil;

 

//IsMobile:自定义的注解

//String:注解参数类型

public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {

 

  //默认值_false,用于接收注解上自定义的 required

    private boolean required = false;

    

  //1、初始化方法:通过该方法我们可以拿到我们的注解

    public void initialize(IsMobile constraintAnnotation) {

 

        //constraintAnnotation.required() 接收我们自定义的属性,是否为空

        required = constraintAnnotation.required();

    }

 

  //2、逻辑处理


### 最后

毕竟工作也这么久了 ,除了途虎一轮,也七七八八面试了不少大厂,像阿里、饿了么、美团、滴滴这些面试过程就不一一写在这篇文章上了。我会整理一份详细的面试过程及大家想知道的一些问题细节

### 美团面试经验
![美团面试](https://img-blog.csdnimg.cn/img_convert/120428b3297bdceb312b567878d65d5e.png)
字节面试经验
![字节面试](https://img-blog.csdnimg.cn/img_convert/0dc1f994d7c60586500c1b3e58f38e38.png)
菜鸟面试经验
![菜鸟面试](https://img-blog.csdnimg.cn/img_convert/1c48323a4391f12b5f2c4eb96390de0d.png)
蚂蚁金服面试经验
![蚂蚁金服](https://img-blog.csdnimg.cn/img_convert/e91964dd4ec9dc679a77da2d0da2bb0e.png)
唯品会面试经验
![唯品会](https://img-blog.csdnimg.cn/img_convert/8f24d4bf51040f428ec2f4763d1754a5.png)

>因篇幅有限,图文无法详细发出,感兴趣的朋友[可以点击这里前往我的腾讯文档](https://gitee.com/vip204888/java-p7)免费获取上述资料!

.(img-GM1a0026-1628396854170)]
菜鸟面试经验
[外链图片转存中...(img-mlvVHT0w-1628396854171)]
蚂蚁金服面试经验
[外链图片转存中...(img-CaQRJRtX-1628396854173)]
唯品会面试经验
[外链图片转存中...(img-jwMoYTz0-1628396854174)]

>因篇幅有限,图文无法详细发出,感兴趣的朋友[可以点击这里前往我的腾讯文档](https://gitee.com/vip204888/java-p7)免费获取上述资料!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值