News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

JSF Interview Questions - Part - 3

Started by ganeshbala, Jul 05, 2008, 06:39 PM

Previous topic - Next topic

ganeshbala

JSF Interview Questions


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

3. What is the significance of properties file (Resource Bundle) and how to use this in our JSF page?

      Properties file is a collection of param=value pairs. This provides a great benefit to the application like we can modify these values easily and there is no need to change the JSP file. For example, we can create "message.properties" like :

      prompt=Enter Your Name:
      greeting_text=Welcome In india
      button_text=Submit

      Now edit the configuration file using <message-bundle> element which tells the application where the message resource file is located.
      <application>
       <message-bundle>india.messages</message-bundle>
      </application>

      Now, message resource bundle is loaded first using core tag <f:loadBundle> in view page. That loads the bundle and stores it in the request scope.
      <f:loadBundle basename="india.messages" var="message"/>

      We can now use this in our JSP like below :
      <h:outputText value="#{message.prompt}"/>



4.  How can I use several configuration resource files in one single application?
   
   JSF finds configuration file or files looking in context initialization parameter, javax.faces.CONFIG_FILES  in web.xml, that specifies one or more paths to multiple configuration files for your web application. These multiple paths must be comma separeted. The important point to remember is not to register /WEB-INF/faces-config.xml file in the web.xml. Otherwise, the JSF implementation will process it twice. For example, make changes in web.xml like below :

      <context-param>
      <param-name>javax.faces.CONFIG_FILES</param-name>
      <param-value>
      /WEB-INF/test1-config.xml,/WEB-INF/test2-config.xml
      </param-value>
      </context-param>


       
5.  Can we use a different configuration resource file in place of traditional "faces-config.xml" file in our application?
      JavaServer Faces technology provides an XML document for configuring resources. This file is used to register application's resources, such as validators, converters, managed beans, and navigation rules. This application configuration resource file is usually called faces-config.xml. You can have more than one application configuration resource file but it must be valid against the DTD located at http://java.sun.com/dtd/web-facesconfig_1_0.dtd. Now register the file within context-param element in web.xml file.


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


     
6. 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.
       
7. 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.
       
8. 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.


9. When to create and use custom convertors?
The main reasons behind creating our converter are :

   1. When we want to convert a component's data to a type other than a standard type
   2. When we want to convert the format of the data.
     
       

10. What are the steps of creating and using custom converter in our application?
Creating and using a custom converter requires the following steps :



Steps to follow :

   1. Create a class that implements javax.faces.converter.Converter interface.
   2. Import necessary packages and classes.
   3. Implement two abstract methods "getAsObject()", "getAsString()" provided by Converter interface. getAsObject() method converts the String (User Input) to Object and getAsString() method converts the Object to String to send back to the page.
   4. Register the converter class in configuration file (faces-config.xml) adding <converter> element. This element has child elements <converter-id> (name of the converter to be used while programming )and <converter-class> ( name of the converter class which we have created).
   5. Create view page where <f:converter> tag is used with attribute "converterId" which specifies the name of the converter which we have specified in <converter-id> element of <converter> element in "faces-config.xml" file.
   6. Use <h:message> tag to display the error message.
     

11. What are the ways to register the custom converters in faces context?
After creating custom converter class implementing Converter interface it needs to register in the faces context. This can be done in one of the two ways :

   1. Register the converter class with the id. This id is used in <f:convertrer> tag in our view page (For example, JSP).

      <converter>
          <converter-id>ms_Converter</converter-id>
          <converter-class>ms_Converter</converter-class>
      </converter>


      Use this converter in the view page as :
      <h:inputText id="input_text">
          <f:converter  converterId="ms_Converter" />
      </h:inputText>


       
   2. Register the converter class to handle all "Email" objects, for example,  automatically.
      <converter>    <converter-for-class>Email</converter-for-class>    <converter-class>EmailConverter</converter-class>
      </converter>


      If  we register the EmailConverter class to handle all Email objects automatically then there is no need to use the <f:converter/> tag in view page. Use this converter as :
      <h:inputText id="phone" value="#{Bean.email}">
      </h:inputText>