Java源码示例:spark.template.velocity.VelocityTemplateEngine

示例1
public static void main(String[] args) {

        get("/hello", (request, response) -> {
            Map<String, Object> model = new HashMap<>();
            model.put("message", "Hello Velocity");
            return new ModelAndView(model, "hello.vm"); // located in the resources directory
        }, new VelocityTemplateEngine());

    }
 
示例2
public WebServer(IServerConfiguration config) {

        //Velocity config
        Properties properties = new Properties();
        properties.setProperty("file.resource.loader.path", "templates/");
        VelocityTemplateEngine templateEngine = new VelocityTemplateEngine(new VelocityEngine(properties));
        Spark.staticFiles.externalLocation("static");

        //Spark config
        if (config.getInt("use_ssl") != 0) {

            /*
             * Generate keystore from Let's Encrypt with command:
             * openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out keystore.jks -name muchassemblyrequired -CAfile chain.pem -caname root -password file:password.txt
             *
             * Certificates generated from Let's Encrypt are usually in /etc/letsencrypt/live/www.site.com
             */
            Spark.secure(
                    config.getString("keyStore_path"),
                    config.getString("keyStore_password"), null, null);
            LogManager.LOGGER.info("(Web) Enabled ssl");
        }

        initGuestPolicy(config);

        socketServer = new SocketServer(guestPolicy);

        Spark.webSocket("/socket", socketServer);

        Spark.get("/", new HomePage(), templateEngine);
        Spark.get("/leaderboard", new LeaderBoardPage(), templateEngine);
        Spark.get("/play", new PlayPage(), templateEngine);
        Spark.get("/account", new AccountPage(), templateEngine);

        Spark.post("/register", new RegisterRoute());
        Spark.post("/login", new LoginRoute());
        Spark.get("/logout", new LogoutRoute());
        Spark.post("/change_password", new ChangePasswordRoute());
        Spark.get("/server_info", new ServerInfoRoute());
        Spark.post("/floppy_upload", new FloppyUploadRoute());
        Spark.get("/floppy_download", new FloppyDownloadRoute());

        Spark.after((request, response) -> response.header("Content-Encoding", "gzip"));
    }