Java 如何将一个DropWizard(带freemarker)视图嵌入到另一个视图中?
我正在使用DropWizard和Freemarker构建一个视图,该视图根据Web服务的结果显示不同类型的表单 我已经创建了作为视图的表单-每个表单都有自己的ftl 因此,在我的资源中,我发现我需要哪个表单,然后加载main.ftl,将表单视图作为参数传递(见下文) 这不管用。有人能看出我们哪里出了问题吗?或者是否有一种完全不同的方式使用DropWizard和freemarker将视图链接在一起Java 如何将一个DropWizard(带freemarker)视图嵌入到另一个视图中?,java,templates,freemarker,dropwizard,Java,Templates,Freemarker,Dropwizard,我正在使用DropWizard和Freemarker构建一个视图,该视图根据Web服务的结果显示不同类型的表单 我已经创建了作为视图的表单-每个表单都有自己的ftl 因此,在我的资源中,我发现我需要哪个表单,然后加载main.ftl,将表单视图作为参数传递(见下文) 这不管用。有人能看出我们哪里出了问题吗?或者是否有一种完全不同的方式使用DropWizard和freemarker将视图链接在一起 @GET public Form getForm() { FormView view
@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>
@GET
公共表单getForm(){
FormView视图=新的FormView(service.getForm());
返回新的主视图(视图);
}
公共类FormView扩展了视图{
私人最终形式;
公共表单视图(表单){
超级(“formView.ftl”);
this.form=形式;
}
公共表单getForm(){
申报表;
}
}
公共类MainView扩展了视图{
私有最终FormView FormView;
公共主视图(FormView FormView){
超级(“main.ftl”);
this.formView=formView;
}
public FormView getFormView(){
返回表单视图;
}
}
公共类TextForm扩展视图实现表单{
私有整数最大长度;
私有字符串文本;
公共文本格式(int-maxLength,字符串文本){
超级(“textForm.ftl”);
this.maxLength=maxLength;
this.text=文本;
}
public int getMaxLength(){
返回最大长度;
}
公共字符串getText(){
返回文本;
}
}
main.ftl
//这将计算为formView.ftl,但它似乎创建了一个新实例,并且没有对textForm.ftl的引用。我们如何在此处引用dropwizard视图?
formView.ftl
${form?html}//也尝试了#include form.templateName
textForm.ftl
${text?html}
根据讨论,我认为您需要这样的东西:
<#-- main.ftl -->
<html>
<body>
<#include formView.templateName>
</body>
</html>
formView.templateName
必须计算为textForm.ftl
、numberForm.ftl
、complexForm.ftl
或任何您可能有的表单视图。不需要中间文件在这些文件之间进行选择。我认为您遇到了问题,因为FormView.getTemplateName()
正在返回硬编码的FormView.ftl
。我认为这个方法需要的是返回包含要显示的表单类型的实际模板文件的名称。我已经找到了方法
您创建了一个TemplateView
类(见下文),该类扩展了View
然后,所有视图类都需要扩展
TemplateView
构造函数接受一个额外的参数,即“body”模板文件,即模板内部的主体
然后,在模板文件中,执行如下操作
<html>
<body>
<h1>My template file</h1>
<#include body>
<footer>footer etc</footer>
</body>
</html>
希望这对某人有所帮助。在
main.ftl
中,formView
需要计算为formView.ftl
或textView.ftl
。从您的代码片段中我不清楚这种情况,但我不熟悉DropWizard。谢谢。使用计算结果为formView.ftl,但它似乎创建了一个新实例,并且没有对textForm.ftl的引用。我的理解是,根据formView.templateName
的值,您想选择formView.ftl
或textForm.ftl
,但您想同时选择两者吗?或者是textView.ftl
在您预期的情况下没有被选择?我很困惑。我希望formView.ftl是一个容器,包括textForm.ftl或(例如)numberForm.ftl、complexForm.ftl或lazyForm.ftl,具体取决于从Web服务返回的内容。因此,模板嵌入有三个级别:mainView.ftl包含一个FormView,其中包含TextForm/NumberForm/ComplexForm/LazyForm
public abstract class TemplateView extends View
{
private final String body;
protected TemplateView(String layout, String body)
{
super(layout);
this.body = resolveName(body);
}
public String getBody()
{
return body;
}
private String resolveName(String templateName)
{
if (templateName.startsWith("/"))
{
return templateName;
}
final String packagePath = getClass().getPackage().getName().replace('.', '/');
return String.format("/%s/%s", packagePath, templateName);
}
}