Spring MVC forwarding HTTP POST request to GET req

2019-06-23 20:06发布

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.

2条回答
你好瞎i
2楼-- · 2019-06-23 20:41

@Midnight Blue...

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

查看更多
再贱就再见
3楼-- · 2019-06-23 20:43

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.

查看更多
登录 后发表回答