控制器方法/浏览器无法识别@RequestParam

时间:2014-01-28 14:56:01

标签: spring spring-mvc

我有一个控制器方法:

@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;
    }

有人可以解释这里发生了什么吗?感谢。

1 个答案:

答案 0 :(得分:0)

here所述,您应该使用@PathVariable代替@RequestParam