繁体   English   中英

如何在Spark Web Framework和Velocity Template Framework中进行for循环?

[英]How can I make a for loop in Spark Web Framework and the Velocity Template Framework?

这是我的Main.java

public class Main {
    public static void main(String[] args) {

        // Create some students
        Student students[] = new Student[4];

        students[0] = new Student("Abe");
        students[1] = new Student("Bill");
        students[2] = new Student("Chris");
        students[3] = new Student("Darrel");

        staticFileLocation("/public");

        String layout = "templates/layout.vtl";

        get("/", (request, response) -> {
            HashMap model = new HashMap();
            model.put("template", "templates/home.vtl" );
            return new ModelAndView(model, layout);
        }, new VelocityTemplateEngine());

        get("/view_students", (request, response) -> {
            HashMap model = new HashMap();

            model.put("students", students );
            // model.put("student", new Student() );

            return new ModelAndView(model, "templates/view_students_layout.vtl");
        }, new VelocityTemplateEngine());

    }
}

这是view_students_layout.vtl

<!DOCTYPE html>
<html>
  <head>
    <title>Hello Friend!</title>
    <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'>
  </head>
  <body>
    <div class="container">

    <h1>Students</h1>

    <ul>

      #foreach( $Student in $students )
        <li>${Student.name}</li>
      #end

    </ul>

    </div>
  </body>
</html>

当我运行spark时,我得到以下信息

<!DOCTYPE html>
<html>
  <head>
    <title>Hello Friend!</title>
    <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'>
  </head>
  <body>
    <div class="container">

    <h1>Students</h1>

    <ul>

              <li>${Student.name}</li>
              <li>${Student.name}</li>
              <li>${Student.name}</li>
              <li>${Student.name}</li>

    </ul>

    </div>
  </body>
</html>

我想念或误解了什么? 我是否以错误的方式将数组发送到框架?

谢谢。

Student类必须具有public String getName()方法或public String get(String key)方法。 您正在尝试直接访问name字段,或者忘记了公开其访问器。

如果要直接向模板公开公共字段,则需要2.0.0-SNAPSHOT版本(开发版本)。 请参阅http://velocity.apache.org/engine/devel/developer-guide.html部分可插拔自省

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM