There is a method that returns answers using id .

 @RequestMapping(value = "/getAnswerByQuestionId",method = RequestMethod.GET) public String getAnswerByQuestionId(ModelMap map,@RequestParam("id")int id){ List<Answer> findAllBYQuestionId=answerRepository.findAllByQuestionId(id); map.addAttribute("findAllAnswersByQuestionId",findAllBYQuestionId); map.addAttribute("addAnswer",new Answer()); return "answerResult"; } 

this method goes to JSP here

 <spring:form action="/addAnswer" method="post" enctype="multipart/form-data" modelAttribute="addAnswer"> <c:forEach items="${findAllAnswersByQuestionId}" var="name"> ${name.text} <input type="hidden" value="${name.id}" name="questionId"> </c:forEach> <spring:textarea path="text"></spring:textarea> <input type="file" name="pict"> <input type="submit" value="add"> </spring:form> 

on the second line prints the result. In this JSP, under the result is a form for adding a response. When everything is ok - the answer is added, the data goes here.

 @RequestMapping(value = "/addAnswer",method = RequestMethod.POST) public String addAnswer(@ModelAttribute("answer")Answer answer,@AuthenticationPrincipal UserDetails userDetails,@RequestParam("questionId")int id,@RequestParam("pict")MultipartFile[] files) throws IOException { File dir = new File(imageUploadPath); if (!dir.exists()) { dir.mkdir(); } for (MultipartFile file : files) { String picName = System.currentTimeMillis() + "_" + file.getOriginalFilename(); File picture = new File("D:\\bankSystem\\" + picName); file.transferTo(picture); answer.setImage(picName); answer.setUserUsername(userDetails.getUsername()); answer.setQuestionId(id); } answerRepository.save(answer); return "redirect:/getAnswerByQuestionId"; } 

Here, getAnswerByQuestionId added and goes back to this method, so that the user adds another answer or sees his answer. The problem is that when you go back to this method getAnswerByQuestionId an error occurs

 request param id is not present 

It will say that they did not give id so that he could print the result. Is it possible to do so in order not to give an id and enter this JSP?

  • if an error occurs, then there should be an exception - Roman C
  • The console does not give anything - Developer
  • What is the mistake then? - Roman C
  • request param id is not present it gives on the browser and the console gives nothing but this error gives when you go back to the method - Developer

1 answer 1

The parameter can be passed using RedirectAttributes . When you redirect a request, it is necessary that the required parameters are in the url.

 @RequestMapping(value = "/addAnswer",method = RequestMethod.POST) public String addAnswer(@ModelAttribute("answer")Answer answer, @AuthenticationPrincipal UserDetails userDetails, @RequestParam("questionId")int id, @RequestParam("pict")MultipartFile[] files, RedirectAttributes redirectAttributes) throws IOException { .... redirectAttributes.addAttribute("id", id); return "redirect:/getAnswerByQuestionId"; } 

For more information, see the documentation .

  • Please give an example. I didn’t really understand exactly where the parameter should be given - Developer
  • I hope that it will be so clear - Roman C
  • Thank you so much thank you thank you very much - Developer