I am pretty new to spring MVC and I can't overcome a problem.
here is the addFriend.jsp:
The ${USERS} is a List that contains many existing User entity instances, each with userName, id, and email.Code:<c:if test="${!empty USERS}"> <form:form method="post" action="requestForFriends.html" commandName="user"> <form:select path="userName"> <c:forEach items="${USERS}" var="user"> <form:option value="${user.userName}"></form:option> </c:forEach> </form:select> <input type="submit" value="Send freindship request" /> </form:form> </c:if>
Here is the Controller's relevant part:
My problem is whenever I submit the form a new User instance is created and used. This new instance will have the same userName as one of the existing instance but it is still a different object and all it's fields are null.Code:@RequestMapping("/toAddFriend") public ModelAndView toAddNewFriend() { Map<String, Object> model = new HashMap<String, Object>(); model.put("USERS", userService.getUsers()); ModelAndView ret=new ModelAndView("addFriend", model); ret.addObject("user", new User()); return ret; } @RequestMapping(value = "/requestForFriends", method = RequestMethod.POST) public ModelAndView requestNewFriend(@ModelAttribute("user") User user, BindingResult result) { System.out.println(user.getUserName()); System.out.println(user.getEmail()); Map<String, Object> model = new HashMap<String, Object>(); model.put("USERS", userService.getUsers()); ModelAndView ret=new ModelAndView("addFriend", model); ret.addObject("user", new User()); return ret; }
I do not want that. Instead of creating every time a new one I want to chose one of the existing User instance from the jsp's select and use that. How should I modify my code to acheive this?
Any help is highly appreciated-


Reply With Quote
