Q: What is
the use of immediate attribute?
UIInput components and command components can
set the immediate attribute to true. This attribute,
when set to true, forces the conversion and
validation phase to occur earlier in the lifecycle,
during the apply request values phase. In other
words, if some components on the page have their
immediate attributes set to true, then the validation,
conversion, and events associated with these
components will be processed during apply request
values phase.
The immediate attribute can be used for the
following purposes :
a. Immediate attribute, when set to true, allows
a commandLink or commandButton to process the
back-end logic and ignore validation process
related to the fields on the page. This allows
navigation to occur even when there are validation
errors.
b. To make one or more input components "high
priority" for validation, so validation
is performed, if there is any invalid component
data, only for high priority input components
and not for low priority input components in
the page. This helps reducing the number of
error messages shown on the page.
For example :
In the code below,
button performs navigation without validating
the required field.
<h:inputText id="it" required="true"/>
<t:message for="it"/>
<t:commandButton value="submit"
immediate="true" action="welcome"/>
c. In the code below, validation is performed
only for the first component when button is
clicked in spite of being both the input components
required.
<h:inputText id="it1" immediate="true"
required="true"/>
<h:inputText id="it2" required="true"/>
<t:message for="it1"/>
<t:message for="it2"/>
<t:commandButton value="submit"
action="welcome"/>
Q: Explain the usage of immediate attribute
in an application.
Take an example of shopping cart application
in which a page contain quantity fields for
each product and two hyperlinks "Continue
Shopping" and "Update quantities".
Now we have set the immediate attributes to
"false" for all the quantity fields,
"true" for "Continue Shopping"
hyperlink and "false" for "Update
quantities" hyperlink. If we click the
"Continue Shopping" hyperlink, none
of the changes entered into the quantity input
fields will be processed. If you click the "Update
Quantities" hyperlink, the values in the
quantity fields will be updated in the shopping
cart.
Q: How to get the error messages displayed?
Error messages can be displayed using "message"
and "messages" tag. "message"
tag is used to display single error message
for a particular component. The ID of the component
for which the message is to be displayed is
specified is specified in "for" attribute
of the message tag.
Error messages for all the components can be
displayed at a place using "messages"
tag. It supports two layouts "table"
and "list". List layout shows all
the messages in a line and table layout places
all the messages in a tabular format.
Q: How to avoid that all the messages are shown
on the same line?
"messages" tag displays all error
messages in a line because the default layout
it supports is "list". This tag also
supplies one more value "table" for
"layout" attribute which displays
all error messages in a tabular format i.e.
in subsequent lines. So specifying explicitly
the value of "layout" attribute to
"table" can solve the problem of displaying
all messages in the same line.
Q: How can we replace the JSF Standard Error
Message?
Create the message bundle file and set the value
of the key for a particular type of error specified
in JSF specification.
For example, JSF specification supplies value
"Value is required" for key "javax.faces.component.UIInput.REQUIRED"
so replace the value in our message bundle file
similar to the following :
javax.faces.component.UIInput.REQUIRED=
Please enter the required value.
Register the message bundle within <application>
tag in the configuration file (faces-config.xml)
and restart the server. Now when we use message
or messages tag in the view page then the value
specified in this message bundle file for a
particular error is displayed.
Q: How we can change the appearance
of error messages in a JSF Page?
The appearance can be changed by any of the
two methods :
Using "style" attribute or "styleClass"
attribute. "style" attribute is used
to set the CSS style definition for the component
while styleClass attribute is used to set the
CSS class for the component. Using "styleClass"
attribute is same as html "class"
attribute.