Created
July 1, 2018 07:33
-
-
Save orekyuu/bf3a813d6f668d83341043d06705d452 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%@ page import="java.util.Objects" %> | |
| <%@ page import="java.util.List" %> | |
| <%@ page import="java.io.IOException" %> | |
| <%@ page import="java.util.function.BiConsumer" %> | |
| <%@ page import="java.util.function.Predicate" %> | |
| <%@ page import="java.util.function.Consumer" %> | |
| <%@ page import="java.util.function.Function" %> | |
| <%@ page import="java.util.Arrays" %> | |
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> | |
| <html> | |
| <head> | |
| <title>真・SPA</title> | |
| </head> | |
| <body> | |
| <h1>信じられるか?これ1枚のjspなんだぜ・・・</h1> | |
| <a href="#page1">ページ1</a> | |
| <a href="#page2">ページ2</a> | |
| <% | |
| Cookie[] cookies = request.getCookies(); | |
| final String path = Arrays.stream(cookies) | |
| .filter(new Predicate<Cookie>() { | |
| @Override | |
| public boolean test(Cookie cookie) { | |
| return Objects.equals(cookie.getName(), "path"); | |
| } | |
| }) | |
| .map(new Function<Cookie, String>() { | |
| @Override | |
| public String apply(Cookie cookie) { | |
| return cookie.getValue(); | |
| } | |
| }) | |
| .findFirst() | |
| .orElse("/"); | |
| out.println(path); | |
| class Page { | |
| final String path; | |
| final BiConsumer<JspWriter, ServletRequest> renderer; | |
| public Page(String path, BiConsumer<JspWriter, ServletRequest> renderer) { | |
| this.path = path; | |
| this.renderer = renderer; | |
| } | |
| public boolean isMatch(String path) { | |
| return Objects.equals(this.path, path); | |
| } | |
| } | |
| List<Page> pages = Arrays.asList( | |
| new Page("/", new BiConsumer<JspWriter, ServletRequest>() { | |
| @Override | |
| public void accept(JspWriter jspWriter, ServletRequest servletRequest) { | |
| try { | |
| jspWriter.println("<h1>Top page</h1>"); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| }), | |
| new Page("/page1", new BiConsumer<JspWriter, ServletRequest>() { | |
| @Override | |
| public void accept(JspWriter jspWriter, ServletRequest servletRequest) { | |
| try { | |
| jspWriter.println("<h1>Page1</h1>"); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| }), | |
| new Page("/page2", new BiConsumer<JspWriter, ServletRequest>() { | |
| @Override | |
| public void accept(JspWriter jspWriter, ServletRequest servletRequest) { | |
| try { | |
| jspWriter.println("<h1>Page2</h1>"); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| }) | |
| ); | |
| final JspWriter writer = out; | |
| final HttpServletRequest req = request; | |
| pages.stream() | |
| .filter(new Predicate<Page>() { | |
| @Override | |
| public boolean test(Page page) { | |
| return page.isMatch(path); | |
| } | |
| }) | |
| .findFirst() | |
| .ifPresent(new Consumer<Page>() { | |
| @Override | |
| public void accept(Page page) { | |
| page.renderer.accept(writer, req); | |
| } | |
| }); | |
| %> | |
| <script> | |
| window.onhashchange = function (ev) { | |
| console.log(ev); | |
| var anchor = location.hash; | |
| var path = anchor; | |
| if (anchor === null) { | |
| path = ""; | |
| } else { | |
| path = location.hash.substring(1); | |
| } | |
| document.cookie = "path=/" + path + ";"; | |
| location = "/"; | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment