Getting org.springframework.web.bind.MissingServletRequestParameterException

I use spring annotations, I wrote 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 both id1 and id2 are present, it works fine, but when there is only one value, I get an org.springframework.web.bind.MissingServletRequestParameterException exception: The required parameter java.lang.String 'secondInstanceId' is missing I tried to solve this problem by checking zero, but still I get this exception. Can anyone tell me what to do to avoid this exception?

+3
spring spring-mvc
source share
1 answer

If the request parameter may be skipped, check its required = false :

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

All Articles