java Spring MVC 将 HTTP POST 请求转发到另一个控制器中的 GET 请求处理程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15780540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:50:33  来源:igfitidea点击:

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

javaspringjspmodel-view-controller

提问by Midnight Blue

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

我试图让我的控制器使用一些参数将 POST 请求转发到另一个控制器:

@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.

我面临的问题是 /question/prepare 指向一个只处理 HTTP GET 请求的控制器方法。

@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.

有没有办法将请求作为 HTTP GET 请求转发到 prepareVoterBean?谢谢。

采纳答案by Vitaly

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

有没有办法将请求作为 HTTP GET 请求转发到 prepareVoterBean?

Try using redirect:prefix.

尝试使用redirect:前缀。



return "forward:/question/prepare";

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

这不是 POST。以下链接可能有用:“22.5.3 重定向到视图”部分

回答by Srinivas

@Midnight Blue...

@午夜蓝...

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

将返回类型更改为 return "forward:/prepare";