Search This Blog

Thursday, September 2, 2010

JSF 1.2 - "Hello World" in NetBeans (with *.jsp pages)


Requirements:
  • installed Java (description here)
  • installed and configured NetBeans (description here)
  • installed and configured Tomcat for the NetBeans (description here)
You will learn:
  • how to create "Hello World" application in JSF 1.2 using NetBeans

Note 1: The following post and example show classic JSF 1.2 application with a view technology as .jsp pages. I do not use any additional libraries and extensions for the standard JSF libraries.

Note 2: I use standard capabilities of IDE platform to generate the project, without those thousands automated plugins which do everything for the user. Sometimes configuration and usage of a plugin is a "pain in the ..." - in my opinion it is worth to know how to deal with a standard set of tools, and after that there is always a time to faciliate our life ;-)

In the previous post I described how to generate working sample of JSF 1.2 application using Eclipse and its capabilities. I decided to check how it looks like using NetBeans. And this was a nice surprise for me - it is easier than in Eclipse. I will show how to generate a working JSF application similar to application created under Eclipse.

Assumptions:
1. Installed Java (description here).
2. Installed and configured NetBeans (description here).
3. Installed and configured Tomcat for the NetBeans (description here).

Step 1: creating and configuring web application.

Press "Next" and on the next screen configure some basic options like name and directory for the project.



Press "Next" and on the next screen configure runtime for the project.

Press "Next" and on the next screen add JSF capabilities to the project. Select "JavaServer Faces" option and set up JSF 1.2 libraries.

Step 2: project is configured.

Screenshot below shows the project structure generated by NetBeans.


That's all. Project is ready to deploy on the server. You don't need to add and configure JSF or JSTL libraries like under Eclipse. Those libraries are already in the project. Anyway in order to show that everything works the same we will modify generated project and write some piece of an application like under Eclipse.


Step 3: creating application.

Now we are ready to implement some sample JSF application. First we should create a package named com.firstjsf.backingbeans in the "Source Packages" directory. Second we should create two classes inside the package named HelloMessageBean and WelcomeBean. Third we should modify generated faces-config.xml file by adding there our classes and navigation rules. Everything should look like this:

In the faces-config.xml file we created entries for the navigation rules for not existing yet web pages. Let's create those web pages (index.jsp and message.jsp) in the Web Pages directory:


Step 4: deploying on server and running the application.

We have to click only one icon - NetBeans will start the Tomcat with our application deployed:



Results of application running can be visible in the console. We don't have to directly tell the Tomcat what application to deploy like under Eclipse - this step is done automatically. Moreover we don't have to start the browser manually to see the application. Browser will be also started automatically with proper URL pointing to our application.

What next? This simple working project can be used as a base for further learning about JSF. For example we can change the view technology from .jsp to facelets. We can add libraries which extend our standard tags by adding tags with ajax support, i.e RichFaces.

No comments:

Post a Comment