Search code examples
htmlgrailsbrowsergsp

Browser can not parse HTML properly - Grails 2


I am generating HTML from controller (Backend) but browser can not parse that HTML properly.

Controller code:

def getValue (returnIndex) {

        String dropDown = "<select name='" + returnIndex + "' style=''>"

        def CatArr = new BudgetViewDatabaseService().executeQuery("SELECT c.id,c.name AS categoryName FROM chart AS a LEFT JOIN crtClass AS b ON a.id=b.chart_class_type_id LEFT JOIN chart_group AS c ON b.id=c.chart_class_id WHERE c.status=1")
       
 if (CatArr.size()) {
            for (int i = 0; i < CatArr.size(); i++) {
                def catId = CatArr[i][0]
                def ProductArr
                ProductArr = new BudgetViewDatabaseService().executeQuery("SELECT id,accountCode,accountName FROM bv.crtMsr where status='1' AND chart_group_id='" + catId + "'")
                if (ProductArr.size()) {
                    dropDown += "<optgroup label='" + CatArr[i][1] + "'>"
                    for (int j = 0; j < ProductArr.size(); j++) {
                        dropDown += "<option value='" + ProductArr[j][1] + "' >" + ProductArr[j][1] + "  " + ProductArr[j][2] + "</option>"
                    }
                    dropDown += "</optgroup>"
                }
            }
        }

        dropDown += "</select>"
        return dropDown
    }

view page's code:

<div class="fieldContainer fcCombo">
      <label>
              My GL <b>:</b>
     </label>
  
    ${new CoreParamsHelperTagLib().getValue('formFourGLAccount')}
</div>

issue:

enter image description here

Generated HTML looks like:

enter image description here

When I am opening that HTML on edit mode from browser then its looks like:

    &lt;select name='formFourGLAccount' style=''&gt;&lt;optgroup 
label='Overige immateriële bezittingen'&gt;&lt;option value='0430' &gt;0430  Overige niet 
materiële bezittingen&lt;/option&gt;&lt;/optgroup&gt;&lt;optgroup label='Computers en 
computerapparatuur'&gt;&lt;option value='0210' &gt;0210  Computers en 
computerapparatuur&lt;/option&gt;&lt;option value='0211' &gt;0211  Afschrijving computers en 
computerapparatuur&lt;/option&gt;&lt;/optgroup&gt;&lt;optgroup label='Overige materiële 
bezittingen'&gt;&lt;option value='0250' &gt;0250  Overige materiële 
bezittingen&lt;/option&gt;&lt;option value='0251' &gt;0251  Afschrijving overige materiele 
bezittingen&lt;/option&gt;&lt;/optgroup&gt;&lt;optgroup label='Waarborgsommen'&gt;&lt;option 
value='0300' &gt;0300  Waarborgsommen&lt;/option&gt;&lt;/optgroup&gt;&lt;optgroup 
label='Deelnemingen in andere bedrijven'&gt;&lt;option value='0310' &gt;0310  Aandeel of belang 
 in andere bedrijven&lt;/option&gt;&lt;/optgroup&gt;&lt;optgroup label='Strategische langlopende 
beleggingen'&gt;&lt;option value='0320' &gt;0320  Strategische langlopende 
beleggingen&lt;/option&gt;&lt;/optgroup&gt;&lt;optgroup label='Verstrekte langlopende leningen 
(hypotheek ed)'&gt;&lt;option value='0330' &gt;0330  Verstrekte langlopende leningen (hypotheek
 ed)&lt;/option&gt;&lt;/optgroup&gt;&lt;optgroup label='Overige financiële 
bezittingen'&gt;&lt;option value='0340' &gt;0340  Overige financiële bezittingen&lt;/option&gt;&lt;/optgroup&gt;&lt;optgroup label='Voorraad'&gt;&lt;option 
          

          
                    

If I copy returns result (HTML) from controller and past it on browser manually then its working fine


Solution

  • You have not shown how that HTML is being rendered so it isn't clear specifically how to fix it, but what is happening is the content is being HTML encoded, which you do not want if you want the browser to evaluate the HTML tags.

    EDIT Based On Comment:

    <div class="fieldContainer fcCombo">
          <label>
                  My GL <b>:</b>
         </label>
    
        ${new CoreParamsHelperTagLib().getGLAccountExpanseBudgetForReconcilationOthersDropDown('formFourGLAccount')}
    </div>
    

    There is no good reason to create an instance of a taglib. You should invoke the tag as a GSP tag.

    You are returning hardcoded HTML from your controller as a model variable. That is a bad idea, but not what you are asking about. If you really do want to do that, then you will need to prevent the data from being HTML encoded in your GSP. You can use the raw(your unescaped html code here) method in your GSP as one way to avoid the encoding.