While working today I came across a strange situation.......
the requirement was something like I have to dynamically get some html elements and contents and put them in a variable which then I was supposed to put in a .jsp file.
It had to be dynamic so that the jsp would render different in different condition. Everything was working okay as supposed to but the dynamic HTML element which I was getting in String format was coming in such a way that it turned all the HTML tags in special characters.
i.e. <br /> was coming like <br />, So eventually while getting rendered in jsp page instead of it being a hypertext it was actually special characters and in browser all the tags were visible.
Hence if in my jsp if there is
and in my java code if there was -
then on browser it was showing this instead of proper rendering of hypertext tags -
Try this
<div id="variableDiv"><p><input type="text"/>Fill Something</p></div>
this was because the c:out was giving the value as-
Someone told me that I need to get it encoded and then decoded by url encoder or something but it was only getting more weird.......
However the help was here
after sometime what actually worked was this -
this unescapeHtml helped in preserving the hypertext values from turning into special character.
Also note that in the reverse case if you have to show the tags on browser but instead it's working as hypertext you can achieve it by StringEscapeUtils.escapeHtml(myHtmlVal); method.
Hope it helps
the requirement was something like I have to dynamically get some html elements and contents and put them in a variable which then I was supposed to put in a .jsp file.
It had to be dynamic so that the jsp would render different in different condition. Everything was working okay as supposed to but the dynamic HTML element which I was getting in String format was coming in such a way that it turned all the HTML tags in special characters.
i.e. <br /> was coming like <br />, So eventually while getting rendered in jsp page instead of it being a hypertext it was actually special characters and in browser all the tags were visible.
Hence if in my jsp if there is
<div id="fixedDiv">
<b>Try this</b>
<c:out value="${myHtml}" />
</div>
and in my java code if there was -
String myHtmlVal = "<div id="variableDiv"><p><input type="text"/>Fill Something</p></div>";
response.add("myHtml",myHtmlVal)
;
then on browser it was showing this instead of proper rendering of hypertext tags -
Try this
<div id="variableDiv"><p><input type="text"/>Fill Something</p></div>
this was because the c:out was giving the value as-
<div id="variableDiv"><p><input type="text"\/>Fill Something<\/p></div>
Someone told me that I need to get it encoded and then decoded by url encoder or something but it was only getting more weird.......
However the help was here
after sometime what actually worked was this -
String myHtmlVal = "<div id="variableDiv"><p><input type="text"/>Fill Something</p></div>";
response.add("myHtml",StringEscapeUtils.unescapeHtml(myHtmlVal));
this unescapeHtml helped in preserving the hypertext values from turning into special character.
Also note that in the reverse case if you have to show the tags on browser but instead it's working as hypertext you can achieve it by StringEscapeUtils.escapeHtml(myHtmlVal); method.
Hope it helps
No comments:
Post a Comment