【发布时间】:2015-03-03 17:02:35
【问题描述】:
我要做什么
我有一个 ControllerA,它需要将带有参数的请求转发或重定向(首选重定向)到 ControllerB。 ControllerB 在一个完全不同的 webapp 中,所以除了重定向之外,这些控制器都不知道另一个存在。
ControllerB 返回 JSON 或 XML 格式的视图。我想要发生的是 ControllerA 转发/重定向到 ControllerB,并显示从 ControllerB 生成的视图。
我这样做是因为给我的要求表明我应该尽量避免将 ControllerB 的应用程序中的任何内容集成到 ControllerA 的应用程序中。否则我会使用 RestTemplate 并将 ControllerB 的视图复制到 ControllerA 的应用程序中。
我目前所拥有的
这是控制器A:
@RequestMapping("/ControllerA")
public ModelAndView ControllerA(
@RequestParam(value = "A", required = true) String A,
@RequestParam(value = "B", required = false) Boolean B)
{
RedirectAttributes redirectAttributes = new RedirectAttributesModelMap();
redirectAttributes.addAttribute("A", A);
redirectAttributes.addAttribute("B", B);
String obj = "redirect:http://{host}:{port}/{appName}/ControllerB?A=A&B=B";
return new ModelAndView(obj);
}
这是控制器B:
@RequestMapping("/ControllerB")
public ModelAndView ControllerB(
@RequestParam(value = "A") String A,
@RequestParam(value = "B", required = false) Boolean B
)
{
ControllerView cView
= serviceService.service(A, B);
return new ModelAndView("viewResolver", "object", cView);
}
我想要找回的示例
<Results A="A" B="B" numResultsReturned="1">
<line data1="ABC" />
</Results>
什么不起作用
视图永远不会在重定向或转发时返回。 Spring 报告状态为 200 的转发或重定向 URL,然后尝试解析为 JSP。我可能做错了什么?
我在 SO 和网络上的其他地方看到的大多数问题似乎都对 JSP 或 HTML 页面使用了重定向/转发。我只是想获取 XML 或 JSON。
我知道我可能在这里要求 Spring 做一些它不应该做的事情。如果是这种情况,如果我想让 ControllerA 和 ControllerB 尽可能分开,我还有哪些其他选择?
【问题讨论】:
-
“我想要发生的是 ControllerA 转发/重定向到 ControllerB,并显示从 ControllerB 生成的视图”所以您希望 webapp A 生成一个视图,其中包含您从控制器 B 获得的结果?
-
@JohnDonn 我不希望 webapp A 生成任何东西。 webapp B 中的 ControllerB 以 XML 或 JSON 格式生成视图。我想要的是让 webapp A 能够获取在 ControllerB 中创建的视图并将其传递给客户端。