Spring MVC forwarding HTTP POST request to GET req

2019-06-23 20:14发布

问题:

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.

回答1:

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.



回答2:

@Midnight Blue...

Change return type to return "forward:/prepare";