dmay
dmay

Reputation: 1315

Redirect from spring controller with post parameter

I want to redirect to another page(outside my application) from spring controller with post parameter. I search a lot but didn't any solution.

Upvotes: 9

Views: 19829

Answers (3)

leandro lion
leandro lion

Reputation: 71

easy way for to do:

    @PostMapping("/redirectPostToPost")
public ModelAndView redirectPostToPost(HttpServletRequest request) {
    request.setAttribute(
      View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
    return new ModelAndView("redirect:/redirectedPostToPost");
}
@PostMapping("/redirectedPostToPost")
public ModelAndView redirectedPostToPost() {
    return new ModelAndView("redirection");
}

Upvotes: 0

Moinul Hossain
Moinul Hossain

Reputation: 2206

Do something like this

@RequestMapping(value="/someUrl",method=RequestMethod.POST)
public String myFunc(HttpServletRequest request,HttpServletResponse response,Map model){
    //do sume stuffs
     return "redirect:/anotherUrl"; //gets redirected to the url '/anotherUrl'
}

@RequestMapping(value="/anotherUrl",method=RequestMethod.GET)
public String myAnotherFunc(HttpServletRequest request,HttpServletResponse response){
    //do sume stuffs
     return "someView"; 
}

Upvotes: 1

aweigold
aweigold

Reputation: 6879

You will not be able to add a POST, but you can redirect with GET. Do the following:

@RequestMapping("/redirectMe")
public void redirectMe (HttpServletResponse response){
    response.sendRedirect("http://redirected.com/form?someGetParam=foo");
}

Upvotes: 5

Related Questions