From 4be26bb3f021aa0f984a824ef3caad4599670244 Mon Sep 17 00:00:00 2001 From: Kumar Chandrakant Date: Sat, 24 Aug 2019 11:07:03 +0530 Subject: [PATCH] Adding source code for article tracked under BAEL-3172. (#7540) * Adding source code for article tracked under BAEL-3172. * Adding source code for article tracked under BAEL-3172. * Adding source code for article tracked under BAEL-3172. * Incorporated the review comments. * Removing absolute URL from JS. * Removing absolute URL from JS. --- pom.xml | 5 + spring-boot-nashorn/README.md | 3 + spring-boot-nashorn/pom.xml | 51 + .../com/baeldung/nashorn/Application.java | 20 + .../nashorn/controller/MyRestController.java | 14 + .../nashorn/controller/MyWebController.java | 32 + .../src/main/resources/application.properties | 2 + .../src/main/resources/static/app.js | 37 + .../resources/static/js/react-dom-server.js | 42 + .../src/main/resources/static/js/react-dom.js | 42 + .../src/main/resources/static/js/react.js | 18794 ++++++++++++++++ .../src/main/webapp/WEB-INF/jsp/index.jsp | 19 + 12 files changed, 19061 insertions(+) create mode 100644 spring-boot-nashorn/README.md create mode 100644 spring-boot-nashorn/pom.xml create mode 100644 spring-boot-nashorn/src/main/java/com/baeldung/nashorn/Application.java create mode 100644 spring-boot-nashorn/src/main/java/com/baeldung/nashorn/controller/MyRestController.java create mode 100644 spring-boot-nashorn/src/main/java/com/baeldung/nashorn/controller/MyWebController.java create mode 100644 spring-boot-nashorn/src/main/resources/application.properties create mode 100644 spring-boot-nashorn/src/main/resources/static/app.js create mode 100644 spring-boot-nashorn/src/main/resources/static/js/react-dom-server.js create mode 100644 spring-boot-nashorn/src/main/resources/static/js/react-dom.js create mode 100644 spring-boot-nashorn/src/main/resources/static/js/react.js create mode 100644 spring-boot-nashorn/src/main/webapp/WEB-INF/jsp/index.jsp diff --git a/pom.xml b/pom.xml index 58c5473744..a4f2a13c28 100644 --- a/pom.xml +++ b/pom.xml @@ -564,6 +564,8 @@ spring-boot-flowable spring-security-kerberos oauth2-framework-impl + + spring-boot-nashorn @@ -798,6 +800,8 @@ tensorflow-java spring-boot-flowable spring-security-kerberos + + spring-boot-nashorn @@ -947,6 +951,7 @@ spring-boot-flowable spring-security-kerberos + spring-boot-nashorn diff --git a/spring-boot-nashorn/README.md b/spring-boot-nashorn/README.md new file mode 100644 index 0000000000..1ca43927ee --- /dev/null +++ b/spring-boot-nashorn/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: +- []() + diff --git a/spring-boot-nashorn/pom.xml b/spring-boot-nashorn/pom.xml new file mode 100644 index 0000000000..0f43752993 --- /dev/null +++ b/spring-boot-nashorn/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + com.baeldung.nashorn + spring-boot-nashorn + spring-boot-nashorn + 1.0 + jar + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.apache.tomcat.embed + tomcat-embed-jasper + provided + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + + \ No newline at end of file diff --git a/spring-boot-nashorn/src/main/java/com/baeldung/nashorn/Application.java b/spring-boot-nashorn/src/main/java/com/baeldung/nashorn/Application.java new file mode 100644 index 0000000000..b3cf1acd79 --- /dev/null +++ b/spring-boot-nashorn/src/main/java/com/baeldung/nashorn/Application.java @@ -0,0 +1,20 @@ +package com.baeldung.nashorn; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +@SpringBootApplication +public class Application extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(Application.class); + } + + public static void main(String[] args) throws Exception { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-boot-nashorn/src/main/java/com/baeldung/nashorn/controller/MyRestController.java b/spring-boot-nashorn/src/main/java/com/baeldung/nashorn/controller/MyRestController.java new file mode 100644 index 0000000000..86340d3d6a --- /dev/null +++ b/spring-boot-nashorn/src/main/java/com/baeldung/nashorn/controller/MyRestController.java @@ -0,0 +1,14 @@ +package com.baeldung.nashorn.controller; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class MyRestController { + + @RequestMapping("/next/{last}/{secondLast}") + public int index(@PathVariable("last") int last, @PathVariable("secondLast") int secondLast) throws Exception { + return last + secondLast; + } +} diff --git a/spring-boot-nashorn/src/main/java/com/baeldung/nashorn/controller/MyWebController.java b/spring-boot-nashorn/src/main/java/com/baeldung/nashorn/controller/MyWebController.java new file mode 100644 index 0000000000..78af6b966a --- /dev/null +++ b/spring-boot-nashorn/src/main/java/com/baeldung/nashorn/controller/MyWebController.java @@ -0,0 +1,32 @@ +package com.baeldung.nashorn.controller; + +import java.io.InputStreamReader; +import java.util.Map; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class MyWebController { + + @RequestMapping("/") + public String index(Map model) throws Exception { + + ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn"); + + getClass().getResource("classpath:storedProcedures.sql"); + + nashorn.eval(new InputStreamReader(new ClassPathResource("static/js/react.js").getInputStream())); + nashorn.eval(new InputStreamReader(new ClassPathResource("static/js/react-dom-server.js").getInputStream())); + + nashorn.eval(new InputStreamReader(new ClassPathResource("static/app.js").getInputStream())); + Object html = nashorn.eval("ReactDOMServer.renderToString(" + "React.createElement(App, {data: [0,1,1]})" + ");"); + + model.put("content", String.valueOf(html)); + return "index"; + } +} diff --git a/spring-boot-nashorn/src/main/resources/application.properties b/spring-boot-nashorn/src/main/resources/application.properties new file mode 100644 index 0000000000..ae00fe72f8 --- /dev/null +++ b/spring-boot-nashorn/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.mvc.view.prefix: /WEB-INF/jsp/ +spring.mvc.view.suffix: .jsp \ No newline at end of file diff --git a/spring-boot-nashorn/src/main/resources/static/app.js b/spring-boot-nashorn/src/main/resources/static/app.js new file mode 100644 index 0000000000..e5d79217dd --- /dev/null +++ b/spring-boot-nashorn/src/main/resources/static/app.js @@ -0,0 +1,37 @@ +var App = React.createClass({displayName: "App", + + handleSubmit: function () { + var last = this.state.data[this.state.data.length-1]; + var secondLast = this.state.data[this.state.data.length-2]; + $.ajax({ + url: '/next/'+last+'/'+secondLast, + dataType: 'text', + success: function (msg) { + var series = this.state.data; + series.push(msg); + this.setState({data: series}); + }.bind(this), + error: function (xhr, status, err) { + console.error("/next", status, err.toString()); + }.bind(this) + }); + }, + + componentDidMount: function() { + this.setState({data: this.props.data}); + }, + + getInitialState: function () { + return {data: []}; + }, + + render: function () { + return ( + React.createElement("div", {className: "app"}, + React.createElement("h2", null, "Fibonacci Generator"), + React.createElement("h2", null, this.state.data.toString()), + React.createElement("input", {type: "submit", value: "Next", onClick: this.handleSubmit}) + ) + ); + } +}); \ No newline at end of file diff --git a/spring-boot-nashorn/src/main/resources/static/js/react-dom-server.js b/spring-boot-nashorn/src/main/resources/static/js/react-dom-server.js new file mode 100644 index 0000000000..91050111c3 --- /dev/null +++ b/spring-boot-nashorn/src/main/resources/static/js/react-dom-server.js @@ -0,0 +1,42 @@ +/** + * ReactDOMServer v0.14.3 + * + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ +// Based off https://github.com/ForbesLindesay/umd/blob/master/template.js +;(function(f) { + // CommonJS + if (typeof exports === "object" && typeof module !== "undefined") { + module.exports = f(require('react')); + + // RequireJS + } else if (typeof define === "function" && define.amd) { + define(['react'], f); + + // + + + + +
${content}
+ + + + \ No newline at end of file