2013-01-15 124 views
2

我在我的JSP页面复选框接受整型值:如果用户改变输入的值设置为String如何在Controller中捕获java.lang.NumberFormatException.forInputString异常?

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

,如:

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

页面会抛出一个NumberFormatException。我怎样才能在我的控制器中捕捉到这一点并显示一条有意义的消息

回答

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> 
+0

真棒!我浏览了这么久,没有出现。 Stackoverflow超过谷歌:D谢谢。 – th3an0maly

+1

顺便说一句,有什么办法,我可以喂这个到'BindingResult'变量? – th3an0maly