This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Adding Spark and Velocity
sebastianchristopher edited this page Sep 16, 2019
·
1 revision
Dependencies:
compile 'com.sparkjava:spark-core:2.2'
compile group: 'org.apache.velocity', name: 'velocity', version: '1.7'
Copy and paste the following into a new Java file, Velocity Template Engine:
/*
* Copyright 2013 Per Wendel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import spark.ModelAndView;
import spark.TemplateEngine;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
/**
* Template Engine based on Apache Velocity.
*/
public class VelocityTemplateEngine extends TemplateEngine {
private final VelocityEngine velocityEngine;
private String encoding;
/**
* Constructor
*/
public VelocityTemplateEngine() {
Properties properties = new Properties();
properties.setProperty("resource.loader", "class");
properties.setProperty(
"class.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
this.velocityEngine = new VelocityEngine(properties);
}
/**
* Constructor
*
* @param encoding The encoding to use
*/
public VelocityTemplateEngine(String encoding) {
this();
this.encoding = encoding;
}
/**
* Constructor
*
* @param velocityEngine The velocity engine, must not be null.
*/
public VelocityTemplateEngine(VelocityEngine velocityEngine) {
if (velocityEngine == null) {
throw new IllegalArgumentException("velocityEngine must not be null");
}
this.velocityEngine = velocityEngine;
}
/**
* {@inheritDoc}
*/
@Override
public String render(ModelAndView modelAndView) {
String templateEncoding = Optional.ofNullable(this.encoding).orElse(StandardCharsets.UTF_8.name());
Template template = velocityEngine.getTemplate(modelAndView.getViewName(), templateEncoding);
Object model = modelAndView.getModel();
if (model instanceof Map) {
Map<?, ?> modelMap = (Map<?, ?>) model;
VelocityContext context = new VelocityContext(modelMap);
StringWriter writer = new StringWriter();
template.merge(context, writer);
return writer.toString();
} else {
throw new IllegalArgumentException("modelAndView must be of type java.util.Map");
}
}
}
Create a new App.java file:
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import dao.Sql2oToDoDao;
import models.ToDo;
import org.sql2o.Sql2o;
import spark.ModelAndView;
import static spark.Spark.*;
public class App {
public static void main(String[] args) {
String connectionString = "jdbc:postgresql://localhost:5432/toDoList";
Sql2o sql2o = new Sql2o(connectionString, "student", "");
Sql2oToDoDao todoDao = new Sql2oToDoDao(sql2o); // create an instance to call methods on
get("/", (req, res) -> {
Map<String, Object> model = new HashMap<>(); // allows us to pass objects into the vtl template
List<ToDo> todos = todoDao.all();
model.put("todos", todos); // pass all ToDos into template
return new ModelAndView(model, "index.vtl");
}, new spark.template.velocity.VelocityTemplateEngine());
get("/tasks/delete", (req, res) -> {
Map<String, Object> model = new HashMap<>();
todoDao.clearAll();
res.redirect("/");
return null;
};
}
}