【发布时间】:2016-04-02 07:58:50
【问题描述】:
我正在使用 thymeleaf、springboot 编写一个 Web 应用程序。我有一个模型类,其中包含 id、名称、地址、州和地区字段。我有 3 个表 user table,state table 带有 id、state_name、state_code 和 district table 带有 id、district_code、district_name、state_code。
如何将状态列表映射到用户表状态字段?
第一季度。当我加载我的新记录视图时,它应该从状态表中获取所有记录并填充到选择元素中?。
第二季度。当我从 select fatch all dstrict list 中选择 state 时?。
第三季度。当我在编辑模式状态下打开相同的记录时,地区列表显示其默认值?
控制器
@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;
}
百里香叶视图
<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>
【问题讨论】:
标签: mysql hibernate jpa spring-boot thymeleaf