Redirect from spring controller with post parameter

18,140

Solution 1

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");
}

Solution 2

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"; 
}
Share:
18,140

Related videos on Youtube

dmay
Author by

dmay

Updated on June 04, 2022

Comments

  • dmay
    dmay almost 2 years

    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.

  • Betlista
    Betlista about 12 years
    Why post is not possible? I have problem with get when I have collection as parameter generated URL is then: http://localhost:8080/myApp/A/result.form?parameters=SomeNam‌​e&parameters=SoemFie‌​ld which leads to parameters=SomeName. Any idea?
  • aweigold
    aweigold about 12 years
    When you advise the browser to redirect, as is stated in this question, there is nowhere to define which http verb is being used. It will always be a GET. If you need to POST, you will need to deploy a javascript client to do so, and you will need to make sure you are not doing cross site scripting.