title: In Thymeleaf, how can I choose the th:selected value for an HTML selector based on the current model object?
body: <p>From the following controller, I pass a list of resource values, i.e. <code>serviceRequestList</code> into the model, which I then bind an HTML selector (drop-down menu). On this page, however, exists a single object, <code>ServiceRequestDO</code>. I want the selector to default to the value of the present object, so that when changes are saved the user doesn't have to remember what all the values initially were and then reset them. Makes sense, right? Is there some Thymeleaf condition that will match the value of the present model object for some field to the selector that should be selected by default?</p>
  
  <p><strong>ServiceRequestController:</strong></p>
  
  <pre><code>@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
      String GetEditDetails(Model model, Principal principal, @PathVariable("id") Long id, @Valid @ModelAttribute(value = "requestModel") RequestModel requestModel, BindingResult bindingResult, RedirectAttributes redirectAttributes)
      {
          UserDO currentUser = userRepository.findOneByInitialName(principal.getName());
  
          // Redirect to index if user is not a manager
          if (currentUser.getRole() != Role.MANAGER) {
              return "redirect:/index";
          }
  
          ServiceRequestDO service = serviceRequestRepository.findById(id);
          model.addAttribute("service", service);
  
          model.addAttribute("currentUser", currentUser);
          model.addAttribute("requestModel", new RequestModel());
  
          List&lt;ServiceRequestTypeResource&gt; serviceRequestList = serviceRequestTypeRepository.findAll();
          List&lt;SubServiceRequestTypeResource&gt; subServiceRequestTypeList = subServiceRequestTypeRepository.findAll();
          List&lt;ServiceLineTypeResource&gt; serviceLineList = serviceLineRepository.findAll();
          List&lt;ProductTypeResource&gt; productList = productRepository.findAll();
          List&lt;CustomerNameTypeResource&gt; customerNameList = customerNameRepository.findAll();
          List&lt;LocalTeamTypeResource&gt; localTeamList = localTeamRepository.findAll();
  
          serviceRequestList.sort(null);
          subServiceRequestTypeList.sort(null);
          serviceLineList.sort(null);
          productList.sort(null);
          customerNameList.sort(null);
          localTeamList.sort(null);
  
          /* Drop-Down List */
          model.addAttribute("serviceRequestList", serviceRequestList);
          model.addAttribute("subServiceRequestList", subServiceRequestTypeList);
          model.addAttribute("serviceLineList", serviceLineList);
          model.addAttribute("productList", productList);
          model.addAttribute("customerNameList", customerNameList);
          model.addAttribute("localTeamList", localTeamList);
  
          return "edit";
      }
  </code></pre>
  
  <p><strong>HTML:</strong></p>
  
  <pre><code>&lt;tr&gt;
     &lt;td&gt;
        &lt;label rel="tooltip"
           title="" data-placement="bottom" data-html="true"&gt;Service Request Type&lt;/label&gt;
     &lt;/td&gt;
     &lt;td&gt;
        &lt;select th:name="*{serviceRequestType}"&gt;
           &lt;option th:each="serviceRequestItem : ${serviceRequestList}"
              th:value="${serviceRequestItem}"
              th:text="${serviceRequestItem}"
              th:selected="${service.serviceRequestType}"&gt;
           &lt;/option&gt;
        &lt;/select&gt;
     &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>With the template above, the selector just defaults to the bottom value in the list, regardless of the present object model's value for <code>${service.serviceRequestType}</code>, for example.</p>