简体   繁体   中英

How to get a page not redirect to the main

I'm absolute beginner at JSP and Spring framework and annotations. I've built a standard CRUD application, but have one issue. Whenever i want add , edit or delete a user, application redirects me to main "user" page, if i'm on a page, with an index more than 1. How can i make it stay still on a page, where i do all actions above?

For example, when i want to edit "Tom Hanks"(14, page number 2)

It redirects me to the 1st page

But i want it to be still on the same page, where i add , edit or delete

UserController class is below

@Controller
public class UserController
{
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService)
    {
        this.userService = userService;
    }

    @RequestMapping(value = "users", method = RequestMethod.GET)
    public String listUsers(@RequestParam(value="page", required = false) Long page, Model model) {
        if (null == page)
            page = 1L;
        model.addAttribute("user", new User());
        model.addAttribute("searcheduser", new User());
        model.addAttribute("listUsers", userService.getUsers(page));
        model.addAttribute("page", page);

        return "users";
    }

    @RequestMapping(value = "/users/add", method = RequestMethod.POST)
    public String addUser(@ModelAttribute("user") User user) {
        if(user.getId() == 0)
        userService.addUser(user);
        else
            {
                userService.updateUser(user);
            }
        return "redirect:/users";
    }

    @RequestMapping("/remove/{id}")
    public String removeUser(@PathVariable("id") int id)
    {
        userService.removeUser(id);
        return "redirect:/users";
    }

    @RequestMapping("edit/{id}")
    public String editUser(@PathVariable("id") int id, @RequestParam(value="page", required = false) Long page, Model model) {
        if (null == page) page = 1L;
        model.addAttribute("user", userService.getUser(id));
        model.addAttribute("searcheduser", new User());
        model.addAttribute("listUsers", userService.getUsers(page));
        model.addAttribute("page", page);

        return "users";
    }

    @RequestMapping("userdata/{id}")
    public String userData(@PathVariable("id") int id, Model model) {
        model.addAttribute("user", userService.getUser(id));

        return "userdata";
    }


    @RequestMapping(value="searchresults", method = RequestMethod.POST)
    public String searchResults(@ModelAttribute("searcheduser") User user, Model model) {
        List<User> searchResult = userService.getUsers(user.getName());
        model.addAttribute("listUsers", searchResult);

        return "searchresults";
    }
}

Add the page as a parameter on redirect for remove and edit :

@RequestMapping("edit/{id}")
public String editUser(@PathVariable("id") int id, @RequestParam(value="page", required = false) Long page, Model model) {
    if (null == page) page = 1L;
    model.addAttribute("user", userService.getUser(id));
    model.addAttribute("searcheduser", new User());
    model.addAttribute("listUsers", userService.getUsers(page));
    model.addAttribute("page", page);

    return "redirect:/users?page="+page;
}

The same for remove .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM