Wednesday, 20 February 2013

Encoding issue with Adobe CQ5.4



Let us think some scenarios where UTF 8 Characters are included in fields. Say we have dialog which contains a textfield, whose value is used later as a request parameter in a servlet. The configuration goes as below.


The XML segment for this simple textfield is shown below (from the dialog.xml) :

                              <name             
                                        jcr:primaryType="cq:Widget" 
                                        allowBlanks="false" 
                                        defaultValue=" " 
                                        fieldLabel="locationdata.dialog.datapanel.name" 
                                        name="./name" 
                                        id="location-name" 
                                        allowBlank="{Boolean}false" 
                                        xtype="textfield"/> 



The dialog also contains a button which, when pressed, is calling a servlet creating a page. The page is created using the 'name' property shown above. Below you can see the JS segment performing the servlet call, when pressing the aforementioned button:

    var locationTagField = CQ.Ext.getCmp('location-tag'); 
      var languageField = CQ.Ext.getCmp('location-available-languages'); 
      var nameField = CQ.Ext.getCmp('location-name'); 
      var jsonURL = CQ.shared.HTTP.getPath() + '/_jcr_content.' + itemtype 
      + '.json?action=createprofilepage&generationlanguage=' 
      + languageField.getValue() + '&location=' 
      + locationTagField.getValue()[0] + '&title=' + nameField.getValue(); 
      var url = CQ.HTTP.externalize(jsonURL, true); 
      var json = CQ.Util.eval(url); 



Below you can see the Java code sgement related to the page creation:

    final String title = slingRequest.getParameter(Constants.PARAM_TITLE); 
    [...] 
    //parentPath and template values are provided as parameters to the relevant method. 
    return resolver.adaptTo(PageManager.class).create(parentPath, null, template, title); 
    [...] 



Issue: Once the page gets created, a special character like ü for Anüssel or Villünkl has encoding issue. E.g it becomes "Asüsersel" instead of "Asüsersel ".

 The above issue is usually a UTF-8. Let us see how to resolve this.

Solution:

It can be easily resolved as below. Set the encoding of the from that you are submit e.g. UTF-8 by the hidden field _charset_. What the default encoding is the different Sling Versions is detailed here. Apache Sling - Request Parameters[http://sling.apache.org/site/request-parameters.html] (see paragraph at the end about Character Encoding)

No comments:

Post a Comment