如何在Controller中捕获java.lang.NumberFormatException.forInputString异常? [英] How to catch java.lang.NumberFormatException.forInputString Exception in Controller?

查看:272
本文介绍了如何在Controller中捕获java.lang.NumberFormatException.forInputString异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < form:checkbox path =somePath value =2/>危险复选框< br /> 

如果用户将输入的值更改为 String value,例如:

 < form:checkbox path =somePathvalue =blah/>危险复选框< br /> 

该页面将抛出一个 NumberFormatException 。如何在我的控制器中找到这个并显示有意义的信息?

解决方案

可以使用JSTL的 c:catch 标签:

 < c:catch var =numberFormatException> 
< form:checkbox path =somePathvalue =blah/>危险复选框< br />
< / c:catch>

< c:if test =$ {numberFormatException!= null}>
< p>异常是:$ {numberFormatException}< br />
有一个例外:$ {numberFormatException.message}< / p>
< / c:if>


I have a checkbox in my JSP page that accepts integer values:

<form:checkbox path="somePath" value="2" /> Dangerous Checkbox <br />

If the user changes the value of the input to a String value, e.g.:

<form:checkbox path="somePath" value="blah" /> Dangerous Checkbox <br />

the page will throw a NumberFormatException. How can I catch this in my Controller and show a meaningful message?

解决方案

you can use JSTL's c:catch tag:

<c:catch var ="numberFormatException">
<form:checkbox path="somePath" value="blah" /> Dangerous Checkbox <br />
</c:catch>

<c:if test = "${numberFormatException!= null}">
   <p>The exception is : ${numberFormatException} <br />
   There is an exception: ${numberFormatException.message}</p>
</c:if>

这篇关于如何在Controller中捕获java.lang.NumberFormatException.forInputString异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆