• InitBinder和数据校验


    public class MyDateeditor extends PropertiesEditor{
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
    SimpleDateFormat sdf=getDate(text);
    Date da=null;
    try {
    da=sdf.parse(text);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    setValue(da);
    }

    private SimpleDateFormat getDate(String text) {
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
    if (Pattern.matches("^\d{4}-\d{1,2}-\d{1,2}$",text)){
    sdf=new SimpleDateFormat("yyyy-MM-dd");
    }
    return sdf;
    }

    }


    @Controller
    public class FirstController {
    @InitBinder
    public void initbinder(WebDataBinder binder){
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    binder.registerCustomEditor(Date.class,new MyDateeditor());
    }



    @RequestMapping("first")
    public String login(String username, int age, Date birthday){
    System.out.println(username);
    System.out.println(age);
    System.out.println(birthday);
    return "second";
    }
    }



    配置:
    <context:component-scan base-package="day011"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>



    数据校验:

    public class ScondController {
    @RequestMapping("/register")
    public String register(@Valid User info, BindingResult br, Model model){
    if (br.getErrorCount()>0){
    FieldError userage=br.getFieldError("uage");
    FieldError username=br.getFieldError("uame");
    FieldError userphone=br.getFieldError("phone");
    FieldError birthday=br.getFieldError("birthday");
    FieldError email=br.getFieldError("email");
    if (userage!=null){
    String useragemsg=userage.getDefaultMessage();
    model.addAttribute("useragemsg",useragemsg);
    }
    if (username!=null){
    String usernamemsg=username.getDefaultMessage();
    model.addAttribute("usernamemsg",usernamemsg);
    }
    if (userphone!=null){
    String userphonemsg=userphone.getDefaultMessage();
    model.addAttribute("userphonemsg",userphonemsg);
    }
    if (birthday!=null){
    String birthdaymsg=birthday.getDefaultMessage();
    model.addAttribute("birthdaymsg",birthdaymsg);
    }
    if (email!=null){
    String emailmsg=email.getDefaultMessage();
    model.addAttribute("emailmsg",emailmsg);
    }
    }
    return "second";
    }
    }



    public class User {
    @NotNull(message = "用户名不能为空")
    private String uame;
    @Min(value = 10 ,message = "你太小啦")
    @Max(value = 50,message = "你太大啦")
    private Integer uage;

    @DateTimeFormat(style = "yyyy-MM-dd")
    private Date birthday;

    @Pattern(regexp = "^\w+@\w+\.\w+$",message = "邮箱格式不正确")
    private String email;

    public String getUame() {
    return uame;
    }

    public void setUame(String uame) {
    this.uame = uame;
    }

    public Integer getUage() {
    return uage;
    }

    public void setUage(Integer uage) {
    this.uage = uage;
    }

    public Date getBirthday() {
    return birthday;
    }

    public void setBirthday(Date birthday) {
    this.birthday = birthday;
    }

    public String getEmail() {
    return email;
    }

    public void setEmail(String email) {
    this.email = email;
    }

    public String getPhone() {
    return phone;
    }

    public void setPhone(String phone) {
    this.phone = phone;
    }

    @Pattern(regexp = "1[3|5|7|9|8]\d{9}",message = "手机号格式不正确")
    private String phone;


    }



    <context:component-scan base-package="day012"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>

    <bean id="myb" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
    </bean>
    <mvc:annotation-driven validator="myb"></mvc:annotation-driven>


  • 相关阅读:
    A计划
    edu_6_4_1
    edu_6_2_2
    edu_6_3_1
    hdu 1205 吃糖果【鸽巢原理】
    float 为什么不能用== ,或者大于等于,或者小于等于
    素数筛法及其优化
    hdu 1106 排序
    hdu 1164 Eddy's research I
    java 格式化输出 printf 总结
  • 原文地址:https://www.cnblogs.com/xu06123/p/8693539.html
Copyright © 2020-2023  润新知