如何在 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>
我错过了什么或误解了什么?我是否以错误的方式将数组发送到框架?
谢谢。
学生 class 必须有 public String getName()
方法,或 public String get(String key)
方法。您要么试图直接访问 name
字段,要么忘记将其访问器设置为 public.
如果您想直接将 public 字段公开给模板,那么您需要 2.0.0-SNAPSHOT 版本(开发版本)。请参阅 http://velocity.apache.org/engine/devel/developer-guide.html 部分 可插入内省 。
这是我的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>
我错过了什么或误解了什么?我是否以错误的方式将数组发送到框架?
谢谢。
学生 class 必须有 public String getName()
方法,或 public String get(String key)
方法。您要么试图直接访问 name
字段,要么忘记将其访问器设置为 public.
如果您想直接将 public 字段公开给模板,那么您需要 2.0.0-SNAPSHOT 版本(开发版本)。请参阅 http://velocity.apache.org/engine/devel/developer-guide.html 部分 可插入内省 。