开发者

Update field in jsp page and persist it in Spring MVC

开发者 https://www.devze.com 2023-04-02 14:16 出处:网络
I\'m new to Spring MVC, and looking for a good way to do the following: I have a jsp page displaying a table of users. In my controller I add the users via the following code:

I'm new to Spring MVC, and looking for a good way to do the following:

I have a jsp page displaying a table of users. In my controller I add the users via the following code:

@ModelAttribute("users")
public List<UserAccount> getAllRegisteredUsers() {
    return adminService.getRegisteredUsers();
}

In the table I also have a button which says "Disable User". When someone clicks on that button, I want call the setEnabled(false) method of that particular user to be called, and finally persist the user, before displaying the same page again. Right now the "Disable User" link in JSP looks like the following:

<a onClick=$.post("/admin/deactivate/${user.id}")>Deactivate</a>

My controller receives the request here:

@RequestMapping(value = "/admin/deactivate/{id}", method = RequestMethod.POST)
public String deactivateUser(@ModelAttribute("users") ArrayList<UserAccount> users, @PathVariable("id") Long userId, ModelMap map) {
    for (UserAccount userAccount : users) {
        if (userAccount.getId() == userId) {
             adminService.deactivateUser(userAccount);
        }
    }
    return "admin";
}

This works fine, except the page is not being refresh with the new model. I have to press F5 and then I can see that the user h开发者_JS百科as been disabled. I really dont want to return anything in my deactivateUser method, just update the model of the current page. Whats the best way to do this?


Since this is an ajax request, you have to update your page with ajax (jQuery as it seems). You can simply return true / false to indicate that deactivation is succsesful.

0

精彩评论

暂无评论...
验证码 换一张
取 消