如何让页面不重定向到主页面

时间:2017-07-01 18:32:58

标签: java spring jsp model-view-controller crud

我是JSP和Spring框架和注释的绝对初学者。我已经构建了一个标准的CRUD应用程序,但有一个问题。每当我想要添加编辑删除用户时,应用程序会将我重定向到主用户"用户"页面,如果我在一个页面上,索引超过1.如何让它保持在页面上,我在上面执行所有操作?

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

It redirects me to the 1st page

但我希望它仍然在同一页面上,我添加修改删除

UserController类位于

之下
@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";
    }
}

1 个答案:

答案 0 :(得分:0)

page添加为重定向参数以进行删除和编辑:

@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;
}

同样的删除。