First commit
This commit is contained in:
27
.gitignore
vendored
Normal file
27
.gitignore
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
## IDEA ##
|
||||
.idea/
|
||||
out/
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
*.ids
|
||||
|
||||
## ECLIPSE ##
|
||||
.settings/
|
||||
bin/
|
||||
.classpath
|
||||
.project
|
||||
|
||||
## GRADLE ##
|
||||
.gradle/
|
||||
build/
|
||||
gradle/
|
||||
gradlew
|
||||
gradlew.bat
|
||||
|
||||
## MAVEN ##
|
||||
target/
|
||||
|
||||
## OTHER ##
|
||||
lib/
|
||||
|
||||
43
pom.xml
Normal file
43
pom.xml
Normal file
@@ -0,0 +1,43 @@
|
||||
<?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>ru.dmitriymx.reactjs</groupId>
|
||||
<artifactId>reactjs-example</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}-${project.version}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.15</version>
|
||||
<configuration>
|
||||
<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
60
src/main/java/ru/dmitriymx/reactjs/JsTools.java
Normal file
60
src/main/java/ru/dmitriymx/reactjs/JsTools.java
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2017-03-06
|
||||
*/
|
||||
package ru.dmitriymx.reactjs;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class JsTools {
|
||||
private static ScriptEngine engine;
|
||||
|
||||
public static ScriptEngine getScriptEngine() {
|
||||
if (engine == null) {
|
||||
engine = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
try {
|
||||
engine.eval("var global = this;");
|
||||
engine.eval("var console = {}; console.debug = print; " +
|
||||
"console.warn = print; console.log = print; console.info = print;");
|
||||
} catch (ScriptException e) {
|
||||
e.printStackTrace();
|
||||
engine = null;
|
||||
}
|
||||
}
|
||||
return engine;
|
||||
}
|
||||
|
||||
public static void loadJsFromUrl(String url) throws IOException, ScriptException {
|
||||
URL _url = new URL(url);
|
||||
URLConnection connection = _url.openConnection();
|
||||
InputStreamReader reader = new InputStreamReader(connection.getInputStream());
|
||||
getScriptEngine().eval(reader);
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public static void loadJsFromClasspath(String path) throws ScriptException, IOException {
|
||||
InputStreamReader reader = new InputStreamReader(Sample1.class.getResourceAsStream(path));
|
||||
getScriptEngine().eval(reader);
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public static Object invokeFunction(String functionName, Object... args) {
|
||||
try {
|
||||
return ((Invocable) getScriptEngine()).invokeFunction(functionName, args);
|
||||
} catch (ScriptException | NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T invokeFunction(String functionName, Function<Object, T> converter, Object... args) {
|
||||
return converter.apply(invokeFunction(functionName, args));
|
||||
}
|
||||
}
|
||||
32
src/main/java/ru/dmitriymx/reactjs/Sample1.java
Normal file
32
src/main/java/ru/dmitriymx/reactjs/Sample1.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2017-03-06
|
||||
*/
|
||||
package ru.dmitriymx.reactjs;
|
||||
|
||||
import static ru.dmitriymx.reactjs.JsTools.*;
|
||||
|
||||
import javax.script.ScriptException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Sample1 {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
log("Load js");
|
||||
loadJsFromUrl("https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js");
|
||||
loadJsFromUrl("https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom-server.min.js");
|
||||
loadJsFromClasspath("/app.js");
|
||||
|
||||
log("Call function");
|
||||
String result = invokeFunction("renderOnServer", String::valueOf);
|
||||
|
||||
log("Result:\n%s", result);
|
||||
} catch (ScriptException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void log(String format, Object... objects) {
|
||||
System.err.printf(format+"\n", objects);
|
||||
}
|
||||
}
|
||||
14
src/main/resources/app.js
Normal file
14
src/main/resources/app.js
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
|
||||
function renderOnServer() {
|
||||
return ReactDOMServer.renderToString(
|
||||
React.createElement("h2", null, "Hello world")
|
||||
);
|
||||
}
|
||||
|
||||
function renderOnClient() {
|
||||
ReactDOM.render(
|
||||
React.createElement("h2", null, "Hello world"),
|
||||
document.getElementById('app')
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user