1.前台freemarker页面传参给后台时报错
报错信息:
2020-05-09 12:05:59.205 WARN 6624 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'product' on field 'originalPrice': rejected value [4,999]; codes [typeMismatch.product.originalPrice,typeMismatch.originalPrice,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [product.originalPrice,originalPrice]; arguments []; default message [originalPrice]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'originalPrice'; nested exception is java.lang.NumberFormatException: For input string: "4,999"]
Field error in object 'product' on field 'promotePrice': rejected value [4,999]; codes [typeMismatch.product.promotePrice,typeMismatch.promotePrice,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [product.promotePrice,promotePrice]; arguments []; default message [promotePrice]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'promotePrice'; nested exception is java.lang.NumberFormatException: For input string: "4,999"]]
前台代码
<div class="layui-form-item layui-row layui-col-xs12">
<label class="layui-form-label">原价</label>
<div class="layui-input-block">
<input type="text" class="layui-input"
lay-verify="required" name="originalPrice" id="originalPrice" value="${(product.originalPrice)!}" placeholder="请输入商品原价">
</div>
</div>
2.报错原因分析
通过报错信息可以看到是参数绑定异常 , 前台在给后台传商品价格的时候传的价格参数带千分符"4,999" , 应当传的参数是"4999" , 就导致参数绑定异常
前台传的参数:
3.解决方法
使用freemarker的数字内建函数 c函数 freemarker内建函数地址
将
value="${(product.originalPrice)!}"
改为
value="${((product.originalPrice)!)?c}"
将数值型数据去掉千分符,即可.