<dependency> <groupId>com.sparkjava</groupId> <artifactId>spark-core</artifactId> <version>2.6.0</version> </dependency>
import static spark.Spark.*; public class Main { public static void main(String[] args) { get("/hello", (req, res) -> "Hello, World!"); } }
<dependency> <groupId>com.sparkjava</groupId> <artifactId>spark-template-freemarker</artifactId> <version>2.5.5</version> </dependency>
<html> <head> </head> <body> <h1>Hello, ${name}!</h1> </body> </html>
FreeMarkerEngine freeMarkerEngine = new FreeMarkerEngine(); Configuration freeMarkerConfiguration = new Configuration(); freeMarkerConfiguration.setTemplateLoader(new ClassTemplateLoader(Main.class, "/")); freeMarkerEngine.setConfiguration(freeMarkerConfiguration);
get("/", (request, response) -> { Map<String, Object> model = new HashMap<>(); model.put("name", "Freemarker"); return freeMarkerEngine.render(new ModelAndView(model, "hello.ftl")); });
<dependencies> <dependency> <groupId>com.sparkjava</groupId> <artifactId>spark-core</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>com.sparkjava</groupId> <artifactId>spark-template-freemarker</artifactId> <version>2.5.5</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version> </dependency>
staticFileLocation("/static"); get("/shortener", (request, response) -> { }); get("/:url", (request, response) -> { });
get("/hello/:name", (request, response) -> { return "Hello: " + request.params(":name"); });
ConcurrentHashMap<String, String> urls = new ConcurrentHashMap<String, String>(); FreeMarkerEngine freeMarkerEngine = new FreeMarkerEngine(); Configuration freemarkerConfiguration = new Configuration(); freemarkerConfiguration.setTemplateLoader(new ClassTemplateLoader(Main.class, "/templates/")); freeMarkerEngine.setConfiguration(freemarkerConfiguration); staticFileLocation("/static");
get("/:url", (request, response) -> { if(urls.containsKey(request.url())) response.redirect(urls.get(request.url())); response.redirect("/"); return null; });
<!DOCTYPE html> <html lang="en" style="height:100%;"> <head> <meta charset='utf-8'> <link rel="stylesheet" href="style.css"> </head> <body> <div id="main"> <form action="/shortener" method="GET"> <input type="text" id="input" name="url" autocomplete="off" autofocus size="44" maxlength="512" /> </form> </div> </body> </html>
@font-face { font-family: Proxima Nova; src: url(pn.otf); } * { font-family: Proxima Nova; background: #2a2826; color: #9C9C9C; margin: 0; padding: 0; } body, html { height: 100%; } #main { display: flex; align-items: center; justify-content: center; height: 100%; font-size: 1.5em; } #input { border: none; outline:none; font-size: 1.5em; }
<!DOCTYPE html> <html lang="en" style="height:100%;"> <head> <meta charset='utf-8'> <link rel="stylesheet" href="style.css"> </head> <body> <div id="main" style="text-align: center"> ${url} </div> </body> </html>
import com.google.common.hash.Hashing; import freemarker.cache.ClassTemplateLoader; import freemarker.template.Configuration; import spark.ModelAndView; import spark.Request; import spark.Response; import spark.template.freemarker.FreeMarkerEngine; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import static spark.Spark.*; public class Main { public static void main(String[] args) { ConcurrentHashMap<String, String> urls = new ConcurrentHashMap<String, String>(); FreeMarkerEngine freeMarkerEngine = new FreeMarkerEngine(); Configuration freemarkerConfiguration = new Configuration(); freemarkerConfiguration.setTemplateLoader(new ClassTemplateLoader(Main.class, "/templates/")); freeMarkerEngine.setConfiguration(freemarkerConfiguration); staticFileLocation("/static"); get("/shortener", (request, response) -> { String shortURL = "http://localhost:4567/" + Hashing.murmur3_32().hashString(request.queryParams("url"), StandardCharsets.UTF_8).toString(); Map<String, Object> model = new HashMap<>(); if(!urls.containsKey(shortURL)) { model.put("url", shortURL); urls.put(shortURL, request.queryParams("url")); return freeMarkerEngine.render(new ModelAndView(model, "shortener.ftl")); } model.put("url", shortURL); return freeMarkerEngine.render(new ModelAndView(model, "shortener.ftl")); }); get("/:url", (request, response) -> { if(urls.containsKey(request.url())) response.redirect(urls.get(request.url())); response.redirect("/"); return null; }); } }
Source: https://habr.com/ru/post/339684/
All Articles