0

I am trying to generate JavaScript code using a Strut tag. However, the inserted content contains newline characters which should be output escaped as \n.

index.jsp:

<script type="text/javascript">
    document.getElementById("content<s:property value="id"/>").innerHTML = converter.makeHtml("<s:property escape='true' value='content' />");  
</script>

The expected result should be:

<script type="text/javascript">
    document.getElementById("content6302").innerHTML = converter.makeHtml("&gt; asdfasdf \n asddfadf");
</script>

The actual result is:

<script type="text/javascript">
    document.getElementById("content6302").innerHTML = converter.makeHtml("&gt; asdfasdf
 asddfadf");    
</script>

I.e. asddfadf"); is printed on a new line but should be \n asddfadf"); within the same line.

How can I get the escaped form \n in the generated HTML?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Does this help? https://stackoverflow.com/questions/58054/how-can-i-replace-newline-characters-using-jsp-and-jstl You might need to replace `"\\n"` by `"\n"`. I am not a a jsp coder. – Pinke Helga Dec 12 '18 at 12:52
  • I just don't want to print a new line instead of printing "\n", don't want to replace either. – santhosh kumar Dec 12 '18 at 12:57
  • The "headings" below the codes are confusing... ok, then replace `'\n'` with `'\\n'`, i.e. escape the backslash. – Pinke Helga Dec 12 '18 at 13:00
  • Since the question does not belong to JavaScript, I've removed the misleading SO tag. Try to select carefully the tags below the question to catch the right people answering your question. – Pinke Helga Dec 12 '18 at 13:12
  • Without further knowledge I guess: `` (cf. https://stackoverflow.com/questions/6866514/replace-part-of-a-string-in-a-struts2-tag) – Pinke Helga Dec 12 '18 at 13:22
  • i tried it , it is not working – santhosh kumar Dec 12 '18 at 13:29
  • {""} this is working – santhosh kumar Dec 12 '18 at 13:31
  • Since other people having the same issue might look for an answer, you may and should answer your own question providing a solution and even accept your own answer. I've formed your question into a propper SO question. – Pinke Helga Dec 12 '18 at 13:51
  • It looks as your source data does not contain newlines but already the escaped form `\n` and somehow that is translated into a newline character. If that is the case, please edit the question as well correct that fact. Try also `replace("\\n","\\\\n")`, this is propably the more accurate form even if jsp interprets `"\\\"` as a literal double slash as well. – Pinke Helga Dec 12 '18 at 13:55
  • What version of S2? The `` tag specifically has an `escapeJavaScript` property specifically for this. – Dave Newton Dec 12 '18 at 15:20

1 Answers1

0

like @Dave Newton posted, adding escapeJavaScript=true to property tag solves the problem

<s:property value='%{content}' escape='false' escapeJavaScript='true'/>