Because an exemple is so much more usefull than a big explanation, here comes a typical simple WebJS page.
The following displays all blog entries with an edit link.
@WebPage(path="blog.html", page="template/blog.html", script="js/blog-server.js") public class Blog { @WebPart @Default public void displayBlogEntries(Request request, Bindings bindings) throws WebException { bindings.addProperty("blogEntries", BlogService.getInstance(request).getBlogEntries()); } @WebPart @Selector("#edit") public void displayEditForm(Request request, Bindings bindings) throws WebException { String id = request.get("id"); bindings.addProperty("editBlogEntry", BlogService.getInstance(request).getBlogEntry(Integer.valueOf(id))); } }
<html> <head> <script src="js/blog-client.js" type="text/javascript" /> </head> <body> <div id="content"> <div class="blog"> <div class="blog-entry"> <div class="blog-entry-actions"> <a class="blog-entry-edit" href="#">Edit</a> </div> <div class="blog-entry-content"> <div class="blog-entry-title"/> <div class="blog-entry-text"/> </div> <hr/> </div> </div> <form id="edit" style="display: none;"> <div class="edit-form"> <div class="edit-content"> <input type="hidden" name="entry-id" /> <label for="entry-title">Title</label> <div><input type="text" name="entry-title" /></div> <label for="entry-text">Text</label> <div><textarea name="entry-text"></textarea></div> </div> <div class="edit-actions"> <button id="edit-ok" type="button">OK</button> <button id="edit-cancel" type="button">Cancel</button> </div> </div> </form> </div> </body> </html>
// Fills the blog entries using a template (if needed) WebJS.checkBindings(["blogEntries"], function(){ $(".blog-entry").template(blogEntries.toArray(), function(entry, index) { this.appendTo(".blog"); this.attr("id", "blog-entry-"+entry.id); this.attr("data-pk", entry.id); this.find(".blog-entry-title").text(entry.title); this.find(".blog-entry-text").text(entry.text); }); }); // Fills the edit form (if needed) WebJS.checkBindings(["editBlogEntry"], function(){ $("#edit input[name=entry-id]").val(editBlogEntry.id); $("#edit input[name=entry-title]").val(editBlogEntry.title); $("#edit textarea[name=entry-text]").val(editBlogEntry.text); });
$(function(){ // On edit click, we execute the displayEditForm and unhide the div $(".blog-entry-edit").click(function(event){ var pk = $(this).parents(".blog-entry").attr("data-pk"); WebJS.executeWebPart("displayEditForm", {id: pk}, function() { $("#edit").css("display", "block"); }); return false; }); });
When it receives an HTTP request, WebJS works in 4 simple steps :
If we apply this to just putting http://localhost:8080/<YourWAR>/webjs/blog.html in your browser URL bar :
On the client side, every Edit link is bound to a WebJS.executeWebPart call. This is an Ajax request to a specific @WebPart by its method name. So, if we apply the WebJS rules to this Ajax request, it gives :
Now that you have WebJS basic principles in mind, you can go to the Sample Project section to go forward with this blog example.
Or simply read the Documentation.