Java/Spring make new ModelAndView that refers to previous page / page where get request was made

784 views Asked by At

I have some trouble forwarding after a request to a previous url.

I've got a team-page, (.../team/teamMembers/{id}) in which all the team players from a team are listed. There is a delete button, to delete a team player from the team. After this request has been made, i want to return to the team/teamMembers/{id} page again, which has now been edited.

My request looks like this:

@RequestMapping(value = "/deleteTeam/{id}", method = RequestMethod.GET)
public ModelAndView deleteTeam(@PathVariable Integer id) {
    ModelAndView modelAndView = new ModelAndView("/team/teamMembers/{id}");
    memberService.deleteTeam(id);
    List<Member> members = memberService.getMembers();
    modelAndView.addObject("members", members);
    String message = "Member was successfully deleted from team.";
    modelAndView.addObject("message", message);
    return modelAndView;
}

But of course, team/teamMembers/{id} does not work, since {id} doesn't have a value, and is not a good url string. Furthermore, the id from the deleteTeam/{id} is a memberID and not a teamID.

How can I redirect to the page from which the getRequest has been made ?

1

There are 1 answers

0
İlker Korkut On

You need to teamId also while deleting a member from team, because you will redirect again teamMembers page.

// Also get teamId while deleting via path variable or as a parameter in controller
// /deleteTeam/{teamId}/{memberId} may be used
// or more user friendly readable path /team/{teamId}/delete/member/{memberId}
return new ModelAndView("redirect:/team/teamMembers/"+ teamId);

So that method will work as you wish.