P
P
Pavel Talaiko2018-03-06 18:15:13
Java
Pavel Talaiko, 2018-03-06 18:15:13

How to get Model from JSP in Spring MVC?

Hello. The problem means such, I transfer in JSP models Role through ModelMap.

@RequestMapping(value = "/users/add", method = RequestMethod.GET)
    public String getPageAddUser(ModelMap model) {
        model.addAttribute("roles", roleService.readAll());
        return "user-action";
    }

Next step. I form a JSP in which data about the User is formed, and in the select I select the Roles of this user.
<form:form method="post" name="user" action="/users">
                <div class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                    <input type="text" class="form-control form-control-sm" name="login" placeholder="Логин"
                                                   title="login" value="${user.login}"/>
                </div>

                <div class="input-group" style="margin-top: 10px; margin-bottom: 10px;">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                    <input type="password" class="form-control form-control-sm" name="password" placeholder="Пароль"
                                                   title="password"/>
                </div>

               <select class="form-control selectpicker" title="Роли пользователя" name="roles" multiple> // user.getRoles();
                    <c:forEach items="${roles}" var="role">
                        <option name="title">${role.title}</option> 
                    </c:forEach>
                </select>

                <c:if test="${!empty user.login}">
                    <input type="submit" class="btn btn-primary"
                                                   style="width: 100%; margin-top: 20px;" value="Сохранить">
                </c:if>

                <c:if test="${empty user.login}">
                    <input type="submit" class="btn btn-success"
                                                  style="width: 100%; margin-top: 20px;" value="Добавить">
            </c:if>
</form:form>

Next, I send an HTTP POST request and get the following picture.
user.getLogin() - has a name.
user.getPassword() - has a password.
user.getRoles() - has selected roles, but id == null The
question is how to return with jsp Roles in which there will be id as well as when passing to jsp?
@RequestMapping(value = "/users", method = RequestMethod.POST)
    public String editUser(@ModelAttribute("user") User user) {

        for (Role role: user.getRoles()) {
            System.out.println(role.getId() + " " + role.getTitle()); // null admin
        }

        userService.create(user);

        return "redirect:/users";
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yerlan Ibraev, 2018-03-07
@jsdevel

That's right! Where is the id-nick?!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question