title: How to pass model attributes globally to avoid repeatable code in Spring controller?
body: <p>I would like to ask you for some best practice to reduce the amount of repeatable code in my controller's methods - one of them is presented below.</p>
  <p>I have quite a big and complex view with a lot of information and two forms. In every method of my controller (and there are quite a few) I have to pass the same attributes to my view (in post controllers even twice). I added a note SAME CODE in the code snippet below indicating identical pieces of code.</p>
  <p>I wonder if there is any possibility to make a one global method in the controller gathering all attributes to be passed to the model and just reference it in any of particular methods?</p>
  <p>I looked into ModelAndView or ModelMap, but cannot see those being suitable here.</p>
  <p>Just want to avoid repeating this part:</p>
  <p><strong>Repeatable piece of code</strong></p>
  <pre><code>model.addAttribute(&quot;hotels&quot;, hotelService.getAllHotels());
  List&lt;GetRoomDto&gt; roomsDto = roomService.getAllRoomsByHotelId(hotelId);
  model.addAttribute(&quot;rooms&quot;, roomsDto);
  model.addAttribute(&quot;roomTypes&quot;, roomTypeService.findAllRoomTypeNames());
  </code></pre>
  <p><strong>Full method with that piece of code appearing twice</strong></p>
  <pre><code>@PostMapping(&quot;/hotels/{hotelId}/rooms&quot;)
      public String createRoomForHotelById(@ModelAttribute(&quot;room&quot;) @Valid NewRoomDto roomDto,
                                           BindingResult result,
                                           @PathVariable(&quot;hotelId&quot;) Long hotelId,
                                           Model model) {
          if(result.hasErrors()) {
              // SAME CODE
              model.addAttribute(&quot;hotels&quot;, hotelService.getAllHotels());
              List&lt;GetRoomDto&gt; roomsDto = roomService.getAllRoomsByHotelId(hotelId);
              model.addAttribute(&quot;rooms&quot;, roomsDto);
              model.addAttribute(&quot;roomTypes&quot;, roomTypeService.findAllRoomTypeNames());
              //
              
              model.addAttribute(&quot;hotel&quot;, new NewHotelDto());
              LOG.info(&quot;Binding error: {}&quot;, result.toString());
              return &quot;admin/dashboard&quot;;
          }
          
          // SAME CODE
          model.addAttribute(&quot;hotels&quot;, hotelService.getAllHotels());
          List&lt;GetRoomDto&gt; roomsDto = roomService.getAllRoomsByHotelId(hotelId);
          model.addAttribute(&quot;rooms&quot;, roomsDto);
          model.addAttribute(&quot;roomTypes&quot;, roomTypeService.findAllRoomTypeNames());
          //
          
          LOG.info(&quot;AdminController: CreateRoomForHotelById: Created room: {}&quot;, roomDto.toString());
          
          roomDto.setHotelId(hotelId);
          roomService.createNewRoom(roomDto);
          
          return &quot;redirect:/auth/admin/hotels/{hotelId}/rooms&quot;;
      }
  </code></pre>