Answer the question
In order to leave comments, you need to log in
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";
}
<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>
@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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question