开发者

Escape '(ticks) in jsp

I have a JSP where I print some string by use of c:out. But I need to escape all ' (ticks) by an back slash.

Example: jsp

<span onclick=
    "document.getElementById('input').value+='<c:outvalue="${tag.title}" />'">
 <c:out value="${tag.title}" />
</span>

Prints for tag.title = test‘s

<span onclick="document.getElementById('input').v开发者_如何学Pythonalue+='test's'">
 'test's
</span>

But I need:

<span onclick="document.getElementById('input').value+='test\'s'">
 'test's
</span>

Is there any easy way to do this in jsp?


You can use Commons Lang, import StringEscapeUtils and use one of its methods (I think that would be escapeJavascript in your case)


In your getTitle() method you can return yourString.replaceAll("'","");

edit:

try

${fn:replace(yourString, "'", "")}


try

 ${fn:replace(text, "'", "")}

or add some jsp in and use string.replace()


This is probably a sign that a bit of refactoring would be advised.

Specifically stop outputting html via jstl tags and instead use if or when; or do a bigger refactoring.

I would probably use jQuery and assign click functions based on class (which could be taken from server side paramters)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜