spring mvc数据格式化

一、配置springmvc.xml

  <!-- 配置数据格式化 注解 所依赖的bean -->
  <bean id="formattingConversionService" class="org.springframework.format.support.FormattingConversionService">
  </bean>

二、通过注解使用


   

 @DateTimeFormat(pattern="yyyy-MM-dd")//2019-01-01 
    private Date birthday;



    @NumberFormat(pattern="###,#")
    private int stuNo;

注意:

org.springframework.format.support.FormattingConversionServiceFactoryBean 这个类包含了org.springframework.context.support.ConversionServiceFactoryBean类的功能,

所以配置了FormattingConversionServiceFactoryBean后,不再需要配置ConversionServiceFactoryBean。

FormattingConversionServiceFactoryBean包含了类型转换和格式化的功能。

错误捕获:

   

@RequestMapping("testDataTimeFormat")
    public String testDataTimeFormat(Student student,BindingResult result/*错误信息会保存在该参数中*/) {
        if(result.getErrorCount()>0)
        {
            for(FieldError error: result.getFieldErrors()){//相当于try ..catch..
                System.out.println(error.getDefaultMessage());
            }
        }
        return "info";
    }

猜你喜欢

转载自blog.csdn.net/zyfzhangyafei/article/details/89241238