在Thymeleaf Spring坚持无线电检查价值

时间:2017-08-19 04:58:05

标签: spring spring-mvc spring-boot thymeleaf

搜索时,如果我们选择一个给定的字段进行搜索并提交,我们的选择就会被遗忘。如何在显示结果时修改视图模板以保持选择上一个搜索字段?

我已经阅读了许多其他SO问题和thymeleaf documentation here但尚未找到合适的答案。

可以使用以下内容对字符串(如雇主)进行硬编码:

search.html代码段

<span th:each="column : ${columns}">
  <input
    type="radio"
    name="searchType"
    th:id="${column.key}"
    th:value="${column.key}"
    th:checked="${column.key == 'employer'}"/>

    <label th:for="${column.key}" th:text="${column.value}"></label>
 </span>

SearchController.html代码段

@RequestMapping(value = "")
public String search(Model model) {
    model.addAttribute("columns", columnChoices);
    return "search";
}

如何在Thymeleaf Spring的POST中保留用户选择的无线电值? (默认为第一个,GET上的值)

1 个答案:

答案 0 :(得分:0)

http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#creating-a-form

命令对象。

mysql -D newdatabasename -h newhostname -u newusername -p < mysqldata.sql

控制器

public class Search {
    // default "employer"
    private String type = "employer";

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

使用搜索作为命令对象,单选按钮应如下所示:

@GetMapping
public String search(Model model) {
    model.addAttribute("columns", columnChoices);
    model.addAttribute("search", new Search());
    return "search";
}

@PostMapping
public String search(@ModelAttribute Search search) {
    // Search's attributes match form selections.
}
相关问题