java - Spring Boot Thymeleaf 下拉列表选项不显示值

标签 java html spring-boot thymeleaf

这是我在这个美丽的网站上的第一个主题;)

我无法解决这个问题,我正在尝试在下拉列表中显示州和产品的名称。 Intelij 通过强调这些领域来帮助我。

https://ibb.co/wKgV940 (无法添加图片)

当我将 th:object 添加到 div 或表单时,它没有帮助。

我的数据库上有记录,并且 mathod 可以正确检索值。

                <div class="row justify-content-center">
                    <div class="form-group col-md-8" >
                        <label for="firstName" class="col-form-label">Choose State</label>
                        <select class="form-control" th:field="*{state.id}" id="state">
                            <options items="${listStates}>"></options>
                            <option th:each="state : ${states}"
                                    th:value="${state.id}"
                                    th:utext="${state.stateName}"></option>
                        </select>
                    </div>
                    <div class="form-group col-md-8" >
                        <label for="firstName" class="col-form-label">Choose products</label>
                        <select class="form-control" th:field="*{product.id}" id="product">
                            <option th:each="product : ${products}"
                                    th:value="${product.id}"
                                    th:utext="${product.productName}"></option>
                        </select>
                    </div>
                    <div class="form-group col-md-8">
                        <label for="firstName" class="col-form-label">Start price</label>
                        <input id="firstName" type="number" value="1" min="0" max="1000" step="1"/>
                    </div>
                    <div class="form-group col-md-8">
                        <label for="firstName" class="col-form-label">Preferred final prize</label>
                        <input type="text" class="form-control"
                               id="lastName" placeholder=""/>
                    </div>
                    <div class="form-group col-md-8">
                        <label for="firstName" class="col-form-label">Logistic costs</label>
                        <input type="text" class="form-control"
                               id="email" placeholder=""/>
                    </div>

                    <div class="col-md-6">
                        <input type="submit" class="btn btn-primary" value=" Calculate ">
                    </div>
                </div>
            </form>
public class CalculateController {

    @Autowired
    private StateRepository stateService;

    @Autowired
    private ProductRepository productService;

    @RequestMapping(value = { "/calculateForm" }, method = RequestMethod.GET)
    public String selectState(Model model) {
        List<Product> product = new ArrayList<>();
        List<State> state = new ArrayList<>();
        model.addAttribute("product", product);
        model.addAttribute("state", state);
        List<Product> products = productService.findAll();
        List<State> states = stateService.findAll();
        model.addAttribute("states", states);
        model.addAttribute("products", products);
        return "calculateForm";
    }
}
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String productName;
    private String category;
    private Double wholePrice;

    public Product() {
    }

    public Product(String productName, String category, Double wholePrice) {
        this.productName = productName;
        this.category = category;
        this.wholePrice = wholePrice;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Double getWholePrice() {
        return wholePrice;
    }

    public void setWholePrice(Double wholePrice) {
        this.wholePrice = wholePrice;
    }
}

最佳答案

我已经替换了这个:

List<Product> product = new ArrayList<>();
List<State> state = new ArrayList<>();
model.addAttribute("product", product);
model.addAttribute("state", state);

通过这个:

Product product = new Product();
State state = new State();
model.addAttribute("product", product);
model.addAttribute("state", state);```

now it works. Thank you.

关于java - Spring Boot Thymeleaf 下拉列表选项不显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61729720/

相关文章:

java - Play Framework 1.2 : How to add custom module dependencies

java - 字符串格式错误

html - 全身覆盖,内部 div 可点击

java - java中如何给jpanel添加jpanel

java - WebElements 列表中的相对 Xpath

javascript - 使用 Javascript 更改 HTML 元素后如何刷新它的样式?

html - 如何捕获用户上传的背景图像 Canvas 和图片?

rest - Spring Boot 不允许通过 URL 中的百分比和反斜杠

spring - 扩展 Spring Boot 加载程序

spring - 如何 : Spring get rid of @Validate for automatic Controller validation?