Showing posts with label jsf. Show all posts
Showing posts with label jsf. Show all posts

Tuesday, April 9, 2013

JSF 2 Tips & Techniques - JSF Messages, Resource Bundle, and Validation Messages

Overriding default JSF error / warning Messages

Message bundle is used to override jsf's default error / warning messages. Declare message-bundle in your faces-config.xml and write new messages for default keys. You can define list of JSF defined messages from JSF specification under "Localized Application Messages"

Resource Bundle

Localized Resource bundle is defined with resource-bundle tag inside faces-config at global level or it is defined at the page level with loadBundle tag. Define resource bundle properties file in src/main/resources folder (You use any pacakge hierarchy e.g. if you want to define your resource bundle something like com.abc.view.ResourceBundle.properties then you needed to define corresponding folder hierarchy in your resource folder which will be src/main/resources/com/abc/view/ResourceBundle.properties. 

After defining your resource bundle, register it either on the page through f:loadBundle tag or globally in faces-config file using resource-bundle tag. 

Then Use it in your page like #{rb['app.title']}

Validation Messages using Bean Validation API

If you want to override default messages of Bean validation api or want to introduce new messages, then create ValidationMessages.properties file at root level i.e. at src/main/resources folder.

Tuesday, October 30, 2012

Registering Custom JSF Renderer

JSF has a very flexible architecture by providing hooks to plugin custom implementation for components, renderers, converters, validators etc. Today i will discuss the way to plugin own renderer without touching any other thing. Usually in every application, there are some properties which are set for ui components in order to make look & feel consistent throughout the application and these properties are set on each page for components. If we override renderer and set these properties as default properties, a lot of burden from developers end is removed. Moreover, it will also provide an easy way to change it in future at a single point instead of each page, so this is very handly thing to provide own renderers.
I am overriding a renderer for input text component of Prime faces. This renderer will not do anything special and will just print a statement on console. Here are the steps to follow:

  • First override the renderer of the component in which we want to provide custom rendering. Prime faces' User guide is an excellent reference source for each component e.g. for InutText, you will find all information about component's implementation classes and you will find there that org.primefaces.component.inputtext.InputTextRenderer is a default renderer for prime faces' input text component.



  • Create a custom renderer class which will extend above renderer and override the method in which you want to provide custom implementations. You can also download source code and can study default implementation of renderer. It will provide you good understanding how to do things. Sample implementation is given below:
     public class MyInputTextRenderer extends InputTextRenderer{
    @Override
     protected void encodeMarkup(FacesContext context, InputText inputText) throws IOException {
        System.out.println("encodemarkup");
        super.encodeMarkup(context, inputText);
    }
    }
              • Third and last thing is to register this custom renderer in jsf application. For that you will have to provide following entries in faces-config.xml:

                 
              component-family and renderer-type will help framework to understand for which component, custom renderer will be used

                          Tuesday, December 29, 2009

                          Programmatically Navigating to another page in adf / jsf

                          Here is the sample code which can be used to programmatically navigating to another page in jsf or adf

                          FacesContext fctx = FacesContext.getCurrentInstance();
                          Application application = fctx.getApplication();
                          NavigationHandler navHandler = application.getNavigationHandler();
                          navHandler.handleNavigation(fctx,null, "name of navigation case");

                          Friday, December 25, 2009

                          JSF Life cycle Tip # 1

                          Usually people think that by setting the attribute "immediate" true, bypasses the validation life cycle but this is not true in every case. If you talk about CommandButton and CommandLink, then its true and after "Apply Request Values" phase, request processing cycle shifts to "Render Response" phase thus skipping "Process Validation","Update Model Values", and "Invoke Application". ActionListener and action method associcated with CommandButton / CommandLink is executed in "Apply Request Values".
                          When we talk about editableValueHolder like InputText, and other controls, setting immediate "true" does not bypass the validation or other phases. Infact it just prioritize the control for validation e.g. if you want to validate one input control first than others, then you can set "immediate" for that control to "true".