복붙노트

[SPRING] 어떻게 thymeleaf와 spring을 사용하여리스트를 드롭 다운할까요?

SPRING

어떻게 thymeleaf와 spring을 사용하여리스트를 드롭 다운할까요?

문자열 목록 내의 모든 값을 드롭 다운으로 채워야합니다.

컨트롤러 클래스

@RequestMapping(value = "/generateIds", method = RequestMethod.GET)
public String launchPage(Model model) {
    List<Municipality> municipalities = rCountryService.finaAllMunicipalities();
    //assume the list is populated with values
    List<String> countryName = new ArrayList<String>();
    for(int i=0; i<municipalities.size(); i++) {
        countryName.add(municipalities.get(i).getCountry().toString());
    }
    model.addAttribute("countryName ", countryName );

    return "generateIds";
}

HTML로 시작할 위치를 알지 못해서 시작했습니다.

<select th:field="*{countryName }">
    <option value="default">Select a country</option>
    <option th:each="${countryName }" th:value="${countryName }"th:text="${countryName }"/>
</select>

목록의 모든 요소를 ​​드롭 다운 옵션으로 추가하려면 내 html / 컨트롤러를 어떻게해야합니까?

미리 감사드립니다.

해결법

  1. ==============================

    1.이게 내가 드롭 다운리스트를 채우는 방법입니다. 제 생각에 도움이 될 것 같아요.

    이게 내가 드롭 다운리스트를 채우는 방법입니다. 제 생각에 도움이 될 것 같아요.

    제어 장치

    List<Operator> operators =  operatorService.getAllOperaors()
    model.addAttribute("operators", operators);
    

    모델

      @Entity
      @Table(name = "operator")
      public class Operator {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        @JsonIgnore
        private Long id;
    
        @NotBlank(message="operator Name cannot be empty")
        @Column(name = "operator_name", nullable = false)
        private String operatorName;
    
        getters and setters ...
    
    }   
    

    전망

    <div class="form-group blu-margin">
        <select class="form-control" th:field="${operator.opeId}"  id="dropOperator">
        <option value="0">select operator</option>
        <option th:each="operator : ${operators}" th:value="${operator.id}" th:text="${operator.operatorName}"></option>
        </select>
    </div>
    
  2. ==============================

    2.먼저 귀하의 질의 응답에 감사드립니다! 나는이 해결책으로 끝냈다.

    먼저 귀하의 질의 응답에 감사드립니다! 나는이 해결책으로 끝냈다.

    내 모델

    @Entity
    @Table(name = "test")
    public class Test {
    
        @Id
        private String testCode;
        private String testName;
        private int price;
    
        public Test() {}
    
        public Test(String testCode, String testName, int price) {
            this.testCode = testCode;
            this.testName = testName;
            this.price = price;
        }
    
        public String getTestCode() {
            return testCode;
        }
    
        public String getTestName() {
            return testName;
        }
    
        public int getPrice() {
            return price;
        }
    }
    

    내보기

        List<Test> test = new ArrayList<>();
        model.addAttribute("test", test);
        List<Test> tests = testRepository.findAll();
        model.addAttribute("tests", tests);
    

    내 HTML

    <div class="col-lg-3" th:object="${test}">
        <select class="form-control" id="testOrder" name="testOrder">
            <option value="">Select Test Order</option>
            <option th:each="test : ${tests}"
                    th:value="${test.testCode}"
                    th:text="${test.testCode}+' : '+${test.testName}"></option>
        </select>
    </div>
    

    내 결과

    이미지 - 모델에서 thymeleaf 드롭 다운

  3. ==============================

    3.이 코드는보기에만 필요합니다.

    이 코드는보기에만 필요합니다.

    List<Test> tests = testRepository.findAll();
    model.addAttribute("tests", tests);
    
  4. from https://stackoverflow.com/questions/37715276/how-do-i-populate-a-drop-down-with-a-list-using-thymeleaf-and-spring by cc-by-sa and MIT license