带有 Thymeleaf 帖子列表的 Spring Boot [英] Spring Boot with Thymeleaf post list

查看:35
本文介绍了带有 Thymeleaf 帖子列表的 Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串列表发布到我的控制器.但它总是只取第一个选定的值.

我的百里香 html 表单:

<div class="form-group"><label for="roleId">ID</label><input type="text" class="form-control" id="roleId" name="id" required="required"/>

<div class="form-group"><label for="rolePrivileges">权限</label><select class="form-control" id="rolePrivileges" name="privileges" multiple="multiple" size="10" required="required"><option th:each="type : ${privilegesList}" th:value="${type}" th:text="${type}">Privilege</option></选择>

<button type="submit" class="btn btn-default">创建</button></表单>

我的控制器:

@RequestMapping(value = "/add", method = RequestMethod.POST)public String addSomething(Model model, @ModelAttribute("id") String id,@ModelAttribute("privileges") List特权){//用服务添加一些东西返回重定向:/角色";}

解决方案

我认为你必须用

注释特权

@RequestParam("权限")

它不是 ModelAttribute,而是您从请求中收到的

两个 SO 线程以更好地理解 @RequestParam@ModelAttribute.

I want to post a list of strings to my controller. But it always only take the first selected value.

my thymeleaf html form:

<form action="/add" method="post">
    <div class="form-group">
        <label for="roleId">ID</label> <input type="text" class="form-control" id="roleId" name="id" required="required" />
    </div>
    <div class="form-group">
        <label for="rolePrivileges">Privileges</label>
        <select class="form-control" id="rolePrivileges" name="privileges" multiple="multiple" size="10" required="required">
            <option th:each="type : ${privilegesList}" th:value="${type}" th:text="${type}">Privilege</option>
        </select>
    </div>
    <button type="submit" class="btn btn-default">Create</button>
</form>

my controller:

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addSomething(Model model, @ModelAttribute("id") String id,
        @ModelAttribute("privileges") List<String> privileges) {
    // add something with a service
    return "redirect:/roles";
}

解决方案

I think you have to Annotate the privilges with

@RequestParam("privileges")

It is not a ModelAttribute but you receive it from the request

Edit: two SO threads to better understand the difference between @RequestParam and @ModelAttribute.

这篇关于带有 Thymeleaf 帖子列表的 Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆