Announcement Announcement Module
Collapse
No announcement yet.
Return error messages on POST Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Return error messages on POST

    With the following code:
    Code:
     @RequestMapping(value = "adminUsers", method = RequestMethod.GET)
        protected ModelAndView listAdminUsers() throws Exception {
            Person person = new Person();
            ModelAndView mav = new ModelAndView("adminUsersList");
            mav.addObject("adminUsersList", adminService.findAllAdminUsers());
            mav.addObject("person", person);
            return mav;
        }
    
        // Create a new item
        @RequestMapping(value="adminUsers", method = RequestMethod.POST)
        public ModelAndView listAdminUsers(Person newPerson, HttpServletResponse response)  throws Exception {
            Person person = personService.findPerson(newPerson.getUsername());
            if (person == null) {
                ModelAndView mav = new ModelAndView("adminUsersList");  //This is ugly, it's copy & pasted from above
                mav.addObject("adminUsersList", adminService.findAllAdminUsers()); //Ugly...
                // How to add error message though?
                return mav;
            } else {
                Roles roles = new Roles();
                roles.setPersonCode(person.getPersonCode());
                roles.setRoleType("ADMN");
                rolesMapper.insert(roles);
                return new ModelAndView("redirect:/admin/adminUsers.html");
            }
        }
    Code:
    <form:form method="post" action="${action}" commandName="person" >
        <form:label path="username">Add new administrator:</form:label>
        <form:input path="username" size="20"/>
        <form:errors path="username" />
        <input type="submit" value="Submit Changes"/>
    </form:form>
    
            <table>
                <c:forEach var="user" items="${adminUsersList}">
                    <tr>
                        <td>
                            ${user.title}&nbsp;<c:choose><c:when test="${not empty user.knownAs}">${user.knownAs}</c:when><c:otherwise>&nbsp;${user.forename}</c:otherwise></c:choose>&nbsp;${user.surname}
                        </td>
                        <td>
                            ${user.emailAddress}
                        </td>
                    </tr>
                </c:forEach>
            </table>
    If there is an error, (e.g. if (person == null) ) what is the best way to redisplay the form with:
    • The input box filled in
    • An error message
    • The contents of adminService.findAllAdminUsers()

    I've marked //ugly where I've just copied and pasted code. Although it's only 2 lines here, it could be a lot more complex and I don't like the feel of it. I also can't see how to bind an error message so it gets displayed in the <form:errors path="username" /> tag.

    Thanks!

  • #2
    I worked it out:

    Code:
        // Create a new item
        @RequestMapping(value="adminUsers", method = RequestMethod.POST)
        public ModelAndView listAdminUsers(Person newPerson,  BindingResult result, HttpServletResponse response)  throws Exception {
            Person person = personService.findPerson(newPerson.getUsername());
            if (person == null) {
                result.rejectValue("username","username.notvalid","Username doesn't exist");
                ModelAndView mav = new ModelAndView("adminUsersList");  //This is ugly, it's copy & pasted from above
                mav.addObject("adminUsersList", adminService.findAllAdminUsers()); //Ugly...
                return mav;
            } else {
                Roles roles = new Roles();
                roles.setPersonCode(person.getPersonCode());
                roles.setRoleType("ADMN");
                rolesMapper.insert(roles);
                return new ModelAndView("redirect:/admin/adminUsers.html");
            }
        }

    Comment

    Working...
    X