当前位置: 首页 > 知识库问答 >
问题:

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

龙学
2023-03-14

当我加载我的newrecord视图时,它应该封存状态表中的所有记录,并填充到select元素中?。

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

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

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

廖臻
2023-03-14

对于Q1,我通过使用Spring MVC中的@ModelAttribute得到了解决方案?

@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;
}

问题也解决了,当你在编辑模式下打开它时,它会选择你保存的值

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

 类似资料:
  • 问题内容: 我创建了一个GUI,并在外部获取了一个数据库。我正在NetBeans中使用GUI构建器来执行此操作。有谁知道用来自数据库的值填充jComboBox的简单方法吗?当我运行项目时,没有错误,但组合框保持为空。 这是设置带有折扣名称的组合框的代码: 它位于与jComboBox对象不同的类中。此类称为模型。 这是我以称为DiscountGUIView的形式调用setDiscountNames方

  • 问题内容: 我知道该怎么做,但是我发誓您可以像[[0] = {0,0,0,0};那样填写;你是怎么做到的?我确实尝试过Google,但没有任何帮助。 问题答案: 您也可以将其作为声明的一部分:

  • 假设我有一个整数数组,如和一个简单的字符串,如。我如何循环通过数组和填充每一个值。假设是这样的: 输出: 到目前为止,我已经尝试了以下内容: 但是,它在print语句中给出了。解决这个问题最好的方法是什么?

  • 我有两个下拉列表(选择元素)。第一个表示类别(从Thymeleaf模型属性填充),第二个表示所选类别的项(使用jqueryajax基于第一个select的值填充)。我希望在提交表单后保留这两个值。对于第一个下拉列表,它很简单(th:使用由Spring控制器添加的usersCategory模型属性选择): 但第二个下拉列表是动态填充的,因此html仅为: 我不知道如何仅使用Thymeleaf/Jav

  • 问题内容: 有一些简单的方法可以在Java中填充字符串吗? 似乎应该在类似StringUtil的API中使用某些东西,但是我找不到能做到这一点的任何东西。 问题答案: 有几种方法:leftPad,[rightPad](https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/Str

  • 现在,我当前的渲染代码如下: 着色器: 假设我想像JavaAWT一样,成功地填充这个形状。。。我该怎么做?(已经尝试使用GL_多边形,但它只填充整个O,我有一个填充圆,而不是O。也尝试使用jogamp glu的部分,但它只是没有渲染任何东西,不知道为什么)