1

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 ?

akash
  • 22,664
  • 11
  • 59
  • 87
Ukster
  • 11
  • 2
  • I dont know if its good or not, but you can send your teamId as hidden value when deleteTeam/{id} is getting called. Extract that hidden id using @RequestParam then use `redirect /team/teamMembers/{idRetrieved}`. – SachinSarawgi Dec 19 '16 at 13:34

1 Answers1

0

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.

İlker Korkut
  • 3,129
  • 3
  • 30
  • 51