我的代码中有NumberFormatException
。任何人都可以建议我如何克服这个问题?我正在尝试根据客户ID添加查询。客户ID是查询表中的连接列。当我点击添加查询链接时,我必须自动获取特定的ID。
@InitBinder
public void initBinder1(WebDataBinder binder) {
binder.registerCustomEditor(Customer.class,"customer", new PropertyEditorSupport() {
@Override
public void setAsText(String customer) {
setValue(new Customer(Integer.parseInt(customer)));
}
});
}
@RequestMapping(value="/addQuery.htm", method=RequestMethod.GET)
public ModelAndView reportForm1(ModelMap model,HttpServletRequest request) {
int id=Integer.parseInt(request.getParameter("id"));
System.out.println(id);
Queries query = new Queries();
Customer customer = new Customer(id);
query.setCustomer(customer);
model.addAttribute("queries", query);
ModelAndView mav = new ModelAndView("addQuery");
return mav;
}
这是我得到的错误:
Oct 14, 2014 12:25:32 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path
[/CustomerPortal] threw exception [Request processing failed; nested exception is
java.lang.NumberFormatException: null] with root cause
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.axiscapstone.customer.web.CustomerController.reportForm1(CustomerController.java:86)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
答案 0 :(得分:0)
不要自己做这个转换。让Spring做到:
@RequestMapping(value="/addQuery.htm", method=RequestMethod.GET)
public ModelAndView reportForm1(@RequestParam Integer id, ModelMap model) {
System.out.println(id);
...
但是id是Integer类型,当然可以为null。
您可以使用@RequestParam(required = true)