3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5 Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
7 Oracle and Java are registered trademarks of Oracle and/or its affiliates.
8 Other names may be trademarks of their respective owners.
10 The contents of this file are subject to the terms of either the GNU
11 General Public License Version 2 only ("GPL") or the Common
12 Development and Distribution License("CDDL") (collectively, the
13 "License"). You may not use this file except in compliance with the
14 License. You can obtain a copy of the License at
15 http://www.netbeans.org/cddl-gplv2.html
16 or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
17 specific language governing permissions and limitations under the
18 License. When distributing the software, include this License Header
19 Notice in each file and include the License file at
20 nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
21 particular file as subject to the "Classpath" exception as provided
22 by Oracle in the GPL Version 2 section of the License file that
23 accompanied this code. If applicable, add the following below the
24 License Header, with the fields enclosed by brackets [] replaced by
25 your own identifying information:
26 "Portions Copyrighted [year] [name of copyright owner]"
30 The Original Software is NetBeans. The Initial Developer of the Original
31 Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
33 If you wish your version of this file to be governed by only the CDDL
34 or only the GPL Version 2, indicate your decision by adding
35 "[Contributor] elects to include this software in this distribution
36 under the [CDDL or GPL Version 2] license." If you do not indicate a
37 single choice of license, a recipient has the option to distribute
38 your version of this file under either the CDDL, the GPL Version 2 or
39 to extend the choice of license to its licensees as provided above.
40 However, if you add GPL Version 2 code and therefore, elected the GPL
41 Version 2 license, then the option applies only if the new code is
42 made subject to such option by the copyright holder.
48 <title>HTML for Java APIs</title>
49 <meta charset="UTF-8">
50 <meta name="viewport" content="width=device-width, initial-scale=1.0">
51 <style type="text/css">
57 border: 1px solid black;
59 table.field td.UNKNOWN {
60 background-color: #D6E4E1;
63 table.field td.EXPLOSION {
64 background-color: #A31E39;
66 table.field td.DISCOVERED {
67 background-color: #9DB2B1;
73 Use Java to write application logic; Use HTML5 to render the UI;
74 {@link net.java.html.json.Model Animate an HTML page from Java}
75 (see <a target="_blank" href="http://dew.apidesign.org/dew/#7212206">Duke being rotated</a> by CSS);
76 Use {@link net.java.html.json.OnReceive REST} or
77 <a href="net/java/html/json/doc-files/websockets.html">WebSockets</a>;
78 interact with <a href="net/java/html/js/package-summary.html">JavaScript</a>;
79 Get the best of both worlds!
81 The goal of these APIs is to use full featured Java runtime
82 (like real <a href="http://wiki.apidesign.org/wiki/HotSpot">HotSpot VM</a>),
83 but still rely on a very lightweight rendering technology
84 (so it can potentially fit
85 <a href="http://bck2brwsr.apidesign.org">Bck2Brwsr</a> and definitely
86 to various types of phones). What can be more lightweight
87 (from a browser perspective) than
88 <a href="http://wiki.apidesign.org/wiki/HTML">HTML</a>!?
89 By default we use {@link net.java.html.boot.fx JavaFX's WebView}
90 component to display the <a href="http://wiki.apidesign.org/wiki/HTML">HTML</a>.
91 We eliminate the need to manipulate the DOM directly,
92 there is a special {@link net.java.html.json Java to Knockout.js binding}.
93 As a result the <a href="http://knockoutjs.com">HTML uses Knockout.js syntax</a>,
94 yet the application code can be written in Java.
97 <h3>What's Been Improved in Version 1.2.3?</h3>
99 One can control {@link net.java.html.json.OnReceive#headers() HTTP request headers}
100 when connecting to server using the {@link net.java.html.json.OnReceive}
101 annotation. It is possible to have
102 {@link net.java.html.json.ComputedProperty#write() writable computed properties}.
103 There is an easy way to enable <a target="_blank" href="http://getfirebug.com/">Firebug</a> in
104 the JavaFX based Web View -
105 just run with <code>-Dfirebug.lite=true</code> as
106 <a target="_blank" href="https://www.youtube.com/watch?v=2rxwY-QJiLo">this video</a>
108 Bugfix of issues <a target="_blank" href='https://netbeans.org/bugzilla/show_bug.cgi?id=250503'>250503</a>,
109 <a target="_blank" href='https://netbeans.org/bugzilla/show_bug.cgi?id=252987'>252987</a>.
111 <h3>What's New in Version 1.1?</h3>
114 The content of a {@link net.java.html.BrwsrCtx context}
115 can be selected by registering implementations under specific
116 {@link org.netbeans.html.context.spi.Contexts.Id technology identifiers}
117 and requesting them during
118 {@link org.netbeans.html.context.spi.Contexts#newBuilder(java.lang.Object...) construction}
119 of the context. <code>org.netbeans.html:ko4j</code> module's implementation
120 offers <b>ko4j</b>, <b>xhr</b> and <b>websocket</b> identifiers
121 for its registered services
122 (e.g. {@link org.netbeans.html.json.spi.Technology},
123 {@link org.netbeans.html.json.spi.Transfer} and
124 {@link org.netbeans.html.json.spi.WSTransfer}).
125 <code>org.netbeans.html:ko-ws-tyrus</code>
127 {@link org.netbeans.html.json.spi.Transfer Java based JSON} and
128 {@link org.netbeans.html.json.spi.WSTransfer WebSocket} implementations
129 under the name <b>tyrus</b>.
132 A particular DOM subtree
133 that a <a target="_blank" href="http://knockoutjs.com">knockout.js</a> model gets
134 applied to can be selected by using
135 {@link net.java.html.json.Models#applyBindings(java.lang.Object,java.lang.String)
136 Models.applyBindings(m, id)} with an id of an HTML element.
137 There is new {@link net.java.html.json.Model#targetId()} attribute
138 which controls behavior of the generated <code>applyBindings</code> method.
139 If <em>specified and non-empty</em>, then the generated method
140 will call {@link net.java.html.json.Models#applyBindings(java.lang.Object,java.lang.String)}
141 with <code>this</code> and the provided {@link net.java.html.json.Model#targetId() target id}.
142 If <em>specified, but left empty</em>, then the generated method
143 calls {@link net.java.html.json.Models#applyBindings(java.lang.Object)}.
144 <em>If unspecified</em>, the method will <b>not</b> be generated at all
145 (a change with respect to older versions). However one can
146 still use {@link net.java.html.json.Models#applyBindings(java.lang.Object)}
147 or {@link net.java.html.json.Models#applyBindings(java.lang.Object,java.lang.String)}
148 to perform the association of any model with the page element.
151 Memory model when using Knockout bindings has been improved
152 (required additions of two new methods:
153 {@link org.netbeans.html.json.spi.PropertyBinding#weak()} and
154 {@link org.netbeans.html.json.spi.FunctionBinding#weak()}) and
155 now the Java {@link net.java.html.json.Model models} can garbage collect,
156 when no longer used. Library writers that use
157 {@link net.java.html.js.JavaScriptBody} annotation can also
158 control garbage collection behavior of method arguments by
159 setting {@link net.java.html.js.JavaScriptBody#keepAlive() keepAlive=false}
163 <h3>What's New in Version 1.0?</h3>
166 {@link net.java.html.json.Property#array() Array properties} are now
167 mutable from the <a href="http://knockoutjs.com">knockout.js</a>
168 point of view (required {@link org.netbeans.html.json.spi.Proto.Type#replaceValue one SPI change}).
169 The page lookup mechanism can use {@link net.java.html.boot.BrowserBuilder#locale(java.util.Locale) locale}
170 to load localized a page with appropriate suffix.
171 All SPI were moved under the NetBeans namespace - e.g.
172 {@link org.netbeans.html.boot.spi},
173 {@link org.netbeans.html.context.spi},
174 {@link org.netbeans.html.json.spi},
175 {@link org.netbeans.html.sound.spi}, and also
176 {@link org.netbeans.html.json.tck}. Methods annotated
177 with {@link net.java.html.js.JavaScriptBody} annotation and
178 without fallback Java code now throw {@link java.lang.IllegalStateException}
179 with a message suggesting to switch to proper
180 {@link net.java.html.BrwsrCtx#execute browser context} to
181 prevent endless debugging when one forgets to do so.
185 What's new in older versions? Click the
186 <a href="#" onclick="return showHistoric(true)">link</a>
188 <a href="#" onclick="return showHistoric(true)">historic changes</a> below:
191 <a name="historic.changes"></a>
192 <div id="historic.changes">
194 function showHistoric(show) {
195 var e = document.getElementById("historic.changes");
197 e.style.display="block";
199 e.style.display="none";
206 <h3>What's New in Version 0.9?</h3>
209 System can run in {@link net.java.html.boot.BrowserBuilder#classloader(java.lang.ClassLoader) Felix OSGi container} (originally only Equinox).
210 {@link net.java.html.json.ComputedProperty Derived properties}
211 now deeply check changes in other {@link net.java.html.json.Model model
212 classes} they depend on and recompute their values accordingly.
213 <a target="_blank" href="http://knockoutjs.com">Knockout.js</a> library has been updated
217 <h3>What's New in 0.8.x Versions?</h3>
220 Setters or array properties on classes generated by {@link net.java.html.json.Model}
221 annotation can be accessed from any thread. {@link org.netbeans.html.sound.spi.AudioEnvironment}
222 can be registered into {@link net.java.html.BrwsrCtx}. There is
223 a {@link net.java.html.json.Models#parse(net.java.html.BrwsrCtx, java.lang.Class, java.io.InputStream, java.util.Collection) method}
224 to parse a JSON array and convert it into
225 {@link net.java.html.json.Model model classes}.
226 Improved behavior of <code>enum</code> values in
227 {@link net.java.html.json.Model knockout bindings}.
231 Few bugfixes for better portability.
232 New API for {@link net.java.html.boot.script.Scripts headless execution}
233 on top of <em>Nashorn</em> - does not run <em>knockout for Java</em>
235 (reported as <a href="https://bugs.openjdk.java.net/browse/JDK-8046013">JDK-8046013</a>),
236 however even in current state it is quite
237 {@link net.java.html.boot.script.Scripts useful for testing}
239 {@link net.java.html.js Java/JavaScript interactions}.
243 {@link net.java.html.boot.fx.FXBrowsers} has been extended
244 with new helper methods to make it easier to use HTML+Java
245 API in existing JavaFX applications.
246 The annotation processor is made
247 more robust with respect to errors in callback syntax of
248 {@link net.java.html.js.JavaScriptBody} body parameter.
249 Javadoc of {@link net.java.html.BrwsrCtx#execute} method
250 has been improved based on a failure of its usability study.
251 There can be additional parameters to methods annotated by
252 {@link net.java.html.json.OnReceive} that allows one to
253 pass state when a JSON call is made and use it when it finishes.
254 The mechanism of discovery of sibling HTML page has been
255 extended to work on systems that don't support
256 {@link java.lang.Class#getProtectionDomain}.
260 The first argument of method annotated by
261 {@link net.java.html.json.OnReceive} annotation has to
262 be the associated {@link net.java.html.json.Model model class}.
266 {@link net.java.html.json.OnReceive} annotation now accepts
267 {@link java.util.List} of data values as second argument
268 (previously required an array).
272 <h3>What's New in 0.7.x Versions?</h3>
275 {@link net.java.html.js.JavaScriptBody} annotation has new attribute
276 {@link net.java.html.js.JavaScriptBody#wait4js()} which allows
277 asynchronous execution. Libraries using
278 {@link net.java.html.js.JavaScriptBody} are urged to use this
279 new attribute as much as possible, as it can speed up execution
280 in certain environments.
284 Use {@link net.java.html.BrwsrCtx#execute(java.lang.Runnable)} in
285 multi-threaded environment to execute your code on the browser thread.
287 {@link net.java.html.BrwsrCtx#execute(java.lang.Runnable) using Java timer}.
291 <h3>Interesting Entry Points</h3>
293 <p>Learn how to {@link net.java.html.json.Model animate an HTML page from Java}
294 without referencing single HTML element from the Java code.
296 <p>Use {@link net.java.html.json.OnReceive JSON} to communicate
297 with REST based server API.
299 <p>Use <a href="net/java/html/json/doc-files/websockets.html">WebSockets</a>
302 <p>Call JavaScript methods from Java and vice versa, via
303 <a href="net/java/html/js/package-summary.html">JavaScriptBody</a>.
306 <h3>Getting Started</h3>
308 There are <a href="http://wiki.apidesign.org/wiki/DukeScriptInNetBeans">many ways</a>
310 <a href="http://html.java.net">Html for Java</a> application.
311 However to be sure one chooses the most recent setup, it is recommended
312 to switch to good old command line and use a
313 <a href="http://wiki.apidesign.org/wiki/Knockout4Java">Maven archetype</a>
314 associated with every version of this project. Make sure at least
315 <em>JDK7</em> is your installed Java and type:
317 $ mvn archetype:generate \
318 -DarchetypeGroupId=org.apidesign.html \
319 -DarchetypeArtifactId=knockout4j-archetype \
320 -DarchetypeVersion=0.8 <em># or newer version, if available</em>
322 Answer few questions (for example choose myfirstbrwsrpage as artifactId)
325 $ cd myfirstbrwsrpage
326 $ mvn process-classes exec:java
328 In a few seconds (or minutes if
329 <a href="http://wiki.apidesign.org/wiki/Maven">Maven</a>
330 decides to download the whole Internet of dependencies) you should
331 see a sample Hello World application rendered in a
332 <a href="http://wiki.apidesign.org/wiki/JavaFX">JavaFX</a>
333 web view component (that of course requires your JDK to come
334 with <a href="http://wiki.apidesign.org/wiki/JavaFX">JavaFX</a>;
335 <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">JDK7
336 and JDK8 from Oracle</a> contain everything that is needed).
337 The generated application is built around one
338 Java source (uses the {@link net.java.html.json.Model} annotation to
339 auto-generate another <code>Data.java</code> class during compilation)
340 and one HTML file (uses the <a href="http://knockoutjs.com">Knockout</a>
341 syntax to <code>data-bind</code> the HTML elements to the
342 generated <code>Data</code> model):
344 $ ls src/main/java/**/DataModel.java
345 $ ls src/main/webapp/pages/index.html
347 That is all you need to get started. Play with the sources,
348 modify them and enjoy
349 <a href="http://html.java.net">Html for Java</a>!
354 This API is part of <a target="_blank"
355 href="http://netbeans.org">NetBeans.org</a> project and as such
356 it works naturally with the <a target="_blank"
357 href="https://netbeans.org/features/index.html">NetBeans IDE</a>.
358 On the other hand, the API is using nothing NetBeans specific,
359 it builds on standard Java6 APIs and as such it shall work fine
364 A lot of work is done by
365 <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">
366 annotation processors</a>
367 that generate various boiler plate code during compilation. This
368 is a standard part of Java since JDK6, but for example Eclipse
369 is known not to deal with processors well and developers using
370 it need to be careful. IntelliJ users hasn't reported any issues
371 and of course, NetBeans IDE support for
372 <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">processors</a>
377 When using {@link net.java.html.js.JavaScriptBody} annotation, it is
378 useful to do a bit of post processing of classes. There is a
379 <a href="http://wiki.apidesign.org/wiki/Maven">Maven</a>
381 NetBeans IDE will invoke it when doing a build. Other IDEs may
382 need some hint to do so.
383 Anyway: If one does not see all (generated) sources or is getting
384 {@link java.lang.LinkageError}s when executing the application,
385 switch to command line and do clean build
388 <pre>$ mvn clean install</pre>
390 If that succeeds, your IDE of choice will hopefully
391 pick the generated sources up and present the result of the build
393 <a href="https://netbeans.org/downloads/">download NetBeans</a>,
394 you will be pleasantly
395 surprised - for example with our excellent
396 <a href="net/java/html/js/package-summary.html#debugging">Java/JavaScript
397 debugging</a> support.
401 <h2>Deploy Your Application</h2>
405 It is not goal of this documentation to list all possible ways
406 to package and deploy applications which use this API. However it is
407 important for new comers to see the benefits of using the
408 <a href="http://html.java.net">HTML for Java</a> API and as such
409 let's list at least few bundling options, known to work at the time of writing
414 First of all, this is a <em>client technology</em>. You write client applications
415 with it which may, but need not connect to a server. You don't need
416 Tomcat or WebLogic to deploy
417 <a href="http://html.java.net">HTML for Java</a> applications.
421 <img src='resources/javafx_logo.jpg' width="64"
422 height="64" align="left"/>
423 The sample project generated by
424 <code>org.apidesign.html knockout4j-archetype</code> is configured
425 to use <a href="http://wiki.apidesign.org/wiki/JavaFX">JavaFX</a>
426 as the rendering technology. This setup is primarily suitable for
427 development - it needs no special packaging, starts quickly and
428 allows you to use classical HotSpot VM debuggers. A final
429 artifact from the build is also a ZIP file which you can use
430 and distribute to your users. Good for desktop applications.
434 <img src='resources/netbeans_logo.jpg' width="64"
435 height="64" align="right"/>
436 <img src='resources/eclipse_logo.png' width="64"
437 height="64" align="right"/>
438 All the <a href="http://html.java.net">HTML for Java</a> libraries
439 are packaged as <a href="http://wiki.apidesign.org/wiki/OSGi">OSGi</a>
440 bundles and as such they can easily be run in NetBeans as well as
441 in Eclipse. As a result one can use
442 <a href="http://wiki.apidesign.org/wiki/OSGi">OSGi</a>
443 and have a common module system for both platforms. In addition to that
445 HTML and have a common UI in both platforms. In such case
446 your application would be packaged as a set of
447 <a href="http://wiki.apidesign.org/wiki/OSGi">OSGi</a> bundles.
449 <a href="http://wiki.apidesign.org/wiki/HTML">more</a>...
453 <img src='resources/chrome_logo.png' width="64"
454 height="64" align="left"/>
455 <img src='resources/safari_logo.png' width="64"
456 height="64" align="left"/>
457 <img src='resources/ie_logo.png' width="64"
458 height="64" align="left"/>
459 <img src='resources/firefox_logo.png' width="64"
460 height="64" align="left"/>
462 There is more and more attempts to execute Java bytecode
463 in a browser, without any special Java plugin installed.
464 The <a href="http://html.java.net">HTML for Java</a> is
465 carefully designed to produce lightweight, well performing
466 applications even on such restricted environments. It uses
467 no reflection calls and that allows to statically pre-compile
468 the applications into JavaScript. One of such environments
469 is called <a href="http://wiki.apidesign.org/wiki/Bck2Brwsr">Bck2Brwsr</a>,
470 another <a href="http://wiki.apidesign.org/wiki/TeaVM">TeaVM</a>. Both support the
471 {@link net.java.html.js.JavaScriptBody} annotation. Read
472 <a href="http://wiki.apidesign.org/wiki/Bck2BrwsrViaCLI">more</a> or play
473 a minesweeper game packaged for your browser
474 (of course <a target="_blank"
475 href="http://source.apidesign.org/hg/html~demo/file/ea79b73d590a/minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java">
476 written</a> in Java):
479 <script type="text/html" id="field">
480 <table class="field">
482 <!-- ko foreach: rows -->
484 <!-- ko foreach: columns -->
485 <td data-bind="css: style, click: $parents[1].click" >
486 <div data-bind='html: html'></div>
495 <div data-bind="template: { name : 'field', if: fieldShowing }"></div>
497 <!-- boot bck2brwsr -->
498 <script type="text/javascript" src="resources/teavm.js"></script>
501 vm.loadClass('org.apidesign.demo.minesweeper.MainBrwsr');
505 <img src='resources/ios_logo.jpg' width="64"
506 height="64" align="right"/>
507 <img src='resources/android_logo.jpg' width="64"
508 height="64" align="right"/>
510 Now when we have seen that the
511 <a href="http://html.java.net">HTML for Java</a> applications
512 can run on any modern browser, we can ask whether they can also
513 fit into a phone!? Yes, they can and especially to phones
514 that can execute Java code already! Just by changing your
515 packaging you can create an APK file and deploy it to your
517 Read <a target="_blank" href="http://wiki.apidesign.org/wiki/DlvkBrwsr">more</a>...
518 In case you'd like your application to reach out to second biggest
519 group of smartphone users, don't despair: It
520 seems the set of devices that can execute
521 <a href="http://html.java.net">HTML for Java</a> applications
522 has been extended to <em>iPads</em> and <em>iPhones</em>. Get the
523 <a target="_blank" href="http://wiki.apidesign.org/wiki/IBrwsr">details here</a>
524 and deploy everywhere!
527 Convinced it makes sense to use
528 <a href="http://html.java.net">HTML for Java</a>
529 APIs for writing applications that are
530 <em>written once, displayed anywhere</em>? Or do you have an
531 environment which is not supported? In such case you can bring
532 <a href="http://html.java.net">HTML for Java</a>
533 to your environment yourself. Just implement your own
534 {@link org.netbeans.html.boot.spi.Fn.Presenter}!
537 <h2>Other Resources</h2>
539 <img src="net/java/html/json/doc-files/DukeHTML.png" width="256" height="184" alt="Duke and HTML5. Together at last!" align="right"/>
541 The javadoc for latest and previous versions is also available
544 <li>Current <a target="_blank" href="http://bits.netbeans.org/html+java/dev/">development</a> version
545 <li>Version <a target="_blank" href="http://bits.netbeans.org/html+java/1.1">1.1</a>
546 <li>Version <a target="_blank" href="http://bits.netbeans.org/html+java/1.0">1.0</a>
547 <li>Version <a target="_blank" href="http://bits.netbeans.org/html+java/0.9">0.9</a>
548 and historic ones (<a target="_blank" href="http://bits.netbeans.org/html+java/0.8.3">0.8.3</a>,
549 <a target="_blank" href="http://bits.netbeans.org/html+java/0.8.2">0.8.2</a>,
550 <a target="_blank" href="http://bits.netbeans.org/html+java/0.8.1">0.8.1</a>,
551 <a target="_blank" href="http://bits.netbeans.org/html+java/0.8">0.8</a>, and
552 <a target="_blank" href="http://bits.netbeans.org/html+java/0.7.5">0.7.5</a>)