我需要做什么才能在浏览器上呈现此代码?下面的代码目前在Eclipse Console上工作并呈现。我需要将这些代码与Tomcat等服务器一起使用,并使用localhost将其显示在浏览器上。请指教。Web浏览器上的Java ReactJs代码的服务器端渲染
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;
public class Test {
private ScriptEngine se;
// Constructor, sets up React and the Component
public Test() throws Throwable {
ScriptEngineManager sem = new ScriptEngineManager();
se = sem.getEngineByName("nashorn");
// React depends on the "global" variable
se.eval("var global = this");
// eval react.js
se.eval(new FileReader("../react-0.14.7/build/react.js"));
// This would also be an external JS file
String component =
"var MyComponent = React.createClass({" +
" render: function() {" +
" return React.DOM.div(null, this.props.text)" +
" }" +
"});";
se.eval(component);
}
// Render the component, which can be called multiple times
public void render(String text) throws Throwable {
String render =
"React.renderToString(React.createFactory(MyComponent)({" +
// using JSONObject here would be cleaner obviously
" text: '" + text + "'" +
"}))";
System.out.println(se.eval(render));
}
public static void main(String... args) throws Throwable {
Test test = new Test();
test.render("I want to Display");
test.render("This on a Browser like google chrome, using Tomcat server with Eclipse, currently it displays on Console in Eclipse.");
}
}
从哪里java进入这张照片。请解释。您可以在后端使用java来生成/处理Json格式的数据。 – WitVault
我在Eclipse IDE上运行该代码。 这里是我在Eclipse控制台上运行上述代码的结果