Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC forwarding HTTP POST request to GET request handler in another controller

I am trying to get my controller to forward a POST request to another controller with some parameters:

@RequestMapping(method=RequestMethod.POST)
public String processSubmit(@Valid Voter voter, BindingResult result, 
      //...
      request.setAttribute("firstName", voter.getFirstName());
      request.setAttribute("lastName", voter.getLastName());
      request.setAttribute("ssn", voter.getSsn());
      logger.info("VoterID exists, forwarding to /question/prepare");
      return "forward:/question/prepare";

The problem that I am facing is that /question/prepare points to a Controller method that handles only HTTP GET requests.

@RequestMapping(value="/prepare", method=RequestMethod.GET)
public String prepareVoterBean(@RequestParam String firstName, 
        @RequestParam String lastName, @RequestParam String ssn, Model model) {
    logger.info("QuestionController got GET REQUEST for " + firstName + lastName + ssn);
    VoterBean bean = new VoterBean();
    bean.setFirstName(firstName);
    bean.setLastName(lastName);
    bean.setSsn(ssn);

    model.addAttribute("questions",bean);
    return "questionPage";
}

Is there a way to forward the request to prepareVoterBean as a HTTP GET request? Thanks.

like image 329
Midnight Blue Avatar asked Nov 03 '22 01:11

Midnight Blue


1 Answers

Is there a way to forward the request to prepareVoterBean as a HTTP GET request?

Try using redirect: prefix.


return "forward:/question/prepare";

This is not POST. The following link might be useful: "22.5.3 Redirecting to views" section.

like image 110
Vitaly Avatar answered Nov 14 '22 23:11

Vitaly