Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@
<module>spring-boot-flowable</module>
<module>spring-security-kerberos</module>
<module>oauth2-framework-impl</module>

<module>spring-boot-nashorn</module>
</modules>

</profile>
Expand Down Expand Up @@ -795,6 +797,8 @@
<module>tensorflow-java</module>
<module>spring-boot-flowable</module>
<module>spring-security-kerberos</module>

<module>spring-boot-nashorn</module>

</modules>

Expand Down Expand Up @@ -944,6 +948,7 @@

<module>spring-boot-flowable</module>
<module>spring-security-kerberos</module>
<module>spring-boot-nashorn</module>
</modules>

</profile>
Expand Down
3 changes: 3 additions & 0 deletions spring-boot-nashorn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Relevant Articles:
- []()

51 changes: 51 additions & 0 deletions spring-boot-nashorn/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
2 changes: 2 additions & 0 deletions spring-boot-nashorn/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
37 changes: 37 additions & 0 deletions spring-boot-nashorn/src/main/resources/static/app.js
Original file line number Diff line number Diff line change
@@ -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})
)
);
}
});
Original file line number Diff line number Diff line change
@@ -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;
});
42 changes: 42 additions & 0 deletions spring-boot-nashorn/src/main/resources/static/js/react-dom.js
Original file line number Diff line number Diff line change
@@ -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;
});
Loading