繁体   English   中英

如何使用百里香叶和弹簧用列表填充下拉列表

[英]How do I populate a drop down with a list using thymeleaf and 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/控制器应该是什么样的?

提前致谢!

这就是我填充下拉列表的方式。我认为这可能有助于您对此有所了解。

控制器

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>

首先感谢您的问答! 我已经完成了这个解决方案。

我的模特

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

我的结果

图像 - 来自模型的 tymeleaf 下拉菜单

要为模型中返回的字符串列表生成下拉列表,您只需要使用这些行。 您不需要使用额外的 getter 和 setter 方法创建任何模型类。 您的代码是正确的,您只是错过了用于存储 th:each 中 countryName 列表中返回值的变量名称。

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

您只需要在您的视图中使用此代码。

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

我已经实现了一个类似的地方,我需要从数据库中填充下拉列表。 为此,我从控制器中检索了数据,如下所示

@GetMapping("/addexpense")
public String addExpense(Expense expense,Model model){
    model.addAttribute("categories",categoryRepo.findAll());
    return "addExpense";
}

然后在百里香叶中我使用了下面的代码。

<div class="form-group col-md-8">
<label  class="col-form-label">Category </label>
<select  id="category" name="category" th:field="*{category.id}" >
<option th:each="category : ${categories}" th:value="${category.id}" th:utext="${category.name}"/>
</select>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM