This tutorial explains you how to build a "Hello World" application using ST-JS. We presume you have previous knowledge of Maven, Java, JavaScript and HTML. Additionally, you'll have to deploy the generated war file to your preferred servlet engine. Even though Java is used to generate the JavaScript, for this particular example no Java code is run, as no interaction with the server is made. So you can also take the content of the war file and deploy it to an Apache server, for example, and it will work!
If you're too busy to follow the steps, you can find this tutorial also here: https://github.com/st-js/bridges-examples/tree/master/html-hello.
So here are the steps to create a new, very simple application, with ST-JS:
<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>org.st-js</groupId> <artifactId>hello</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>war</packaging> <name>Hello world</name> </project>
<properties> <stjs.version>3.0.3</stjs.version> </properties>
<build> <sourceDirectory>src/main/java</sourceDirectory> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build>
<dependency> <groupId>org.st-js.bridge</groupId> <artifactId>html</artifactId> <version>5.0.bv2</version> </dependency>
<plugin> <groupId>org.st-js</groupId> <artifactId>maven-plugin</artifactId> <version>${stjs.version}</version> <executions> <execution> <id>main</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin>
<html> <body> <h1>ST-JS example: Hello world</h1> <form id="form"> Say hello to: <input type="text" name="to"> <input type="button" name="say" value="Go"> </form> </body> </html>
public class HelloWorld { public static void main(String [] args){ } }
window.onload = new Callback1<DOMEvent>() { public void $invoke(DOMEvent ev) { } };
Form form = window.document.forms.$get(0); Element button = form.elements.$get("say"); final Input text = form.elements.$get("to"); button.onclick = (ev) -> { alert("Hello " + text.value); return true; };
<head> <script src="generated-js/stjs.js" type="text/javascript"></script> <script src="generated-js/org/stjs/hello/HelloWorld.js" type="text/javascript"></script> </head>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> </web-app>
*) The generated JavaScript code:
var HelloWorld = function(){}; stjs.extend(HelloWorld, null, [], function(constructor, prototype){ constructor.main = function(args) { window.onload = function(ev) { var form = window.document.forms[0]; var button = form.elements["say"]; var text = form.elements["to"]; button.onclick = function(ev) { alert("Hello " + text.value); return true; }; }; }; }, {}); if (!stjs.mainCallDisabled) HelloWorld.main();
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <configuration> <webAppConfig> <contextPath>/stjs</contextPath> <baseResource implementation="org.eclipse.jetty.util.resource.ResourceCollection"> <resourcesAsCSV>src/main/webapp,target/${project.artifactId}-${project.version}</resourcesAsCSV> </baseResource> </webAppConfig> </configuration> </plugin>