gpt4 book ai didi

mysql - 如何在 thymeleaf springboot 中填充选择元素?

转载 作者:行者123 更新时间:2023-11-30 22:28:40 25 4
gpt4 key购买 nike

我正在使用 thymeleaf、springboot 编写一个 Web 应用程序。我有一个带有 id、name、address、state 和 district 字段的模型类。我有 3 个表user tablestate table with id, state_name, state_code, and district table with id, district_code, district_name, state_code。

如何将状态列表映射到用户表状态字段?。

Q1。当我加载我的新记录 View 时,它应该从状态表中获取所有记录并填充到选择元素中?

Q2。当我从 select fatch all dstrict 列表中选择状态时?。

Q3。当我在编辑模式状态下打开同一条记录时,地区列表显示其默认值?。

Controller

@RequestMapping("user/new")
public String newUser(Model model){
model.addAttribute("user", new User());
return "userform";
}

@RequestMapping("user/edit/{id}")
public String update(@PathVariable Integer id, Model model){
model.addAttribute("user", userService.getProductById(id));
return "userform";
}

模型

@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String address;
private String state;
private String dist;
//getter setter
}

public class State {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer state_id;
private String state_name;
private String state_code;
}

thymeleaf View

<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
</head>
<body>
<div class="container">
<h2>User Details</h2>
<div>
<form class="form-horizontal" th:object="${user}" th:action="@{/user}" method="post">
<input type="hidden" th:field="*{id}"/>
<div class="form-group">
<label class="col-sm-2 control-label">Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{name}"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Address:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{address}"/>
</div>
</div>
**//NOTE:- These select list not working I am able to display above information not this list**
*<div class="form-group">
<label class="col-sm-2 control-label">State:</label>
<div class="col-sm-10">
<select class="form-control" name="selectState" th:field="*{state}" >
<option th:each="sopt:${state}" th:value="?" th:text="?">State</option>
</select>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Dist:</label>
<div class="col-sm-10">
<select class="form-control" name="selectDistrict" th:field="*{dist}" >
<option th:each="sopt:${dist}" th:value="?" th:text="?">Dist</option>
</select>
</div>
</div>*
<div class="row">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
</div>
</body>
</html>

最佳答案

对于第 1 季度,我通过使用 What is @ModelAttribute in Spring MVC? 得到了解决方案

@ModelAttribute("states")
public List<State> populateStates() {
List<State> listStates = new ArrayList<State>();
for (State e : stateService.listAllState()) {
System.out.println(e.getState_code());
listStates.add(e);
}
return listStates;
}

当您在编辑模式下打开它时,Q3 问题也由此解决,它将选择您保存的值

<select th:field="*{states}" id="state" onChange="onstatechange(this)" class="infobox">
<option value="0">Select Your State</option>
<option th:each="state : ${states}" th:value="${state.state_id}" th:text="${state.state_name}"></option>
</select>

对于第二季度,您可以使用 ajax 调用或 javascript onChange 来解决我的问题。

关于mysql - 如何在 thymeleaf springboot 中填充选择元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34494024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com