vendredi 20 février 2015

Spring MVC Controller , pass a bean between two method

I have started to learn Spring MVC by developping a simple SPRING MVC application .


I have created a JSP file for listing - Editing a list of users like this :


List-Edit Usres


When I click on the "Edit" link ( from list item ) , the form should be populated by the Item information , so the user can edit informations and save the item ( Note that this form is used for create / Edit an item)


This is the code related to the JSP file :



<c:url var="addAction" value="/admin/users/add"></c:url>

<form:form action="${addAction}" commandName="user">

<table>

<c:if test="${!empty user.adminName}">
<tr>
<td><form:label path="id">
<spring:message text="ID" />
</form:label></td>
<td><form:input path="id" readonly="true" size="8"
disabled="true" /> <form:hidden path="id" /></td>
</tr>
</c:if>

<tr>
<td><form:label path="adminName">
<spring:message text="Name" />
</form:label></td>
<td><form:input path="adminName" /></td>
</tr>

<tr>
<td><form:label path="adminEmail">
<spring:message text="adminEmail" />
</form:label></td>
<td><form:input path="adminEmail" /></td>
</tr>

<tr>

<td><form:select path="groups" items="${groupList}" /></td>
</tr>


<tr>
<td colspan="2"><c:if test="${!empty user.adminName}">
<input type="submit" value="<spring:message text="Edit Person"/>" />
</c:if> <c:if test="${empty user.adminName}">
<input type="submit" value="<spring:message text="Add Person"/>" />
</c:if></td>
</tr>
</table>
</form:form>





<br>
<div class="bs-example">
<table class="table">
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Email</th>
</tr>
</thead>


<tbody>

<c:forEach var="i" items="${users}">

<tr>
<td>${i.id}</td>
<td>${i.firstName}</td>
<td>${i.adminEmail}</td>

<td><a href="<c:url value='/admin/users/edit/${i.id}'/>">Edit</a></td>
<td><a href="<c:url value='/admin/users/remove/${i.id}'/>">Delete</a></td>


</tr>

</c:forEach>

</tbody>

</table>
</div>


My controlers Methods are :



@Controller
@RequestMapping(value = "/admin/users/")
public class UserController {

private static final Logger LOGGER = LoggerFactory
.getLogger(UserController.class);

@Autowired
UserService userService;

@Autowired
GroupService groupService;

@Autowired
LabelUtils messages;

@Autowired
private BCryptPasswordEncoder passwordEncoder;


// @PreAuthorize("hasRole('STORE_ADMIN')")
@RequestMapping(value = "list.html", method = RequestMethod.GET)
public String displayUsers(Model model) throws Exception {

List<User> users = userService.listUser();

List<Group> groups = groupService.list();

List<String> groupList = new ArrayList<String>();

model.addAttribute("users", users);
model.addAttribute("user", new User());

for (Group group : groups) {

groupList.add(group.getGroupName());

}

model.addAttribute("groupList", groupList);

return "admin/userAdmin";

}

@RequestMapping("remove/{id}")
public String removePerson(@PathVariable("id") int id) {

User user = userService.getById((long) id);

try {
userService.delete(user);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return "redirect:/admin/users/list.html";
}

// For add and update person both
@RequestMapping(value = "add" , method = RequestMethod.POST)
public String addPerson(@ModelAttribute("user") User user,
BindingResult result) {

if (result.hasErrors()) {
return "error";
}

try {
if (user.getId() == 0) {
// new person, add it

this.userService.create(user);

} else {
// existing person, call update
this.userService.saveOrUpdate(user);
}

} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return "redirect:/admin/users/list.html";


}

@RequestMapping(value = "edit/{id}" )
public String editPerson(@PathVariable("id") int id , Model model) {



// Prepare Groups

List<Group> groups = groupService.list();

List<String> groupList = new ArrayList<String>();

for (Group group : groups) {

groupList.add(group.getGroupName());

}

model.addAttribute("groupList", groupList);

model.addAttribute("user", this.userService.getById((long) id));
model.addAttribute("users", this.userService.list());

return "admin/userAdmin";

}


}


The problem is when I click on edit link , The form is correcly populated by Item information , so the user can edit retreived data , but the object "user" retreived from method "editPerson" in "addPerson" method in controller (


code line : model.addAttribute("user", this.userService.getById((long) id)); in editPerson method ) have all the other fields null so when I merge data , the save action fails .


Example : The user Item has an other field "AdminAPassword" not printed in JSP and not changed bu user , when the data are retreived from the form this field is null .


Could you help please


Thanks in Advance


Aucun commentaire:

Enregistrer un commentaire