Académique Documents
Professionnel Documents
Culture Documents
1
JSF Edition
Beginner Tutorial
JSF Edition 4.3.1 2
Table of Contents
Chapter 1. Introduction 3
Chapter 8. Conclusion 19
Introduction
About the Beginner Tutorial
The Backbase Enterprise Ajax 4.3.1 - JSF Edition Beginner Tutorial demonstrates how to build a JSF Edition web application
with the JSF Edition. At the end of this tutorial you will have an application as shown in the following image:
Figure 1 – Form Response
Getting Answers
If you are working on a difficult problem that cannot be resolved using information in this document, try the following:
Intended Audience
This Beginner Tutorial is intended for JSF application developers who have not yet built an application with the JSF Edition.
We assume that application developers have some experience with JSF and Java. Additionally, a basic understanding of the
structure of JSF projects and how to create class files will be useful.
Project Creation
The software resources required to develop a Backbase JSF Edition web application are minimal: a text editor is sufficient to
create and edit the various classes and resource files. However, to assist the development process and ensure that all the
resources required to create a Backbase JSF Edition web application are available, a Backbase Eclipse plugin is available
that provides everything you need to create and maintain a Backbase JSF Edition web application.
For more information on the a Backbase Eclipse plugin, please refer to the Eclipse Plugins section in the
Application Development Guide.
In this tutorial, a new JSF Edition web application will be created using the Backbase Eclipse plugin.
As an alternative to using the Backbase Eclipse plugin to create your project, you can use the blank application
that is bundled with the JSF Edition. For more information, please refer to the Deployment Guide.
To create a JSF Edition web application, from the menu choose Backbase>File>New Project>JSF Edition Project to start the
Backbase - New JSF Project wizard.
Alternatively, you can access the Backbase - New JSF Project wizard from one of several native Eclipse IDE
menus:
In the wizard, expand the Backbase folder and choose JSF Edition Project to start the Backbase - New JSF
Project wizard.
The Backbase - New JSF Project wizard guides you through the project creation process. For more information, refer to the
section entitled Creating Projects in the Application Development Guide.
Startup Page
In a JSF Edition application, you will typically have a single web page that loads XML fragments dynamically. The client
sends a list of changed attributes and properties (the client delta) to the server, which then updates its component tree,
performs server-side business logic, and generates a response (the server delta) that is returned to the client. The response
is rendered inside the existing page. In other words, the processing of a request involves a request for data (which can also
be 'presentation' data) that results in an update of only the changed parts of the application.
A JSF Edition application will therefore contain one or more startup pages (generally a .jsp) and subviews that can be
loaded statically or dynamically. A startup page generally contains nothing more than some required application code, a
basic layout, and include directives to load default content. The Eclipse IDE JSF Edition 4.3.1 plugin generates the required
application code for you:
Example 1 – Backbase JSF Application Required Code
<!-- -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://www.backbase.com/2007/jsf" prefix="bjsf" %>
<html>
<body>
<f:view>
<bjsf:application>
<f:verbatim>
<xi:include
href="<%=application.getInitParameter("com.backbase.bjsf.CLIENT_BOOT_DIR")
%>/bindings/config.xhtml_btl.chameleon.xml" parse="xml"></xi:include>
<xi:include
href="<%=application.getInitParameter("com.backbase.bjsf.CLIENT_BOOT_DIR")
%>/bindings/www.backbase.com.2007.jsf.client/server.xml"
parse="xml"></xi:include> <bjsfc:server url="index.jsf" identify="id"
loadingMessage="__loadingMsg"></bjsfc:server> <b:loadingMessage
id="__loadingMsg">Loading...</b:loadingMessage>
</f:verbatim>
<%-- your code goes here --%>
</bjsf:application>
</f:view>
</body>
</html>
The XHTML comment that is the first line of the application forces Microsoft Internet Explorer to go into
quirks mode, which is essential to force the CSS border box model. Some client controls require this mode, so it
is essential that you leave this comment. For more information, please see the section on the box model in the
Application Development Guide.
Tag Libraries
At the beginning of the file, you must declare the JSP tag directives that declare the tag libraries used in the page:
• Core JSF tag library containing the custom tags for validation and event handling
• Backbase JSF Server Runtime tag library for Backbase JSF components
The elements prefixed with f and bjsf represent the JSF Core and Backbase JSF Server Runtime components. Although
we recommend that you keep the f and bjsf prefixes, you can define your own prefixes.
In JSF, the combination of components that make up a specific user interface screen is called a view. The JSF f:view
element is a non-visual element that acts as a container for all other components in the view. When you create a Backbase
JSF application, you must define a JSF f:view element as the first action element. This element must enclose all other JSF
action elements, otherwise they are not included in the view.
In a JSF Edition web application, the Backbase JSF Server Runtime and the Client Runtime are responsible for managing
communications between the web server and the client browser. The Client Runtime is installed as a web application in your
servlet container, and is downloaded transparently to the client on an initial request.
For the Backbase JSF Server Runtime application to locate the Client Runtime and render the generated output, use the
bjsf:application element. On the client, the bjsf:application element is rendered as one xmp element that
marks the space parsed and rendered by the Client Runtime in the browser. The default location of the Client Runtime is
taken from the web.xml configuration file.
The listing shows the simple case of a bjsf:application component wrapping the entire content of the
page. The result is that the bjsf:application component defines a single model space, meaning that all
the rendered markup is parsed by the Client Runtime. This is just one of many different application architectures
supported by the Backbase JSF Edition, and the possibility exists to mix various combinations of Backbase JSF,
plain JSF, BTL, XEL, and XHTML in a single page. This is achieved by mixing model and view spaces in a single
page. For more information, refer to the section entitled Application Architectures in the Application
Development Guide.
The f:verbatim component that is instantiated immediately after the bjsf:application component loads the client
resources. These resources can be customized to match requirements. For more information, refer to the topic entitled
Customization of Client Resources in the Application Development Guide.
Page Layout
It is now time to create a basic layout structure. In most applications, your layout can be broken down into navigation and
one or more content sections. When a link, for example, is clicked, then only the changed or new information is loaded into a
specific section of the layout.
With the JSF Edition, you can use the panelSet component in combination with panel components to partition your layout.
The panelSet creates a rectangular structure that can be separated into rows and columns. To be able to create a complex
layout, each row or column can be further subdivided. To create a simple layout, copy the following code fragment inside the
bjsf:application element (after the f:verbatim element) in your JSP:
In this tutorial, we add styling directly to elements. In a production environment, we would add CSS class
information using the class attribute.
Unless it is the root element inside the application component, the panelSet takes up 100% of the height and width of its
parent element. If the panelSet is the root element (as in this examples) a fullScreen attribute set to true is required
for the page to display correctly.
Any panelSet component can be divided into rows or columns by using either the rows or columns attribute. The size of
the row/column can be determined by setting pixel, percentage, or other value types. You can also use an asterisk (*) on one
row or column to specify that that particular row/column should take up any remaining space.
Once you have specified in your panelSet component that there will be three rows, you can use the panel component to
create the row. Children of each panel component represent the content in that row. We have only added outputText
components as the contents of the panel. This component simply adds text to your layout. The panel supports the XHTML
style attribute, so you can style each panel differently.
Let's say that you want to subdivide the middle row into three columns. Although you can use the rows and columns
attributes on the panelSet at the same time, it is not recommended. The best approach to create a more sophisticated
layout is to use panel components for the first and third rows and another panelSet for the row that we want to subdivide.
To accomplish this, copy and paste the following code fragment into the index.jsp file, replacing the second panel
component with three panel components nested inside a panelSet component:
Example 3 – panelSet complicated layout
...
<bjsf:panelSet columns="20% 60% 20%">
<bjsf:panel style="border: 1px solid black;">
<bjsf:outputText value="Row 2, Col 1" />
</bjsf:panel>
<bjsf:panel style="background-color: #eeeeee; border: 1px solid black;">
<bjsf:outputText value="Row 2, Col 2" />
</bjsf:panel>
<bjsf:panel style="border: 1px solid black;">
<bjsf:outputText value="Row 2, Col 3" />
</bjsf:panel>
</bjsf:panelSet>
...
In this way, you subdivide a specific row or column with a panelSet component that is a child of the original panelSet. You
can continue to nest panelSet components until you have a very complicated but still easily manageable layout.
Using Subviews
Now that you have a basic layout, you can start to populate each panel with f:subview elements that contain your
content. Standard JSF offers you a number of mechanisms to load files statically. You can use any of these mechanisms in a
JSF Edition application. In addition to static loading, the JSF Edition offers the ability to load content dynamically at
application runtime. Dynamic loading can be achieved by using the url attribute supported by many components, including
the panel. The value of this attribute points to a content JSP file to be loaded into the panel. To set this in motion, let's
remove our outputText component from the second column and add the url attribute:
Example 4 – panelSet complicated layout with url
...
<bjsf:panelSet columns="20% 60% 20%" fullScreen="true">
<bjsf:panel style="border: 1px solid black;">
<bjsf:outputText value="Row 2, Col 1" />
</bjsf:panel>
<bjsf:panel style="background-color: #eeeeee; border: 1px solid black;"
url="views/form.jsp" />
<bjsf:panel style="border: 1px solid black;">
<bjsf:outputText value="Row 2, Col 3" />
</bjsf:panel>
</bjsf:panelSet>
...
• In the WebContent folder of your JSF project, create a folder named views.
• In the views folder, create a new JSP file called form.jsp.
If you use the Eclipse IDE, you can choose Backbase>New>File>JSP to start the New JavaServer Pages wizard,
and then select New JSP File (Backbase Sub View) from the JSP template list.
The newly created file consists of a standard JSP code snippet that contains necessary tag library definitions and the JSF
f:subview element, which indicates that this file will be loaded as a subview.
For dynamically loaded subviews, the JSF Edition bjsf:view component must be the first child of the JSF
f:subview element, and all code must be nested within the JSF Edition bjsf:view component. You do not
need the JSF Edition bjsf:view component for statically included files.
Now we have our subview set up, but need to add some content. To see that the subview has been loaded dynamically, edit
the newly created file to match the following listing:
Example 5 – Subview With Form
<f:subview id="author">
<bjsf:view>
<bjsf:commandButton id="formSubmit" value="Submit Form" />
</bjsf:view>
</f:subview>
We can create a resource bundle and hook it up to the application in four simple steps:
Before creating the resource bundle, add a package named sample to your project.
In the Project Explorer, select the src Java Resource, choose New->Package from the context menu, and use
the wizard to create a package named sample.
Next, create a resource bundle named language_en.properties in the newly created sample package.
Select the newly created package, choose New->Other from the context menu, and use the wizard to create a
file named language_en.properties.
It is essential that you use the language_en.properties naming convention because we will later rely on the
basename (the information before the dot) to access the file.
In this file you create a series of name/value pairs that are separated by the equals (=) sign. Add the message labels in the
following code fragment to the file:
Example 6 – message bundle sample
Now we have to load this resource into our application. In your index.jsp page, add a line that implements the
loadBundle component:
<bjsf:application>
<f:verbatim>
<xi:include
href="<%=application.getInitParameter("com.backbase.bjsf.CLIENT_BOOT_DIR")
%>/bindings/config.xhtml_btl.chameleon.xml" parse="xml"></xi:include> <xi:include
href="<%=application.getInitParameter("com.backbase.bjsf.CLIENT_BOOT_DIR")
%>/bindings/www.backbase.com.2007.jsf.client/server.xml" parse="xml"></xi:include>
<bjsfc:server url="index.jsf" identify="id"
loadingMessage="__loadingMsg"></bjsfc:server> <b:loadingMessage
id="__loadingMsg">Loading...</b:loadingMessage>
</f:verbatim>
<bjsf:loadBundle basename="sample.language" var="msg" />
...
</bjsf:application>
The basename identifies a specific resource bundle, and its value is the fully-qualified name of the file. Thus, the sample
part of the value refers to the name of our newly created package, while language identifies the resource bundle with the
corresponding name.
In our example, there is only the one language_en.properties resource bundle, so language is
sufficient to identify this file uniquely. In a more complete localization scenario, multiple files will exist that
correspond to different locales (language_nl.properties, language_jp.properties, etc) and you
would have to append the locale code to language to identify the required resource bundle uniquely. For
example, script can be added to retrieve the locale from the request and assign the result to a variable for use in
the JSP:
Example 8 – Retrieving the Locale from the Request
The value of the var attribute, indicates the name you will use when binding a value to one of the messages. To complete
the connection between a field and a message, remove the hard coded value in the value attribute of our
commandButton and replace it with a reference to our message:
<f:subview id="author">
<bjsf:view>
<bjsf:commandButton id="formSubmit" value="#{msg.buttonLabel}" />
</bjsf:view>
</f:subview>
By doing this, we not only keep all our labels, validation messages, and strings in a single location, but we also prepare for a
situation when we need to display the same labels and messages in another language.
Save the changes, and refresh your browser to see the button label updated with the resource bundle value.
Backbase Forms
Developing Your Form
Now we can create the form itself by adding to the subview a form component, an outputLabel component (in conjunction
with a second message from our resource bundle), and an inputText component:
Example 10 – Subview With auhorName Form
<f:subview id="author">
<bjsf:view>
<bjsf:form id="authorForm">
<bjsf:outputLabel for="name" value="#{msg.outputLabel}" />
<bjsf:inputText id="name" />
</bjsf:form>
<bjsf:commandButton id="formSubmit" value="#{msg.buttonLabel}" style="padding-top:
20px; padding-bottom: 20px;" />
</bjsf:view>
</f:subview>
Reload your application once again, and you can see that you have now loaded the form into your application.
Figure 5 – panelSet Component With Form
Form Submission
Simply adding a form to your application does very little if it cannot be submitted. You might have noticed that the form
component only has an id attribute and some styling. This is because form submission is managed by the
commandButton component, not the form component. This way, the commandButton can determine which form(s) to
submit. On the commandButton, the value of the submitForms attribute determines which form to be submitted to the
server. The value of the submitForms attribute must be a comma separated list of id values of the form components:
Example 11 – commandButton With submitForms Attribute
<f:subview id="author">
<bjsf:view>
...
<bjsf:commandButton id="formSubmit" submitForms="authorForm"
actionListener="#{sampleBean.showAuthorPicture}" value="#{msg.buttonLabel}"
style="padding-top: 20px; padding-bottom: 20px;" />
</bjsf:view>
</f:subview>
Please note that form submission components, including commandButton, do not need the submitForms
attribute to be defined if the component resides inside the form. In that case, the component submits only the
form in which it is located.
By setting the value of the submitForms attribute to authorForm, we instruct the commandButton to submit this
particular form when the user clicks the commandButton.
If a form is nested inside another naming container, such as a f:subview, the value of the submitForms
attribute should be the id of the form concatenated (using colons) with the id of each of its ancestor naming
containers. Thus, for this example, the fully qualified value is author:authorForm. However, if the id of the
form is unique in the scope of the parent view, it can be uniquely identified using its unqualified id.
For more information, please refer to the description of naming containers in the section entitled Startup Page
in the Application Development Guide.
The actionListener listens for a click event on the commandButton. The value of the actionListener determines what
method the server calls when submitting the form. In our tutorial, the server executes a method called showAuthorPicture()
that is a member of a backing bean identified by sampleBean.
The sampleBean identifier for the Sample backing bean is defined by adding the following code fragment to
the faces-config.xml configuration file in the WEB-INF folder:
Example 12 – faces-include entry for the Sample class
<managed-bean>
<managed-bean-name>sampleBean</managed-bean-name>
<managed-bean-class>sample.Sample</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
To create the backing bean that contains the showAuthorPicture() method, copy and paste the following code to a new class
named Sample.java in the sample package.
package sample;
import javax.faces.event.ActionEvent;
import com.backbase.bjsf.component.UIBackbaseGraphicImage;
else if (actualInput.equals("irving")) {
image.setUrl("images/irving.jpg");
outputText = "";
}
else {
image.setUrl("images/question.jpg");
outputText = "We don't know this author.";
}
}
}
To run this example you will need to create a folder named images in the WebContent folder, and add
images with names that correspond to those in the Sample.java listing.
In the Sample class, we have set up a variable, actualInput, that we will use to store the value of our inputText
component. To accomplish this, we must bind the variable to the inputText component with the component's value
attribute:
<f:subview id="author">
<bjsf:view>
<bjsf:form id="authorForm">
<bjsf:outputLabel for="name" value="#{msg.outputLabel}" />
<bjsf:inputText id="name" value="#{sampleBean.actualInput}" />
</bjsf:form>
...
</bjsf:view>
</f:subview>
As a result of this binding, the value of the inputText component is stored in the actualInput server variable upon form
submission.
In the backing bean, after variable declarations and getters and setters, we declare the showAuthorPicture method, which is
the method executed by our event listener. In the showAuthorPicture method, an extended control statement checks the
value of the acutalInput variable. Based on this value, we set two items. First, the outputText will be populated if the
server does not recognize the value of the input field. We also set the url attribute of a graphicImage component - this
will allow us to display a picture of the correct author.
However, as with our outputLabel component, we have not yet created a mechanism to get our response values back
into our application. To display our response, we will bind our server variables to the outputText and graphicImage
application components:
Example 15 – graphicImage Binding
<f:subview id="author">
<bjsf:view>
...
<bjsf:panelGroup>
<bjsf:outputText id="outputText" value="#{sampleBean.outputText}" />
<bjsf:graphicImage binding="#{sampleBean.image}" />
</bjsf:panelGroup>
</bjsf:view>
</f:subview>
In the listing, the bjsf:panelGroup provides a more efficient alternative to the bjsf:panelSet and
bjsf:panel to group related components in a cell in the grid.
In the showAuthorPicture() method, the outputText variable and the url attribute on the graphicImage component
are updated. This makes the value visible in your application:
Figure 6 – Form With Successful Response
Most forms will include input fields requiring some input ot be entered. Sometimes the user should only enter a number, or a
date in a certain format, or a certain set of values. In the JSF Edition, form validation can be handled both client-side and
server-side. The JSF Edition supports Tomahawk validation, so you can easily create server-side validators.
Additionally, we provide a few mechanisms to inform the user that input must conform to a certain format. What would be
useful is to add error messages to instruct the user. We can accomplish this in a few different ways. First, we can use an
outputRequired and a toolTip component to instruct the user before they even enter the field:
<f:subview id="author">
<bjsf:view>
<bjsf:form id="authorForm">
<bjsf:outputLabel for="name" value="#{msg.outputLabel}" />
<bjsf:inputText id="name" value="#{sampleBean.actualInput}"
clientValidators="required" required="true" />
<bjsf:outputRequired for="name">
<bjsf:toolTip>
<bjsf:outputText value="#{msg.validationRequired}" />
</bjsf:toolTip>
</bjsf:outputRequired>
</bjsf:form>
...
</bjsf:view>
</f:subview>
The outputRequired component places an asterisk (*) next to a given component to indicate that it is mandatory. The
asterisk is matched with the component by having the same value in the outputRequired for attribute and the id
attribute of the input component. The toolTip component adds additional information when the user places the mouse
over the parent component of the toolTip, which, in this case, is the outputRequired component.
While this is useful information, and reflects good user interaction design, it has no relation to validation or the error
messages you might want to display in response to failed validation. To create proper error messages, you can use the
message component:
<f:subview id="author">
<bjsf:view>
<bjsf:form id="authorForm">
<bjsf:outputLabel for="name" value="#{msg.outputLabel}" />
<bjsf:inputText id="name" value="#{sampleBean.actualInput}"
clientValidators="required" required="true" />
<bjsf:outputRequired for="name">
<bjsf:toolTip>
<bjsf:outputText value="#{msg.validationRequired}" />
</bjsf:toolTip>
</bjsf:outputRequired>
<bjsf:message for="name" errorStyle="color:red" />
</bjsf:form>
<bjsf:commandButton id="formSubmit" actionListener="#{sampleBean.showAuthorPicture}"
submitForms="authorForm" value="#{msg.buttonLabel}" style="padding-top: 20px;
padding-bottom: 20px;" />
<bjsf:panelGroup>
<bjsf:outputText id="outputText" value="#{sampleBean.outputText}" />
<bjsf:graphicImage binding="#{sampleBean.image}" />
</bjsf:panelGroup>
</bjsf:view>
</f:subview>
With the message component, the value of the for attribute must match the id of the component to be validated. We
have also used the errorStyle attribute to give the message a bit of extra styling. Reload the form. If you do not enter a
value, the contents of the message component will be displayed:
The message that is displayed, "name": Value is required in this example, is defined by the following key/value
pairs in the MyFaces Message.properties file:
Example 18 – Validation Message Definitions
To customize this message, you override the MyFaces key/value pairs using a custom message bundle. For
more details, please refer to the topic entitled Validation Messages in the Application Development Guide.
Conclusion
That concludes our Beginner Tutorial. We've shown you how a JSF Edition application works, how to create a layout, add
static and dynamic files, as well as the basics of form submission, validation, and error message displaying. For more
information, please consult our Document Suite and Resource List in the Start Guide.
Contact Backbase
Community
The Backbase Developer Network http://bdn.backbase.com brings many developers resources such as the latest
releases, errata, updates, news and our free forum to your fingertips. Architects and evaluators will also benefit from the
insights and articles posted by Backbase and by the community.
Support
Free community-based support is available through our Backbase Developer Network discussion forums. Commercial
support is available through a Backbase yearly subscription, which includes product support and software licenses. This
support is useful for individuals and businesses requiring superior technical support, regular maintenance releases,
notification and alerts on product issues, knowledge base access, email or phone support, hot fixes, and an account
manager.
Training
Backbase training courses are designed for a whole range of developers, from AJAX beginners to enterprise developers.
Courses are available online, on site, and in classroom settings. Our online classes are live with real-time instructors.
Training attendees are encouraged to communicate in real time with the instructor. All of our instructors are AJAX experts
with extensive teaching experience.
Sales
For sales-related questions and inquiries, contact your local or regional office. For up-to-date information on our global sales
offices check: http://www.backbase.com/contact
You can also submit questions to our general email address at: contact@backbase.com .