Tuesday 2 August 2016

how to implement tokenized messages in ADF-Faces

    Some time ago I've posted an article about customizing error messages in BC4J. Now's the time to think more about the UI. From time to time there is a requirement to write a sentence or a string concatenation on the screen which won't be static. As an example: There are XYZ rows from which ABC are shown on the screen. Of course my application must be internationalized, I'm using the bundle file. I can split my sentence into three different entries in the bundle file but I must be sure that the syntax in each language is the same. And it mustn't be like this.

    The solution is simple and, as usually, no extra Java code. ADF-Faces allows to tokenize text message. My simple use case is to display two fields and write "a+b is aANDbSUM" at the bottom of the page. For that purpose I'll use af:format3 function. It is a part of EL expression and it can be mixed with other valid EL invocations inside.

    The code snippet (it is using static text and viewScope implicit variables just to simplify the example):
<af:outputText value="#{af:format3('the sum of {0} and {1} is {2}.', viewScope.v1, viewScope.v2, viewScope.v1+viewScope.v2)}" id="ot1" partialTriggers="it2 it1"/>

    And the screenshots of the code and of the running page: