【问题标题】:Spring Boot Thymeleaf Dropdown List option doesn't display valuesSpring Boot Thymeleaf 下拉列表选项不显示值
【发布时间】:2020-05-14 23:05:49
【问题描述】:

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

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

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

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

我的数据库中有 racords,并且 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;
    }
}

【问题讨论】:

    标签: java html spring-boot thymeleaf


    【解决方案1】:

    我已经替换了这个:

    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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      相关资源
      最近更新 更多