Java 在木兰中使用模型
假设我有如下控制器:Java 在木兰中使用模型,java,spring-mvc,magnolia,Java,Spring Mvc,Magnolia,假设我有如下控制器: @Controller @Template(id= HomePageTemplate.ID, title = "Home Page") public class HomePageTemplate { public static final String ID = "project:pages/home-page"; @RequestMapping("/home-page") public String render(Model model, No
@Controller
@Template(id= HomePageTemplate.ID, title = "Home Page")
public class HomePageTemplate {
public static final String ID = "project:pages/home-page";
@RequestMapping("/home-page")
public String render(Model model, Node node) {
model.addAttribute("meta", new MetaModel(node));
}
}
我希望能够将元模型与模板功能
和其他Magnolia项目结合使用,但我不确定如何从该模型内部访问内容图:
public class AbstractModel {
protected Node node;
protected TemplatingFunctions tf;
public AbstractModel(Node node, @Inject TemplatingFunctions tf) {
this.node = node;
this.tf = tf;
}
public function getTitle() {
return tf.get("metaTitle");
}
}
有没有想过如何让模板函数注入?使用
info.magnolia.objectfactory.Components.newInstance(MetaModel.class, node)
以创建模型的新实例<代码>模板功能将自动注入
另一个选项是将模板函数
作为Springbean公开,在@Configuration
类中的某个地方:
@Bean
public TemplatingFunctions templatingFunctions() {
return Components.getComponent(TemplatingFunctions.class);
}
@Controller
@Template(id= HomePageTemplate.ID, title = "Home Page")
public class HomePageTemplate {
@Autowired
private TemplatingFunctions cmsfn;
public String render(Model model, Node node) {
model.addAttribute("meta", new MetaModel(node, cmsfn));
}
}
只需在Spring控制器中自动连接bean,并将新构造函数添加到元模型
类:
@Bean
public TemplatingFunctions templatingFunctions() {
return Components.getComponent(TemplatingFunctions.class);
}
@Controller
@Template(id= HomePageTemplate.ID, title = "Home Page")
public class HomePageTemplate {
@Autowired
private TemplatingFunctions cmsfn;
public String render(Model model, Node node) {
model.addAttribute("meta", new MetaModel(node, cmsfn));
}
}