Good day everyone. This question worries me, I have an Attend object (Long id, Long studentId, String title) and Rating (Long id, Attend attend, Integer mark). I am in the controller setu ready (filled) object Attend, a list of estimates and a new object Rating and the name of the page where it all goes good.

@RequestMapping(value = {"/rating/add/{id}"}, method = RequestMethod.GET) public ModelAndView ratingAdd(@PathVariable Long id){ ModelAndView mov = new ModelAndView(); Attend attend = attendService.findById(id); Rating rating = new Rating(); List<Integer> marks = new ArrayList<>(); for (int i=1;i<11;i++){ marks.add(i); } mov.addObject("attend", attend); mov.addObject("marks", marks); mov.addObject("rating", rating); mov.setViewName("ratingAdd"); return mov; } 

On the page, I create a form for Rating and then my question is how to send the transferred object to attend in this form, select the rating and transfer the finished Rating object to the controller so that it would write to the database? I do it this way.

 <form:form action="ratingSave" method="post" modelAttribute="rating"> <form:input type="hidden" path="id"/> <form:input type="hidden" path="attend"/> <form:label path="mark">Оценка: </form:label> <form:select path="mark" id="mark"> <form:option value="0" label="Выберите оценку"/> <form:options items="${marks}"/> </form:select> <input type="submit" value="Поставить"> </form:form> 

but at the end of the record in the database is obtained with an empty Attend field in the Rating table. If you are already confused) I will repeat the question again how to send a passed-in object to a jsp in a form?

    2 answers 2

    If you understand correctly, you need the controller to accept id, attend, rating. To do this, you can use this method:

     @RequestMapping(value = "/rating/add/{id}", method = POST) public String view(@RequestParam(value = "id") int id, @RequestParam(value = "attend") Attend attend, @RequestParam(value = "rating") Rating rating, Model model) { // Some code // Add elements to the jsp (use of ${}) model.addAttribute("id", id); model.addAttribute("attend", attend); model.addAttribute("rating", rating); return "page"; } 

    In @RequestParam , value - write the name of the element. Please note that there is still such a value as defaultValue .

    • Thank you very much for the answer, and did so) - CopKubar

    As a result, I decided this way, I sent the id Attend to the usual hidden field and then in the controller using @RequestParam I got it and got the object I needed

     <form:form action="ratingSave" method="post" modelAttribute="rating"> <form:input type="hidden" path="id"/> <form:label path="mark">Оценка: </form:label> <form:select path="mark" id="mark"> <form:option value="0" label="Выберите оценку"/> <form:options items="${marks}"/> </form:select> <input type="hidden" name="attendId" value="${attendId}"> <input type="submit" value="Поставить"> </form:form> 

    Accepted here

     @RequestMapping(value = {"/rating/add/ratingSave"}, method = RequestMethod.POST) public ModelAndView ratingSave(@RequestParam("attendId") String attendId, @ModelAttribute Rating rating){ System.out.println(attendId); Attend attend = attendService.findById(Long.parseLong(attendId)); rating.setAttend(attend); ratingService.save(rating); return new ModelAndView(new RedirectView("/university/profile/"+rating.getAttend().getStudent().getId()+"/student")); }