如何在Controller中捕获java.lang.NumberFormatException.forInputString异常?

时间:2013-01-15 22:49:23

标签: java exception exception-handling numberformatexception

我的JSP页面中有一个接受整数值的复选框:

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

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

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

该页面将抛出NumberFormatException。如何在我的Controller中捕获并显示有意义的消息?

1 个答案:

答案 0 :(得分:1)

您可以使用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>