Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle newlines in a Struts form that relies on JavaScript
    primarykey
    data
    text
    <p>I have a Struts form which contains a Map:</p> <pre><code>private Map&lt;Long, String&gt; questionAnswers = new TreeMap&lt;Long, String&gt;(); </code></pre> <p>I have the normal getter and setter for this variable (not shown here), and I also have the getter and setter required for Struts to work (using String/Object):</p> <pre><code>public Object getQuestionAnswer(String questionId) { return getQuestionAnswers().get(questionId); } public void setQuestionAnswer(String questionId, Object answerText) { String answer = (answerText == null) ? "" : answerText.toString(); getQuestionAnswers().put(Long.valueOf(questionId), answer); } </code></pre> <p>In my JSP, I am dynamically generating the textareas that are used to enter the values for the map. This is all working fine. However, when the form is invalid, I need to dynamically generate the textareas again, and put the user text back into the textareas. I am currently repopulating the textareas like so:</p> <pre><code>&lt;c:forEach items="${myForm.questionAnswers}" var="questionAnswer"&gt; var textareaBoxName = "questionAnswer(" + '${questionAnswer.key}' + ")"; var textareaBox = document.getElementsByName(textareaBoxName)[0]; if (textareaBox) { $('textarea[name=' + textareaBoxName + ']').val('${questionAnswer.value}'); } &lt;/c:forEach&gt; </code></pre> <p>This works fine, except if you enter a newline in the textarea. Then a JavaScript error complains about an "Unterminated string constant". I am guessing the newlines are being performed instead of just read.</p> <p>In the <code>setQuestionAnswer</code> method, I put in some debugging and found that a newline entered in the textarea is being read as 2 characters, which converted into ints are 13 and 10 (which I believe are \r and \n). I tried replacing the the "\r\n" with just "\n" in the <code>setQuestionAnswer</code> method (using the String <code>replaceAll</code> method), but the same error occurred. I then tried replacing the "\r\n" with "%0A" (which I believe is the JavaScript newline). While this got rid of the JavaScript error, the textareas now have the "%0A" displayed instead of a newline. I tried all sorts of escaping and unescaping with no luck (note, I also want special characters to be preserved). </p> <p>Does anyone have any idea on how to preserve newlines and special characters in the textarea boxes on invalid submits? I need this to work in IE. And I would like to avoid anything hacky like using some special character/string to "represent" a newline which I then replace in JavaScript, etc.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload