有没有办法将带有其他参数的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之前可以向请求添加一些参数.
有没有办法做到这一点?
解决方法:
您可以尝试,如果这是您想要的
@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";
}