我将 Spark Java Web 框架与 Apache 的 Velocity 模板引擎一起使用,以帮助设计一个从 SQL 数据库中提取数据的响应式 Web 应用程序。使用 SQL2o,我创建了一些自定义类类型的 Java 对象,即用户、组、站点等。
我已经检查并填充了创建的对象列表。然后,当我将对象列表放入哈希图中并返回 ModelandView 时,由于某种原因,我的列表在那里,但我不能在 vtl.xml 中使用它的任何属性。
main方法和Spark代码的相关部分:
public static void main(String[] args) {
WEB_LOGMGR loggr = new WEB_LOGMGR(true);
WEB_DBMGR dbmgr = new WEB_DBMGR(true, loggr);
Model backend = new ScadaModel(dbmgr, loggr);
System.out.println(dataToJson(backend.getUsers()));
staticFiles.location("/");
staticFiles.externalLocation("/");
String layout = "/templates/layout.vtl";
//secure("public/keystore.jks", "password", null, null);
before("/form", (request, response) -> {
boolean authenticated = false;
// ... check if authenticated
if (!authenticated) {
halt(401, "You are not welcome here");
}
});
get("/", (req, res) -> {
HashMap pdata = new HashMap();
pdata.put("template", "/templates/main.vtl");
return new ModelAndView(pdata, layout);
}, new VelocityTemplateEngine());
get("/users", (req, res) -> {
HashMap pdata = new HashMap();
pdata.put("template", "/templates/users.vtl");
pdata.put("users", backend.getUsers());
return new ModelAndView(pdata, layout);
}, new VelocityTemplateEngine());
Parsed User VTL 的相关部分
<div class="w3-row-padding w3-margin-bottom">
<div class="w3-container">
<h5>SCADA Users</h5>
<ul class="w3-ul w3-card-4 w3-white">
#foreach( $user in $users )
<li class="w3-padding-16">
<img src="/images/cole.jpg" class="w3-left w3-circle w3-margin-right" style="width:35px">
<span class="w3-xlarge">$user.firstName</span><br>
<!-- The above line should return a name for my 3 users, but it doesn't. Removing the .firstname
allows the code to run but it just returns User@ and then a memory location -->
</li>
#end
</ul>
</div>
</div>
Layout VTL 的相关部分
<!-- Overlay effect when opening sidebar on small screens -->
<div class="w3-overlay w3-hide-large w3-animate-opacity" onclick="w3_close()" style="cursor:pointer" title="close side menu" id="myOverlay"></div>
<!-- !PAGE CONTENT! -->
#parse( $template )