com.korpacz.testproject.HelloWorldMongoDBSparkFreemarkerStyle.java Source code

Java tutorial

Introduction

Here is the source code for com.korpacz.testproject.HelloWorldMongoDBSparkFreemarkerStyle.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.korpacz.testproject;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.StringWriter;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.Spark;

/**
 *
 * @author Ask
 */
public class HelloWorldMongoDBSparkFreemarkerStyle {

    public static void main(String... args) throws UnknownHostException {
        //ssssss

        final Configuration config = new Configuration();
        config.setClassForTemplateLoading(HelloWorldSparkFreemarkerStyle.class, "/");

        MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));
        DB database = client.getDB("test");
        final DBCollection collection = database.getCollection("names");

        Spark.get("/", new Route() {

            @Override
            public Object handle(Request rqst, Response rspns) throws Exception {

                Template helloTemplete = config.getTemplate("hello.ftl");
                StringWriter writter = new StringWriter();

                DBObject object = collection.findOne();

                helloTemplete.process(object, writter);

                System.out.println(writter);

                return writter;
            }
        });

    }
}