Sunday, October 5, 2014

Setting up Slim3 Framework for Google App Engine using Eclipse Luna

Slim3, new to me, cool stuff. Official web site: https://sites.google.com/site/slim3appengine/Home

Step 1 - get eclipse for JEE (eclipse-jee-luna-SR1-win32-x86_64) downloaded, unzip, run, install Google plugin, and done.

Step 2 - File > New > Project... > Google > Web Application Project
Here I turn off GWT. Other than that, fill in your project info accordingly.
PS: If you don't see this screen, DON'T PROCEED. There is something very wrong in your setup. lol.

Step 3 - Download slim3 v 1.0.16 from here, then unzip it into a folder. It should look like this.
The structure is similar to your project structure in eclipse, only with few extra (slim3) files.

Step 4 - Copy paste.
Now highlighted are files copied from slim3-blank folder.

Also, some jar as well from WEB-INF/lib


Step 5 - Annotation Processing Factory configuration.
This setting is simple but very important. It actually help generate source code for you. You'll know what it does as you continue working on your project.
Right click your project > properties and find Java Compiler > Annotation Processing.
Enable project specific settings.
Enable annotation processing.

Click on Factory path (left panel).
Click Add JARs...
Find slim3-gen-1.0.16.jar from your project. Add it.
Move it to the top.

Click OK and we leave the settings as it is for now.

Step 6 - web.xml
The web.xml was copied from slim-blank folder, only thing need to change is the slim3.rootPackage. It should be something like "com.mycompany", but...

Anyway, step 7 - Ant build.
As you can see, there is a build.xml file copied from slim3. This file will be heavily used throughout the project. So it will be a good idea to open a Ant view to use it.

To show Ant view, go Window > Show View > Others... > Ant > Ant
Then find and click the little add buildfiles icon and add the build.xml from your project folder. And you will see display as picture above.

From slim3 tutorial, the difference is you don't need to right click the build.xml and Run As...
Just double click target in the Ant View and you are done.

Step 8 - now, let's see how the magic works. Run gen-controller (double click on the ant view). You will see a popup, key in the path to your controller.
Once you click OK, slim3 will create a few files on your project.

Let's look at each file closely.
As I entered '/admin/user/' as controller path, slim3 creates:

  • IndexController.java - the controller class to handle URL http://localhost:8888/admin/user/ or http://localhost:8888/admin/user/index
  • IndexControllerTest.java - test class to test the controller. I love this feature.
  • war/admin/user/index.jsp - the View 
In short, code generator. (for lazy developer like me)

Step 9 - let's look at the controller.
Oh No~!! We have a problem! Help!!!
Nar~! No worries. Just build path issues.
Go to WEB-INF/lib, right click slim3-1.0.16.jar and add to build path. Solved.


Step 10 - not ready to run yet.
GAE is complaining. What to do... 
Go to eclipse  > preference > Java > Installed JREs, add JDK 1.7 into it. Click the checkbox to make it the default. Default is only JRE. Sorry for not covering the details.



Once configured, you should see the changes on your project showing it is using JDK and the error message will be gone.


Step 11 - show time
Right click your project and run as google web application.

Output on the console:

Now we go to http://localhost:8888/admin/user/

Just to make sure what is happening. Edit war/admin/user/index.jsp

Yeap! We got hello world.

In short, path "/admin/user/" is handled by the controller, then the controller forward us to /war/admin/user/index.jsp. Typical mvc, right.

This is my first approach trying slim3. I believe there is a more elegant way to set it up. Any comments for improvement is welcome. Actually there is a slim3 plugin for eclipse too, I only found out after this...

My next assignment was to implement RBAC in slim3. Will post some more stuff when it is ready.

No comments: