我的 JSP 页面中有一个接受整数值的复选框:
<form:checkbox path="somePath" value="2" /> Dangerous Checkbox <br />
如果用户将输入的值更改为 String
值,例如:
<form:checkbox path="somePath" value="blah" /> Dangerous Checkbox <br />
页面将抛出一个NumberFormatException
。我怎样才能在我的 Controller 中捕获它并显示有意义的消息?
最佳答案
你可以使用 JSTL 的 c:catch标签:
<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>
关于java - 如何在 Controller 中捕获 java.lang.NumberFormatException.forInputString 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14348387/