javaBean中,validate校验(errors.rejectValue)

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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值