有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我在传递给freemarker的哈希映射上得到一个null值,而它不是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和结果。ftl。登录表单。ftl是一个简单的html表单,它向服务器发送post请求,由上面代码中的post处理程序处理。当我填写表单并发送请求时,我得到一个500内部服务器错误。错误与结果有关。ftl,现在,我用它来测试模板制作。我正在将哈希映射传递给结果。ftl模板。我得到的错误是:

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]
---- 

我认为这意味着生成模板时数据为空,但很明显不是。我不知道会发生这种情况。我的模板文件如下
登录表单。超光速

<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>

结果。超光速

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

共 (0) 个答案