2014-01-28 29 views
0

我有一個控制器的方法:控制方法/瀏覽器不能識別@RequestParam

@RequestMapping(value = "/editComment/{id}", method = RequestMethod.GET) 
public ModelAndView showOfficeComment(@RequestParam(value="id", required = false) Long id) { 
     Office office;   
     office = officeServiceImpl.find(id); 
     ModelAndView result = new ModelAndView("editComment", "command", office); 
     return result; 
    } 

當我把地址在瀏覽器中與ID末(... editComment/100),我得到的以下錯誤消息:

java.lang.IllegalArgumentException: id to load is required for loading 

我有以下方法完美的作品:

@RequestMapping(value = "/officeForm/{id}", method=RequestMethod.GET) 
public ModelAndView showOfficeForm(@RequestParam(value="id", required = false) Long id) { 
     Office office; 
     if (id == null) {office = new Office();} 
     else {office = officeServiceImpl.find(id);} 
     office.setDepartments(new ArrayList<Department>()); 
     List<Department> departments = departmentServiceImpl.findAll(); 

     ModelAndView result = new ModelAndView("officeForm", "command", office); 
     result.addObject("departments", departments); 
     return result; 
    } 

任何人能解釋一下什麼是哈在這裏?謝謝。

回答

0

您應該使用@PathVariable而不是@RequestParam,如here所述。

+0

然後我得到「org.apache.jasper.JasperException:javax.servlet.ServletException:javax.servlet.jsp.JspException:java.lang.IllegalStateException:既沒有BindingResult也沒有bean名稱的普通目標對象'命令'作爲請求可用屬性 「 –

+0

'officeServiceImpl.find(100)'是否返回任何內容? – david

+0

是的。我得到它的工作,可能是由於我的數據庫是空的,但我不相信。我所做的只是添加一個HttpServletRequest參數併爲其綁定一個屬性。似乎與我的問題無關。 –