Friday, 19 February 2010

Beginning Java EE 6. Hand Holding Learning Trail (JSF 2.0, Maven, Glassfish, Primefaces)

In this series and few more to follow, I will be posting on how to get started
with Java EE 6, JSF 2.0, Maven, Glasfish V3, Primefaces 2.0 and related frameworks.
This is just part of my effort to know it more. As I try to teach you,
I will be teaching myself even more. Lets get started.

I will try as much as possible not to use any IDE for now. Just plain text editor
so we can understand the basics.

This is to help us understand the structure of the whole web application.
Once we are done and we start using an IDE (Netbeans 4 Me), you will realise you have much control
you have over what you are doing because you understand how it all comes together.

Ok. Lets Run

ONE GOTCHA
Windows Notepad has the habit of adding .txt to my xml files which caused me a lot of trouble
untill I realised the files were being named wrongly. eg web.xml.txt.

Make sure your files are actually .xml and .xhtml files as windows will still tell
you they are even when they are not. (I used notepad++)

PART 1


Setting up our environment
I will not be jack of all trades. I will only use Windows. If you are using linux
or some other platform you must look for the equivalent commands.

Create the folder

This will be our Workspace will be
(Just make sure you understand what you are doing if you have to choose settings
to suit your environment)
I will generally work from c:\
Change the drive letters to suit your environment

Task 1. Install Java


Get Java from our old(rebranded) site. Make sure your version is >= 1.6 update 17
Get 1.6 update 18(the latest at the time of writing) from here
http://java.sun.com/javase/downloads/widget/jdk6.jsp
Install it to so your JAVA_HOME becomes

I will not install it because I already have it installed but it should be very easy.

Task 2. Install Glassfish V3 (Zip Version)


Download Glassfish V3(Zip Version). The zip version will help us to understand how things work
even more. Also we can easily clean up our environment easily if we dont need it.
Get the full zip (Not the Web profile) from
http://download.java.net/glassfish/v3/release/glassfish-v3.zip
NB: the web profile will do but as I'm new to this, I will stay out of trouble for now
Save it to

Right-click and extract it so that, after its done, your glassfish home will be

Open notepad or any editor and type the following

and save it as setenv.bat in the folder C:\home

Task 3. Install Maven


Download Maven from one of the mirrors here
http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-2.2.1-bin.zip
and save it.
Then extract it such that your maven home folder will be

Open your setenv.bat in the folder C:\home
and add the following

So your whole setenv.bat file will look like

Now open Command Prompt(I guess you know how to do that..lol)
and type each of the following, press enter after each command(Don't copy and paste)



You should get something like this after your last command

Our environment is almost set.
Lets test our glassfish.
With our command prompt still opened(If you closed it, make sure you run setenv.bat to set your environment and cd to the training folder before you proceed.)

type


then type

then browse to http://localhost:4848/
You should see glassfish loading.
For now, If we get a modal box asking us to register we will not register,so just do remind me later.
Hopefully, you will see a very nice man in a boat with a paddle.
That is the test. If you see that, you have completed part 1.

PART 2 Creating the Skeleton of the Application


On the command prompt

type

the type

then press Enter
This is a wrapped version so you can understand it. Use the one above to stay out of trouble


Maven will go off to do some stuff. You will get some messages on the console and the you will see
Build Successful. Yes. The Skeleton is now created.

Maven will generate standard maven project into our training folder.

The folder structure of the maven generated project is as shown below
Skeleton of a maven application

Open the pom.xml with your favourite editor . Delete the whole content and just paste this there.
(You can also just copy the parts you just need but to make sure we all make
the same mistakes, just change it all)

I have put a lot of comments in the pom.xml to explain each part so you can adapt it to suit your needs



Inside the main folder create another two subfolders


In the webapp folder, create another sub folder called WEB-INF

In the WEB-INF folder, create a new file called web.xml


For servlet 3.0, web.xml is not required. However, you need a way to specify your welcome file and also, the custom tags were not getting processed if i used .xhtml files instead of jsp
When we understand it more, we will know why it didn't work

So your entire directory structure will look like this:
The folder structure of the maven generated project is as shown below
Maven Web Application Directory Structure

Open the web.xml in your favourite editor and enter the following

In the webapp folder, create a new file called index.xhtml
Open it in your editor and enter the following

Now our basic application is almost complete.
Now cd into simplea folder by typing


make sure you run cd c:\home\setenv.bat if you closed you Command Prompt

then type

I all goes well, You will see the build successfull message on the screen.
That will create our war file:

Task 4. Installing the war file in glassfish


NOTE

If you shut it down, restart it using the instructions above . If you are doing that
make sure you run c:\home\setenv.bat before.

Now whiles Glassfish is running
Now go to

On the left sidebar, click on Applications
Click on the deploy button
On the next page, leave the selected radio button and click browse
Browse to C:\home\training\simplea\target and select simplea.war and click open.
When you return, leave all the defaults, scroll to the bottom and click ok.
That should take you back to the applications list page

Task 5. Enable and Run


On the applications list just tick checkbox for simplea and click enable.
When the notification comes that the application is snabled,
Just click launch to see the application

Task 5. Shut down Glassfish


type

then type

Coming up next. Designing the homepage and the Login.
Stay tuned

No comments: