Freemarker模板错误:null或丢失 [英] Freemarker template error: null or missing

查看:473
本文介绍了Freemarker模板错误:null或丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void main(String[] args) throws IOException {
    port(8080);

    Configuration config  = new Configuration(Configuration.VERSION_2_3_26);
    config.setDirectoryForTemplateLoading(new File("PATH_NAME"));



    get("/test", (req,res) ->{
        StringWriter writer = new StringWriter();
        Template temp = config.getTemplate("loginform.ftl");
        temp.process(null, writer);
        return writer;
    });


    post("/select", (req,res) -> {
        String city = req.queryParams("city");
        String state = req.queryParams("state");


        Map<String, Object> data = new HashMap<>();

        data.put("Hello", "Your not null!");

        StringWriter writer = new StringWriter();
        Template temp = config.getTemplate("result.ftl");

        temp.process(data, writer);

        return writer;

        });

}

以上是我正在开发的Spark应用程序的主要方法.它涉及两个模板,loginform.ftl和result.ftl. Loginform.ftl是一个简单的html表单,它向服务器发送发布请求,该请求由上述代码中的发布处理程序处理.当我填写表格并发送请求时,出现500个内部服务器错误.该错误与result.ftl有关,目前,我正在使用此结果来测试模板制作.我正在将HashMap传递给result.ftl模板.我得到的错误是:

Above is the main method to the Spark application i am developing. It involves two templates, loginform.ftl and result.ftl. Loginform.ftl is a simple html form that sends a post request to the server which handled by the post handler in the code above. When I fill out the form and send the request, i get a 500 internal server error. The error relates to the result.ftl which, right now, i am using to test template making. I am passing a HashMap to the result.ftl template. The error i get is:

FreeMarker template error:
The following has evaluated to null or missing:
==> data  [in template "result.ftl" at line 2, column 8]


FTL stack trace ("~" means nesting-related):
    - Failed at: #list data as key, value  [in template "result.ftl" at line 
2, column 1]
---- 

我认为这是指在生成模板时数据为null,但显然不是.我不知道这正在发生.我的模板文件在下面.
loginform.ftl

I took this to mean that data was null when the template was being generated , but it very clearly isn't. I have no idea this is happening. My template files are below.
loginform.ftl

<form action= "/select" method= "POST" accept-charset="utf-8">

  City Name: <input type= "text"  name = "city">

  State(2 letter format):<input type= "text"  name = "state">

 <input type= "submit" id = "submitButton">

</form>

result.ftl

result.ftl

<html>
  <#list data as key, value>
    ${key} = ${value};
  </#list>
</html>

推荐答案

错误消息正确.在您的Java代码中,data用作数据模型的根.根本身不是顶级变量,而是顶级变量的容器.因此,例如${Hello}将起作用,并显示"Your not null!". (还要注意,数据"只是一个局部变量名,在Java编译过程中就消失了,并且您永远不要将数据"变量名传递给FreeMarker.)因此,您应该制作一个root Map(或bean) ,将data Map(或bean)放入其中,然后将root传递给Template.process.

The error message is right. In your Java code data is used as the data-model root. The root itself is not a top-level variable, but the container of the top-level variables. So for example ${Hello} would work, and print "Your not null!". (Also note that "data" is just a local variable name, which is gone during Java compilation, and you never pass the "data" variable name to FreeMarker.) So you should make a root Map (or bean), put the data Map (or bean) into it, and pass root to Template.process.

更新:也就是说,现在您拥有temp.process(data, writer);的地方,您应该拥有:

Update: That is, where now you have temp.process(data, writer);, you should have:

Map<String, object> root = new HashMap<>();
root.put("data", data);

temp.process(root, writer);

这篇关于Freemarker模板错误:null或丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆