如何在依赖JavaScript的Struts表单中处理换行符 [英] How to handle newlines in a Struts form that relies on JavaScript
问题描述
我有一个包含地图的Struts表单:
I have a Struts form which contains a Map:
private Map<Long, String> questionAnswers = new TreeMap<Long, String>();
我具有此变量的普通吸气剂和吸气剂(此处未显示),并且还具有Struts正常工作所需的吸气剂和吸气剂(使用String/Object):
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);
}
在我的JSP中,我正在动态生成用于输入地图值的文本区域.一切都很好.但是,当表单无效时,我需要再次动态生成文本区域,然后将用户文本放回文本区域.我目前正在像这样重新填充文本区域:
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>
这很好用,除非您在文本区域中输入换行符.然后,JavaScript错误抱怨"Unterminated string constant".我猜想正在执行换行符,而不仅仅是阅读.
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.
在setQuestionAnswer
方法中,我进行了一些调试,发现在textarea中输入的换行符被读取为2个字符,转换为int的是13和10(我相信是\ r和\ n) .我尝试用setQuestionAnswer
方法中的"\ n"替换"\ r \ n"(使用String replaceAll
方法),但是发生了相同的错误.然后,我尝试将"\ r \ n"替换为%0A"(我相信这是JavaScript换行符).尽管这消除了JavaScript错误,但是现在文本区域显示为%0A"而不是换行符.我尝试了各种转义和转义,但没有运气(请注意,我也希望保留特殊字符).
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).
有人对无效提交的文本区域框中的换行符和特殊字符保留任何想法吗?我需要这个才能在IE中工作.而且,我想避免任何麻烦,例如使用一些特殊的字符/字符串来表示"换行符,然后在JavaScript中替换换行符,等等.
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.
推荐答案
由于$ {questionAnswer.value}已放入JavaScript字符串文字中,因此您需要对它进行转义,就像想要JavaScript文字中的换行符一样:Hello和World行必须写为'Hello \ nWorld'.查看commons-lang StringEscapeUtils escapeECMAScript方法.除了转义换行符之外,它还会转义制表符,撇号等.
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.
将此方法设为EL方法,并将其直接用于您的JSP:
Make this method an EL method, and use it directly into your JSP:
$('textarea[name=' + textareaBoxName + ']').val('${myFn:escapeJs(questionAnswer.value)}');
您也可以静态生成文本区域,而不是使用JavaScript生成文本区域:
You might also generate the text areas statically instead of generating them using JavaScript:
这篇关于如何在依赖JavaScript的Struts表单中处理换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!