1、bean中写法:
/** 标题 */
@Length(max = 30, message = "只允许输入30个字")
@NotBlank(message = "请输入标题")
private String name;
/** 奖励额度 */
@Digits(integer = 8, fraction = 2)
@NotBlank(message = "请输入奖励额度")
private String price_setting;
/** 达币数量 */
@Digits(integer = 8, fraction = 2)
@NotNull()
private BigDecimal money;
2、controller中,根据情况判断,显示不同校验信息:
/**
* 修改
* @param B008003Bean 画面Bean
* @param request
* @return 返回上级页面
*/
@RequestMapping("/update.do")
public String update(@ModelAttribute("b008003Bean") @Validated B008003Bean b008003Bean,BindingResult errors,HttpServletRequest request,Model model){
String strprice_setting = b008003Bean.getPrice_setting();
String strupperprice = b008003Bean.getUpperprice();
BigDecimal price_setting = new BigDecimal(strprice_setting);
// 分情况显示校验信息
if(price_setting.compareTo(new BigDecimal("0.01")) == -1){
errors.rejectValue("price_setting","","奖励额度不可小于0.01。");
}
if(!strprice_setting.equals("") && !strupperprice.equals("")){
BigDecimal upperprice = new BigDecimal(strupperprice);
if(price_setting.compareTo(upperprice) == 1){
errors.rejectValue("price_setting","","奖励额度不允许超出限额。");
}
}
if(errors.hasErrors()){
// 数据重新复制回页面中
List<B008002TypeBean> categoryList = b008002Service.getCategorys();
b008003Bean.setCategoryList(categoryList);
model.addAttribute("b008003Bean", b008003Bean);
return "/b008/b008003";
}
b008003Service.update(b008003Bean);
// 返回上级页面
return "redirect:/b008/b008001/init.do";
}
3、jsp中代码:
<div class="form-group">
<label class="col-sm-2 control-label"><i style = "color:red;padding-right: 5px;">*</i>完成奖励:</label>
<div class="col-sm-8">
<sf:input type="text" onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''"
path="price_setting" placeholder="完成奖励" class="form-control" required="" value="${b008003Bean.price_setting}"/>
<sf:errors path="price_setting" cssStyle="color:red"/>
</div>
</div>