JSF Interview Questions

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 re

JSF Interview Questions

JSF Interview Questions

        

  1. 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 :

    1. 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.
    2. 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"/>

      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"/>

     

  2. 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.
      
  3. 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.
      
  4. 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.