Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect with POST variables with Spring MVC

Tags:

spring-mvc

I have written the following code:

@Controller
    @RequestMapping("something")
    public class somethingController {
       @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.POST)
      public String myAnotherFunc(HttpServletRequest request,HttpServletResponse response){
        //do sume stuffs
         return "someView"; 
      }
    }

How would i be able to redirect to "anotherUrl" request mapping whose request method is POST?

like image 293
Viraj Dhamal Avatar asked Aug 30 '13 06:08

Viraj Dhamal


1 Answers

A spring Controller methods can be both POST and GET requests.

In your scenario:

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

You want this GET because you are redirecting to it. Hence your solution will be

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

Caution : here, if your method accepts some request parameters by @requestParam,then while redirecting you must pass them.

Simply all attributes required by this method must send while redirecting...

Thank You.

like image 133
Oomph Fortuity Avatar answered Sep 28 '22 03:09

Oomph Fortuity