【问题标题】:How to populate select element in thymeleaf springboot?如何在百里香弹簧靴中填充选择元素?
【发布时间】:2016-04-02 07:58:50
【问题描述】:

我正在使用 thymeleaf、springboot 编写一个 Web 应用程序。我有一个模型类,其中包含 id、名称、地址、州和地区字段。我有 3 个表 user tablestate 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


    【解决方案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 可以解决我的问题。

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 2014-06-25
      • 2018-04-02
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 2016-06-01
      相关资源
      最近更新 更多