http://www.developer.com/java/ent/article.php/3823376/JBoss-Tools-3-Developer-Guide.htm
JBoss Tools consist of the best Java frameworks and technologies placed together under the same "roof." Discovering JBoss Tools is like exploring a cave; at first everything seems unknown and complicated, but once you become familiar with the main features of the tools, you will start to feel at home.
Throughout this article, you will follow the "learning by example" technique, and you will develop a completely functional JSF application that will represent a JSF registration form as you can see in Figure 1. These kinds of forms can be seen on many sites, so it will be very useful to know how to develop them with JSF Tools.
The example consists of a simple JavaServer Pages (JSP) page, named register.jsp, containing a JSF form with four fields (these fields will be mapped into a managed bean), as follows:
The information provided by the user will be properly converted and validated using JSF capabilities. Afterwards, the information will be displayed on a second JSP page, named success.jsp.
Speaking of the JSF life cycle, you should know that every JSF request that renders a JSP involves a JSF component tree, also called a view. Each request is made up of phases. By default, JSF has the following phases (shown in Figure 2):
We end this overview with a few bullets regarding JSF UI components:
Figure 4 is an example of how to configure your JSF project at this step. At the end, just click the Next button:
This step allows you to set the servlet version (Servlet Version field), the runtime (Runtime field) used for building and compiling the application, and the server where the application will be deployed (Target Server field). Note that this server is in direct relationship with the selected runtime. Figure 5 shows an example of how to complete this step (click on the Finish button):
When the editor is available, it will appear as in Figure 6.
This step allows you to set the servlet version (Servlet Version field), the runtime (Runtime field) used for building and compiling the application, and the server where the application will be deployed (Target Server field). Note that this server is in direct relationship with the selected runtime. Figure 5 shows an example of how to complete this step (click on the Finish button):
When the editor is available, it will appear as in Figure 6.
Another way to create a new page is selecting the View Template icon (fourth from the top) from the toolbar placed in the top-left corner of the Diagram view and, following that, clicking inside the Diagram view surface.
As you have seen in the introduction of this article, you want to develop a registration form (register.jsp) with four fields representing name (java.lang.String), age (int), birth date (java.util.Date), and phone number (register.PhoneNumber) of the user. Each of these fields can be encapsulated as a property of a managed bean that can be developed by following these steps:
Now that you have created the managed bean stub, it is time to create the bean's properties. For doing this, you can follow these steps:
An interesting aspect is represented by the Your Phone Number field, because this field is mapped to another managed bean named register.PhoneNumber. This managed bean has three properties used for extracting from the inserted phone number, the country code (countryCode property), area code (areaCode property), prefix number (prefixNumber property), and a property for keeping the entire phone number (allNumber property). These four properties are all strings (java.lang.String). Now, use what you have learned so far to create this managed bean with JSF Tools (for an alias name, use the phoneNumber text).
In the end, you should have the two managed beans as shown in Figure 19, Figure 20, and Figure 21.
Finally, save the project state by selecting the Save option from the File main menu.
If you want to see the generated code source for your beans, you can double-click the Managed-Bean-Class link that appears in the right panel of the Tree view when you select a bean from the left panel of the same view (as shown in Figure 22).
You can also open a bean from the Package Explorer view by expanding the project node and double-clicking on the bean's name.
Now, creating a custom converter can be done like this:
Edit the converter-generated source code by clicking on the Converter-Class link that appears in the right panel of the Tree view (as shown in Figure 24).
Note that you have to manually insert the behavior of the getAsObject and getAsString methods. For example, you convert the inserted phone number into a register.PhoneNumber object by adding the following code to these methods:
Now, creating a custom validator can be done like this:
Edit the validator-generated source code by clicking on the Validator-Class link that appears in the right panel of the Tree view as shown in Figure 26.
Notice that you have to manually insert the behavior of the validate method. For example, you validate the inserted phone number by adding the following code to this method:
JBoss Tools 3 Developer Guide
June 3, 2009
Click here for larger image
Figure 1: JSF Registration Form
Overview of JSF
Java Server Faces (JSF) is a popular framework used to develop user interfaces (UIs) for server-based applications (it is also known as JSR-127 and is a part of J2EE 1.5). It contains a set of UI components totally managed through JSF support, like handling events, validation, navigation rules, internationalization, accessibility, customizability, and so on. In addition, it contains a tag library for expressing UI components within a JSP page. The JSF features mentioned here are:
Click here for larger image
Figure 2: JSF Default Phases
Creating a JSF Project Stub
In this section you will see how to create a JSF project stub with JSF Tools. This is a straightforward task that is based on the following steps:
After a few seconds, the new JSF project stub is created and ready to take shape! You can see the new project in the Package Explorer view.
Click here for larger image
Figure 3: Create a JSF Project
Click here for larger image
Figure 4: Configure Your JSF Project
Click here for larger image
Figure 5: Set the Servlet Version, the Runtime, and the Server
JSF Project Verification
Before starting to develop a JSF application, you can take advantage of an important facility of JSF Tools: the ability to verify and report any dysfunction that may affect the application flow. This facility, known as the JSF Project Verification, will verify the application when it is saved and help you to easily identify errors. For this, you can follow these steps:
JSF Application Configuration File
A very useful facility of JSF Tools is an editor especially designed for managing everything that is related to the JSF main configuration file (faces-config.xml). You can start this editor like this:
Click here for larger image
Figure 6: JSF Tools Editor for Managing faces-config.xml
Creating a JSF Project Stub
In this section you will see how to create a JSF project stub with JSF Tools. This is a straightforward task that is based on the following steps:
After a few seconds, the new JSF project stub is created and ready to take shape! You can see the new project in the Package Explorer view.
Click here for larger image
Figure 3: Create a JSF Project
Figure 4 is an example of how to configure your JSF project at this step. At the end, just click the Next button:
Click here for larger image
Figure 4: Configure Your JSF Project
Click here for larger image
Figure 5: Set the Servlet Version, the Runtime, and the Server
JSF Project Verification
Before starting to develop a JSF application, you can take advantage of an important facility of JSF Tools: the ability to verify and report any dysfunction that may affect the application flow. This facility, known as the JSF Project Verification, will verify the application when it is saved and help you to easily identify errors. For this, you can follow these steps:
JSF Application Configuration File
A very useful facility of JSF Tools is an editor especially designed for managing everything that is related to the JSF main configuration file (faces-config.xml). You can start this editor like this:
Click here for larger image
Figure 6: JSF Tools Editor for Managing faces-config.xml
The Diagram View
From this view, you can easily manage pages and transitions between the application's pages (navigation rules). You can create, modify, or delete a page/transition in just a few seconds, and you will always have a clear image of the application flow, thanks to the graphical representation mode.
Creating Pages
As an example, let's create two pages named register.jsp and success.jsp. Usually, you would have had to edit these files by hand to describe the pages flow, but using Diagram view features, you can do it visually. For this, you have to apply the following steps:
Click here for larger image
Figure 7: The New View... Option from the Contextual Menu
Click here for larger image
Figure 8: The New View Window
Click here for larger image
Figure 9: The Diagram View Surface Adding Navigation Rules
Now, it is time to connect the two pages by creating a transition (navigation rule) from register.jsp to success.jsp. To do this, follow these steps:
Click here for larger image
Figure 10: Result of Clicking on register.jsp and then success.jsp
The Tree View
Maybe the most important view of this editor is the Tree view, which is made of a set of nodes that put the configuration file in a graphical and easy-to-use manner. Just with a few clicks, we can create and modify the properties of the JSF components by skipping the annoying process of manually editing the configuration file. In this section you will see how to exploit this view for managing the most important components of a JSF application, such as managed beans, converters, and validators. The Tree view can be seen as shown in Figure 11.
Click here for larger image
Figure 11: The Tree View
Working with Managed Beans
A very powerful facility of the Tree view is the support for managed beans. Now, you can create, modify, and delete managed beans without touching the source code. Next, you will see how to create beans properties, how to create getter and setter methods, and how to obtain the generated source code in just a few steps.
Click here for larger image
Figure 12: The New Managed Bean Window
Click here for larger image
Figure 13: The New Managed Bean Window Will Generate the Managed Bean
Click here for larger image
Figure 14: The Add Property Window
Click here for larger image
Figure 15: Generating the New Property
For primitive types (including strings), the value kind is recommended to be set as value. For objects, it is recommended to set it as null-value. List-entries and map-entries values are also supported.
Click here for larger image
Figure 16: The Add Property Window for Mapping User Age
Click here for larger image
Figure 17: The Add Property Window for Mapping User Birth Date
Click here for larger image
Figure 18: The Add Property Window for Mapping User Phone Number
Click here for larger image
Figure 19: Faces Config Editor View of Two Managed Beans
Click here for larger image
Figure 20: Faces Config Editor View of personBean
Click here for larger image
Figure 21: Faces Config Editor View of phoneNumber
Click here for larger image
Figure 22: The Generated Code Source for Your Beans
In addition, JSF Tools allows you to add existing Java Beans to the configuration file. This is a very useful facility because it allows you to import existing Java Beans so that you don't have to recreate them from the start. For this, you select the Managed Beans node (in Tree view) and click the Add button in the right panel. In the New Managed Bean wizard, you browse to the existing Java Bean, click the Next button (adding an exiting bean will deactivate the Generate Source Code checkbox and activate the Next button), select the properties to be imported (from the Managed Properties wizard), and click on the Finish button.
Working with Custom Converters
Creating and registering a custom converter is a very elegant method for accomplishing conversion issues that can't be resolved by JSF default converters. For example, in your registration form, you need the following conversions:
Click here for larger image
Figure 23: The Add Converter Window
Click here for larger image
Figure 24: Edit the Converter-Generated Source Code
import java.util.StringTokenizer;
...
public Object getAsObject(FacesContext arg0, UIComponent arg1,
String arg2) {
// TODO Auto-generated method stub
System.out.println("//getAsObject method//");
StringTokenizer st = new StringTokenizer(arg2,"-");
PhoneNumber pn = new PhoneNumber();
try{
pn.setAllNumber(arg2);
pn.setCountryCode(st.nextToken());
pn.setAreaCode(st.nextToken());
pn.setPrefixNumber(st.nextToken());
}catch (Exception e){ return pn; }
return pn;
}
public String getAsString(FacesContext arg0, UIComponent arg1,
Object arg2) {
// TODO Auto-generated method stub
System.out.println("//getAsString method//");
if(arg2 != null)
{
String value = ((PhoneNumber)arg2).getAllNumber();
return value;
}
return "";
}Finally, save the project state by selecting the Save option from the File main menu.
Working with Custom Validators
When JSF default validators don't satisfy your application's needs, it is time to implement a custom validator. For example, for your registration form, you have the following situation:
Click here for larger image
Figure 25: The Add Validator Window
Click here for larger image
Figure 26: Edit the Validator-Generated Source Code
import javax.faces.application.FacesMessage;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
public void validate(FacesContext arg0, UIComponent arg1, Object arg2)
throws ValidatorException {
// TODO Auto-generated method stub
System.out.println("//validate method//");
String value = ((PhoneNumber)arg2).getAllNumber();
String countryCode = "^[0-9]{1,2}";
String areaCode = "( |-|(){1}[0-9]{3}( |-|)){1}";
String prefixNumber = "( |-)?[0-9]{3}";
String number = "( |-)[0-9]{4}$";
Pattern mask = Pattern.compile(countryCode + areaCode +
prefixNumber + number);
Matcher matcher = mask.matcher(value);
if (!matcher.find()){
FacesMessage message = new FacesMessage();
message.setDetail("Your phone number is not valid!");
message.setSummary("Your phone number is not valid!");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
}Finally, save the project state by selecting the Save option from the File main menu.
In addition to this, JSF Tools allows you to create custom referenced beans (note that referenced beans are used very rarely and they basically are Java Beans available in a JSF scopethe application must create an instance of the referenced bean and place it in the desired scope). For this, select the Referenced Beans node (Tree view) and click the Add button from the right panel. In the Add Referenced Bean wizard, fill up the Referenced-Bean-Name with a name for your referenced bean and the Referenced-Bean-Class with the full name of the referenced bean class. Optionally, click on the Referenced-Bean-Class link if you want to customize the referenced bean stub class. Click Finish.
The Source View
In the Source view, you can see the source of the faces-config.xml document. This source is synchronized with the other two views, so you will always see the reflection of your modifications. This view allows you to modify the configuration file manually or by using the Outline view that displays a tree view of it (see Figure 27).
Click here for larger image
Figure 27: Using the Outline View
Summary
In this article, you have seen how to use JSF Tools for modeling the most important components of a traditional JSF project. You have developed a complete functional JSF project by using the most important skills of JSF Tools like managed beans, converters, validators.
About the Book Excerpt
This article is an excerpt from the book, JBoss Tools 3 Developers Guide, by Anghel Leonard, published by Packt Publishing. The book teaches Java developers how to develop JSF, Struts, Seam, Hibernate, jBPM, ESB, web services, and portal applications rapidly using JBoss Tools for Eclipse and the JBoss Application Server.
About the Author
Anghel Leonard is a senior Java developer with more than 12 years of experience in Java SE, Java EE, and the related frameworks. He has written and published dozens of articles about Java technologies and two books about XML and Java (one for beginners and one for experts).