我有带有 @InitBinder
的 Controller ,该 Controller 具有 setAsText
方法,但由于某些未知原因,当传递空字符串时出现异常。异常(exception):
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [java.lang.NumberFormatException: For input string: ""] with root cause
java.lang.NumberFormatException: For input string: ""
此 JSP 代码导致错误:
<div class="form-group">
<label for="organization">
<spring:message code="label.organization"></spring:message>:</label>
<form:select path="organization" class="form-control" id="organization">
<form:option value="" label="- Select -" />
<form:options items="${organizations}" itemLabel="name" itemValue="id" />
</form:select>
</div>
Controller :
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Organization.class,
new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
Organization organization = organizationService.getOrganization(Integer.parseInt(text));
setValue(organization);
}
});
}
出了什么问题?如何让它发挥作用?
更新:
我不明白为什么空字符串作为 setAsText
方法的参数到达。首先浏览器向 url 发出请求,Spring 调度程序 servlet 提供匹配 Controller 的方法,然后发生一些神奇的事情,空字符串到达 @InitBinder 的 setAsText
方法......
最佳答案
已解决:
public void setAsText(String text) {
Object organization;
if ("".equals(text)) {
organization = (List < Organization > ) organizationService.getOrganizations();
} else {
organization = organizationService.getOrganization(Integer.parseInt(text));
}
setValue(organization);
}
关于java - 调度程序 servlet 抛出异常 : NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28637950/