在木兰中使用模型
Using models in 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, Node node) {
model.addAttribute("meta", new MetaModel(node));
}
}
我希望能够将 MetaModel 与 TemplatingFunctions
和其他 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");
}
}
关于如何获取要注入的模板函数有什么想法吗?
而不是new MetaModel(node)
,使用
info.magnolia.objectfactory.Components.newInstance(MetaModel.class, node)
为了创建模型的新实例。 TemplatingFunctions
会自动注入。
另一种选择是将 TemplatingFunctions
作为 Spring bean 暴露在 @Configuration
class:
中的某处
@Bean
public TemplatingFunctions templatingFunctions() {
return Components.getComponent(TemplatingFunctions.class);
}
并在您的 Spring 控制器中自动装配 bean,并向 MetaModel
添加一个新的构造函数 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));
}
}
假设我有如下控制器:
@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));
}
}
我希望能够将 MetaModel 与 TemplatingFunctions
和其他 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");
}
}
关于如何获取要注入的模板函数有什么想法吗?
而不是new MetaModel(node)
,使用
info.magnolia.objectfactory.Components.newInstance(MetaModel.class, node)
为了创建模型的新实例。 TemplatingFunctions
会自动注入。
另一种选择是将 TemplatingFunctions
作为 Spring bean 暴露在 @Configuration
class:
@Bean
public TemplatingFunctions templatingFunctions() {
return Components.getComponent(TemplatingFunctions.class);
}
并在您的 Spring 控制器中自动装配 bean,并向 MetaModel
添加一个新的构造函数 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));
}
}