使用其他参数转发POST请求

时间:2013-11-21 03:28:34

标签: spring spring-mvc post forward

是否有办法将POST请求从一个控制器转发到另一个控制器并使用其他参数?

假设我有一个这样的表格:

<form action"${contextPath}/controller1/post">
  <input name="field1" type="text"/>
  <input name="field2" type="text"/>
  <input value="submit" type="submit"/>
</form>

此表单将发布到controller1.post()方法。

但是现在我有另一个控制器 - controller2也使用post方法。 我现在想发布到controller2.post,这样我就可以在转发到controller1之前为请求添加一些参数。 有没有办法做到这一点?

1 个答案:

答案 0 :(得分:4)

如果您正在寻找

,可以试试
@RequestMapping(value = "/controller1/{id}", method = RequestMethod.Post)
public void doSomething(
        @PathVariable Long id, 
        HttpServletRequest request, 
        HttpServletResponse response) {

     request.setAttribute("id",Id);

     RequestDispatcher rd = request.getRequestDispatcher("your url/controller2");
     rd.forward(request, response);
}

在controller2之后

@RequestMapping(value = "/controller2", method = RequestMethod.Post)
public string doSomething2(Model model,       
        HttpServletRequest request, 
        HttpServletResponse response) {

     model.addAttribute("id", request.getAttribute("id"));

    return "myView";
}
相关问题