How to handle newlines in a Struts form that relies on JavaScript

醉酒当歌 提交于 2019-12-25 00:38:29

问题


I have a Struts form which contains a Map:

private Map<Long, String> questionAnswers = new TreeMap<Long, String>();

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):

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);
}

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:

<c:forEach items="${myForm.questionAnswers}" var="questionAnswer">
    var textareaBoxName = "questionAnswer(" + '${questionAnswer.key}' + ")";
    var textareaBox = document.getElementsByName(textareaBoxName)[0];
    if (textareaBox) {
        $('textarea[name=' + textareaBoxName + ']').val('${questionAnswer.value}');
    }
</c:forEach>

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.

In the setQuestionAnswer 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 setQuestionAnswer method (using the String replaceAll 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).

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.


回答1:


Since ${questionAnswer.value} is put inside a JavaScript String literal, you need to escape it as you would do if you wanted a newline in a JavaScript literal: the lines Hello and World must be written as 'Hello\nWorld'. Look at commons-lang StringEscapeUtils escapeECMAScript method. In iddition to escaping the newlines, it will also escape tabs, apostrophes, etc.

Make this method an EL method, and use it directly into your JSP:

$('textarea[name=' + textareaBoxName + ']').val('${myFn:escapeJs(questionAnswer.value)}');

You might also generate the text areas statically instead of generating them using JavaScript:



来源:https://stackoverflow.com/questions/8539696/how-to-handle-newlines-in-a-struts-form-that-relies-on-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!