Welcome to the first Jaiwls Tutorial. In this post I will present you how to build up a Webapplication using Jaiwls by using your favorite IDE.
If you are working on the subversion-source please make sure that you added all libraries from the distribution (included all *common-jars and surely jetty etc.), otherwise just include the binary and all it's libraries. In the subversion trunk, there are also two examples, one "Hello World" and another more complex one. The complex one is recommended. You can find all needed ressources on the Jaiwls project page.
Let's start our web-app by setting up a main-class, in which we just start our Jaiwls-server.
LoggerInit.initializeLog4J(Level.ALL); ApplicationStarter.startWebserver(WelcomePage.class, 8080);
First of all you have to initialize log4j properly. Then you tell the jaiwls framework to start a webserver at port 8080 and give it a class, which will be the root of your application. This has to be a ContentContainer. GHtmlPage is such a thing. It is recommended to make use of object hierarchies to make your webapp a better one.
public class Layout extends GHTMLPage{ protected GDiv menu; protected GDiv content; protected GDiv footer; protected GDiv options; /** * Build the layout and standard-menu and force to be loggedin for every page inheriting from this */ public void onEnter() { menu=new GDiv("nav"); menu.add(new GLink(getContext().generateURL(MyPictures.class, ""),"Meine Bilder", "Bilder verwalten")); menu.add(new GLink(getContext().generateURL(Login.class, "logOut"),"Abmelden", "Datei hochladen und archivieren")); getPage().add(new GTitle("ContentManager", 1)); getPage().addExternalStyleScript(getContext().generateURL(Ressource.class, "")+"/?file=style.css"); getPage().add(new GDiv("bglefttop")); getPage().add(menu); options=new GDiv("options"); getPage().add(options); content=new GDiv("content"); getPage().add(content); footer=new GDiv("footer"); footer.add(new GPlain(" - ")); footer.add(new GLink("http://validator.w3.org/check/referer", "Valid XHTML,", null)); footer.add(new GPlain(" ")); footer.add(new GLink("http://jigsaw.w3.org/css-validator/check/referer", "CSS", null)); footer.add(new GPlain(" ")); footer.add(new GPlain("ContentManager Version 0.1")); getPage().add(footer); Login.getNeededLogin(getContext()); } }
After defining a superclass which implements your layout, you can inherit every webpage directly from it.
public class Welcome extends Layout { public void onDefault() { getPage().setTitle("Willkommen beim ContentManager"); getContext().getDivFromPage("content").add(new GPar("Willkommen beim ContentManager")); //surely you should use the protected content attribute from the superclass to get better performance } }
Congratulations, you've got your first hello world example running. Now let's go to the ressource handling. This is not as easy, because perhaps you want the ressources to be compilied directly to your webapplication or have another dedicated folder where you files are (so you can also change them in static mode).
To get your ressources compiled to your projects, create a package where you put a class, e.g. called Resource. This may look like this:
public class Ressource extends GPackage{ }
Now you can put any ressources into this package. This is how you use it in your application:
If you study the sourcecode, you will find how to make this available from directories or to generate resources completely dynamically at runtime.
Have fun