getting org.springframework.web.bind.MissingServletRequestParameterException

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

I am using spring annotations I have written one method

public ModelAndView showTestPage(@RequestParam("firstInstanceId") String id1,    @RequestParam("secondInstanceId") String id2, HttpSession  session) {    ModelAndView mv = new ModelAndView("showCompareItemList");   mv.addObject("pageTitle", "showCompareItemList");   mv.addObject("firstInstanceId", id1);   mv.addObject("secondInstanceId", id2);    return mv;  } 

when there both values of id1 and id2 are present it works fine but when there is only one value i get exception org.springframework.web.bind.MissingServletRequestParameterException: Required java.lang.String parameter 'secondInstanceId' is not present I tried resolve this problem by checking null but still i am getting this exception can anybody tell me what should I do to avoid this excpetion?

回答1:

If request parameter may be missed, mark it with required = false:

public ModelAndView showTestPage(@RequestParam("firstInstanceId") String id1,         @RequestParam(value = "secondInstanceId", required = false) String id2,     HttpSession session) {     ... } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!