How to catch java.lang.NumberFormatException.forInputString Exception in Controller?

天涯浪子 提交于 2019-12-19 10:42:42

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/14348387/how-to-catch-java-lang-numberformatexception-forinputstring-exception-in-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!