How do I populate a drop down with a list using thymeleaf and spring

前端 未结 5 1911
天涯浪人
天涯浪人 2020-11-29 06:26

I need to populate a drop down with all the values within a list of strings.

Controller Class

@RequestMapping(value = \"/generateIds\", method = Requ         


        
5条回答
  •  执笔经年
    2020-11-29 07:13

    First thank to your question and answer! I've done with this solution.

    My Model

    @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;
        }
    }
    

    My View

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

    My HTML

    My Result

    Image - tymeleaf dropdown from model

提交回复
热议问题