Passing parameter in XML Resource Bundle and use it in Managed Bean

Sharing is Caring

If we are developing a Fusion Web application and we are thinking about the Multilingual application, then the best option I came to know is to use Resource Bundle properties of ADF. When we use resource bundle we have to use labels of Fields and Validation message or any kind of other custom messages from an XML file as Resource.xml.
So first you should know that how to configure resource-bundle in an ADF Application and there are plenty of posts about configuring Properties or List ResourceBundle.

In this post, I will show you that how to pass parameter in XML or how to use the Parametrized resource.

Suppose I  have a xml file for ResourceBundle reference Resource.xml

  1. <?xml version=“1.0” encoding=“windows-1252” ?>
  2. <bundle>
  3. <label>
  4.     <key>MessageCheck</key>
  5.     <value>Only %s %s %s %s %s allowed</value>
  6.  </label>
  7. </bundle>

Now I use it in managed bean to show a custom message and replace its parameters %s with any desired value then our code looks like this

  1. //To get String from XML key, resolvElDC is a method to resolve expression language
  2. String message = resolvElDC(“#{bundle[‘MessageCheck’]}”).toString();
  3. //here replace parameter(%s) in string message with your values
  4. String saveMsg = message.format(message, “,”, “/”, “@”, “_”, “%”);
  5. //Show FacesMessage
  6. FacesMessage msg = new FacesMessage(saveMsg);
  7. msg.setSeverity(FacesMessage.SEVERITY_INFO);
  8. FacesContext ctx = FacesContext.getCurrentInstance();
  9. ctx.addMessage(null, msg);
  10. // Code for resolvElDC method
  11. public Object resolvElDC(String data) {
  12.     FacesContext fc = FacesContext.getCurrentInstance();
  13.     Application app = fc.getApplication();
  14.     ExpressionFactory elFactory = app.getExpressionFactory();
  15.     ELContext elContext = fc.getELContext();
  16.     ValueExpression valueExp = elFactory.createValueExpression(elContext, data, Object.class);
  17.     return valueExp.getValue(elContext);
  18. }


Now run your code and see the updated message- Only %s %s %s %s%s allowed is now
 

Resource Bundle

 

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *