问题描述
我正在使用DropWizard和Freemarker构建一个视图,该视图根据Web服务的结果显示不同类型的表单。
I am using DropWizard and Freemarker to build up a view which displays different types of forms based on results from a webservice.
我已经将表单创建为视图 - 每个都有自己的ftl。
I have created the forms as views - each with their own ftl.
所以,在我的资源中,我发现我需要哪种形式,然后加载main.ftl,将表单视图作为参数传递(见下文)。
So, in my resource, I discover which form I need, then load the main.ftl, passing the form view as a parameter (see below).
这不起作用。谁能看到我们哪里出错?或者使用DropWizard和freemarker将视图链接在一起是否有完全不同的方式?
This doesn't work. Can anyone see where we're going wrong? Or is there a completely different way to chain views together using DropWizard and freemarker?
@GET
public Form getForm() {
FormView view = new FormView(service.getForm());
return new MainView(view);
}
public class FormView extends View {
private final Form form;
public FormView(Form form) {
super("formView.ftl");
this.form = form;
}
public Form getForm() {
return form;
}
}
public class MainView extends View {
private final FormView formView;
public MainView(FormView formView) {
super("main.ftl");
this.formView = formView;
}
public FormView getFormView() {
return formView;
}
}
public class TextForm extends View implements Form {
private int maxLength;
private String text;
public TextForm(int maxLength, String text) {
super("textForm.ftl");
this.maxLength = maxLength;
this.text = text;
}
public int getMaxLength() {
return maxLength;
}
public String getText() {
return text;
}
}
main.ftl
<#-- @ftlvariable formView="" type="MainView" -->
<html>
<body>
<#include formView.templateName /> // This evaluates to formView.ftl, but it seems to create a new instance, and doesn't have the reference to textForm.ftl. How do we reference a dropwizard view here?
</body>
</html>
formView.ftl
<#-- @ftlvariable form type="FormView" -->
${form?html} // also tried #include form.templateName
textForm.ftl
<#-- @ftlvariable text type="TextForm" -->
<div>${text?html}</div>
推荐答案
根据讨论,我认为你需要这样的东西:
Based on the discussion, I think you need something like this:
<#-- main.ftl -->
<html>
<body>
<#include formView.templateName>
</body>
</html>
formView.templateName
必须评估为 textForm.ftl
, numberForm.ftl
, complexForm.ftl
或其他您可能拥有的表单视图。不需要在这些文件之间选择的中间文件。我认为你遇到了问题,因为 FormView.getTemplateName()
正在返回一个硬编码的 formView.ftl
。我想你需要的是这个方法返回包含你想要显示的表单类型的实际模板文件的名称。
formView.templateName
must evaluate to textForm.ftl
, numberForm.ftl
, complexForm.ftl
or whatever form view you might have. There's no need for an intermediate file that chooses between these. I think you are running into problems because FormView.getTemplateName()
is returning a hard-coded formView.ftl
. I think that what you need is for this method to return the name of the actual template file containing the form type you want to display.
这篇关于如何在另一个中嵌入一个DropWizard(带有freemarker)视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!