如何在thymeleaf springboot中填充select元素?

时间:2015-12-28 13:01:54

标签: mysql hibernate jpa spring-boot thymeleaf

我正在使用thymeleaf,springboot编写一个Web应用程序。我有一个模型类,包括id,name,address,state和district字段。我有3个表用户表状态表,带有id,state_name,state_code和分区表,带有id,district_code,district_name,state_code。

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

Q1。当我加载我的新记录视图时,它应该从状态表中获取所有记录并填入select元素?。

Q2。当我从select fatch中选择状态时,该状态的所有dstrict列表?。

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

控制器

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

<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 个答案:

答案 0 :(得分:0)

对于Q1,我使用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>

对于Q2你可以使用ajax调用或javascript onChange可以解决我的问题。