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.
This commit is contained in:
parent
00c5ecc8ef
commit
4be26bb3f0
5
pom.xml
5
pom.xml
|
@ -564,6 +564,8 @@
|
|||
<module>spring-boot-flowable</module>
|
||||
<module>spring-security-kerberos</module>
|
||||
<module>oauth2-framework-impl</module>
|
||||
|
||||
<module>spring-boot-nashorn</module>
|
||||
</modules>
|
||||
|
||||
</profile>
|
||||
|
@ -798,6 +800,8 @@
|
|||
<module>tensorflow-java</module>
|
||||
<module>spring-boot-flowable</module>
|
||||
<module>spring-security-kerberos</module>
|
||||
|
||||
<module>spring-boot-nashorn</module>
|
||||
|
||||
</modules>
|
||||
|
||||
|
@ -947,6 +951,7 @@
|
|||
|
||||
<module>spring-boot-flowable</module>
|
||||
<module>spring-security-kerberos</module>
|
||||
<module>spring-boot-nashorn</module>
|
||||
</modules>
|
||||
|
||||
</profile>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
- []()
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.nashorn</groupId>
|
||||
<artifactId>spring-boot-nashorn</artifactId>
|
||||
<name>spring-boot-nashorn</name>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-jasper</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<String, Object> 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";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
spring.mvc.view.prefix: /WEB-INF/jsp/
|
||||
spring.mvc.view.suffix: .jsp
|
|
@ -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})
|
||||
)
|
||||
);
|
||||
}
|
||||
});
|
|
@ -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);
|
||||
|
||||
// <script>
|
||||
} else {
|
||||
var g
|
||||
if (typeof window !== "undefined") {
|
||||
g = window;
|
||||
} else if (typeof global !== "undefined") {
|
||||
g = global;
|
||||
} else if (typeof self !== "undefined") {
|
||||
g = self;
|
||||
} else {
|
||||
// works providing we're not in "use strict";
|
||||
// needed for Java 8 Nashorn
|
||||
// see https://github.com/facebook/react/issues/3037
|
||||
g = this;
|
||||
}
|
||||
g.ReactDOMServer = f(g.React);
|
||||
}
|
||||
|
||||
})(function(React) {
|
||||
return React.__SECRET_DOM_SERVER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
||||
});
|
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* ReactDOM 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);
|
||||
|
||||
// <script>
|
||||
} else {
|
||||
var g
|
||||
if (typeof window !== "undefined") {
|
||||
g = window;
|
||||
} else if (typeof global !== "undefined") {
|
||||
g = global;
|
||||
} else if (typeof self !== "undefined") {
|
||||
g = self;
|
||||
} else {
|
||||
// works providing we're not in "use strict";
|
||||
// needed for Java 8 Nashorn
|
||||
// see https://github.com/facebook/react/issues/3037
|
||||
g = this;
|
||||
}
|
||||
g.ReactDOM = f(g.React);
|
||||
}
|
||||
|
||||
})(function(React) {
|
||||
return React.__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
||||
});
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,19 @@
|
|||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Hello React!</title>
|
||||
<script type="text/javascript" src="js/react.js"></script>
|
||||
<script type="text/javascript" src="js/react-dom.js"></script>
|
||||
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root">${content}</div>
|
||||
<script type="text/javascript" src="app.js"></script>
|
||||
<script type="text/javascript">
|
||||
ReactDOM.render(
|
||||
React.createElement(App, {data: [0,1,1]}),
|
||||
document.getElementById("root")
|
||||
);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue