spring-mvc - Spring mvc:有没有办法调用另一个控制器,获取响应并将其填充到模型属性?
问题描述
我有两个 mvc 样式端点返回模板文件名称作为视图名称,在同一个类路径中:/source 和 /target。source_template 有一个变量,该变量需要由另一个模板的内容填充,比如 target_template。
@RestController
class SomeController {
@GetMapping("/source")
public String source(Model model) {
model.addAttribute("attr1", /*call endpoint /target and add the response of parsed template 'target_template' here */);
return "source_template";
}
@GetMapping("/target")
public String target(Model model) {
model.addAttribute("attr2", "good");
//may be continue the nested invocation n number of times
return "target_template";
}
}
给定 source_template.html: Hai, $attr1 和 target_template.html: 这是 $attr2 的一天
话虽如此,我调用 url /source,我应该得到“嘿,这是美好的一天”。
我可以直接调用 target() 方法,但这不会呈现模板。或者我应该直接使用模板引擎api链接模板文件,放置上下文对象,解析模板并返回字符串,这违背了spring mvc的全部目的。或者我可以使用resttemplate,但这需要一个绝对url,并且性能会受到影响。那么,还有其他方法可以做到吗?
解决方案
推荐阅读
- r - 如何检查日期是否在 R 中按时间顺序排列?
- python - 将 json 保存为单字符串列表的 Python 列表
- python-3.x - Python:ImmutableDenseNDimArray' 对象没有属性 'coeff'
- c - 使用 Readline 按下制表符时禁止打印新提示
- mysql - 限制删除订单数量 SQL Query
- r - 在 R 中,是否可以在文本段落中调用函数并删除中断?
- mysql - 仅获取用户在练习 _id 上的最后一次 trynumber 报告
- python - dict_student[id] = [item[1]] 和 dict_student[id].append(item[1]) 的区别?
- java - 将mysql服务器连接到android应用程序(java)错误
- azure - 最佳实践 - 本地 Node.js (*.env) 到远程 Azure 应用服务(配置设置)