【问题标题】:ModelAttribute adds only 1st value into ListModelAttribute 仅将第一个值添加到 List
【发布时间】:2017-12-04 09:33:37
【问题描述】:

在我看来,我生成输入“用户名”,选择属性 multiple=multiple,其名称为“rolesss”。

我的问题是,如果我通过邮件发送此类表单,我的控制器应该将角色转换为列表,但我只得到包含单个元素的列表。

例如: 我发送带有值的帖子:

username:MyUser
_csrf:aef50238-92cf-48df-86a4-cb6e2b8f62c9
rolesss:USER
rolesss:ADMIN

在我的控制器的调试模式下,我看到了以下值: 角色:“用户” 用户名:“我的用户”

“ADMIN”确实消失了。

我的控制器看起来像:

@Controller
@RequestMapping("/user-management")
public class UserManagementController {

    @RequestMapping("")
    public ModelAndView home() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/user-management");

        return modelAndView;
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ModelAndView changeRoles(@ModelAttribute("username") String username,@ModelAttribute("rolesss") List<String> rolesss) {

        return null;
    }

}

我认为我将 2 个百里香片段合并为 1 个,在我的代码中 #user-roles-form 是在单独的片段中,但我认为它不应该改变任何东西:

<th:block layout:fragment="main-content">
    <section>
        <h2 class="section-title no-margin-top">Role Management</h2>
        <div class="form-group">
            <div class="panel panel-primary">
                <div class="panel-heading">Změna rolí uživatele</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" action="/user-management" method="post">
                        <div class="form-group">
                            <label for="user-name" class="col-sm-2 control-label">Uživatel</label>
                            <div class="col-sm-10">
                                <input id="user-name" class="autocomplete form-control" type="text"
                                       placeholder="Jméno uživatele" name="username"/>
                            </div>
                            <input type="hidden"
                                   th:name="${_csrf.parameterName}"
                                   th:value="${_csrf.token}"/>
                        </div>
                        <div id="user-roles-form" th:fragment="roles(roles)" xmlns:th="http://www.thymeleaf.org">
                            <div class="form-group">
                                <label for="user-roles" class="col-sm-2 control-label">Uživatelovy role</label>
                                <div class="col-sm-10">
                                    <select multiple="multiple" class="form-control" id="user-roles" name="rolesss">
                                        <th:block th:each="role : ${roles}">
                                            <option th:value="${role.userRole.role}" th:text="${role.userRole.role}" th:selected="${role.selected}"></option>
                                        </th:block>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <button type="submit" class="btn btn-ar btn-primary">Uložit</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>
</th:block>

【问题讨论】:

  • 带有该输入的视图文件是什么样的?
  • 我只是添加了视图,但我认为这并不重要,因为在浏览器开发人员工具中我可以看到我添加到问题中的表单数据内容
  • 通过ajax提交表单的方式是直接form submit
  • 而不是@ModelAttribute("username") 使用@RequestParam("username")
  • 我使用按钮提交。我做的唯一奇怪的事情是,输入用户名是自动完成的。用户选择用户后,ajax生成#user-roles-form,其中包含select和button元素

标签: java spring-mvc modelattribute


【解决方案1】:

尝试在您的控制器中使用如下方式

在您的 HTML 或 JSP 中

 <form modelAttribute="your_model_name"></form>

如果您使用的是模型属性,则使用@ModelAttribute

否则,请使用@RequestParam("username")

你的情况

@RequestMapping(value = "", method = RequestMethod.POST)
public ModelAndView changeRoles(@RequestParam("username") String username,@RequestParam("rolesss") List<String> rolesss) {
     ..........
     .........
    return null;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 2017-05-04
    • 2014-04-14
    • 2020-12-25
    • 2021-02-01
    相关资源
    最近更新 更多