-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtutorial.html
More file actions
210 lines (184 loc) · 7.38 KB
/
tutorial.html
File metadata and controls
210 lines (184 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
layout: default
title: Tutorial
---
<p>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!
</p>
<p>If you're too busy to follow the steps, you can find this tutorial also here: <a href="https://github.com/st-js/bridges-examples/tree/master/html-hello">https://github.com/st-js/bridges-examples/tree/master/html-hello</a>.
</p>
<p>
So here are the steps to create a new, very simple application, with ST-JS:</p>
<ol class="tutorial">
<li>Create a new empty POM file for a war project
<pre class="code prettyprint lang-xml">
<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>
</pre>
</li>
<li>Add the <i>stjs.version</i> property - easier to have the same version between the different ST-JS artifacts
<pre class="code prettyprint lang-xml">
<properties>
<stjs.version>3.0.3</stjs.version>
</properties>
</pre>
</li>
<li>Point the build to the actual source folder containing the Java source files to compile to JavaScript and set the Java version to a version superior to 1.5
<pre class="code prettyprint lang-xml">
<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>
</pre>
</li>
<li>Add the dependency to the ST-JS JavaScript (basic and DOM) bridge library
<pre class="code prettyprint lang-xml">
<dependency>
<groupId>org.st-js.bridge</groupId>
<artifactId>html</artifactId>
<version>5.0.bv2</version>
</dependency>
</pre>
</li>
<li>Add the ST-JS plugin that will generate the JavaScript code (inside the build/plugins section)
<pre class="code prettyprint lang-xml">
<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>
</pre>
</li>
<li>Add the index.html page (in src/main/webapp)
<pre class="code prettyprint lang-html">
<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>
</pre>
</li>
<li>Create the Java class org/stjs/hello/HelloWorld.java. The main method will be executed after the declaration of the class.
<pre class="code prettyprint lang-java">
public class HelloWorld {
public static void main(String [] args){
}
}
</pre>
</li>
<li>Add the onload listener in the main method. The "window" object is a static member of the <b>Global</b> class.
<pre class="code prettyprint lang-java">
window.onload = new Callback1<DOMEvent>() {
public void $invoke(DOMEvent ev) {
}
};
</pre>
</li>
<li>Add the click listener to the "Go" button inside the onload listener
<pre class="code prettyprint lang-java">
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;
};
</pre>
</li>
<li>Include the generated JavaScript and the ST-JS support file in the page
<pre class="code prettyprint lang-html">
<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>
</pre>
</li>
<li>Add an (almost) empty WEB-INF/web.xml file in src/main/webapp to allow maven build
<pre class="code prettyprint lang-xml">
<?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>
</pre>
</li>
<li>Build (* see generated JavaScript below) and deploy it to your application server (or use the <a href="#jetty">Jetty Maven plugin **</a> to start it directly from the command line)</li>
<li>Browse <a href="http://localhost:8080/stjs/index.html">the page</a> and click on the "Go" button<br/>
<img src="hello.jpg">
</li>
</ol>
<!-- This code cannot be found there!!
<p>
You can also find this example (together with other, mode advanced examples) included in the ST-JS downloadable archive. Go to our <a href="download.html">download section</a> for details.
</p>
-->
<p>*) The generated JavaScript code:</p>
<pre class="code prettyprint lang-javascript">
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();
</pre>
<p><a id="jetty"></a>**) Jetty configuration:</p>
<pre class="code prettyprint lang-xml">
<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>
</pre>