Skip to content

Instantly share code, notes, and snippets.

@desireesantos
Last active December 31, 2015 09:18
Show Gist options
  • Save desireesantos/7965657 to your computer and use it in GitHub Desktop.
Save desireesantos/7965657 to your computer and use it in GitHub Desktop.
Using Configuration.class to set a personal folder struct to put .html files (In the end of class there is a struct of folder, that you can follow or create yours.)
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import spark.Request;
import spark.Response;
import spark.Route;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import static spark.Spark.get;
import static spark.Spark.setPort;
import static spark.Spark.staticFileLocation;
public class HelloWorld {
public static void main(String[] args) {
final Configuration configuration = new Configuration();
configuration.setClassForTemplateLoading(HelloWorld.class, "/");
setPort(8000);
staticFileLocation("/public");
get(new Route("/") {
@Override
public Object handle(final Request request, final Response response) {
Writer stringWriter = new StringWriter();
Map<String, Object> attributes = new HashMap<>();
attributes.put("name", "Start Project");
try {
Template template = configuration.getTemplate("/public/template/index.html");
template.process(attributes, stringWriter);
} catch (TemplateException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return stringWriter;
}
});
}
}
// FOLDER STRUCT:
// Resources
// --public
// --template
// --index.html
@desireesantos
Copy link
Author

After running go to browser and type: localhost:8000/index

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment