存入数据库中的字段是
<select name="${item.name}" id="${item.id}" style="${item.styles}" class="commonInput longtext ${item.classes}" >
< option value="${item.value}" >
</option >
</select >
显示在页面需要显示为如下:
<select name="${item.name}" id="${item.id}" style="${item.styles}" class="commonInput longtext ${item.classes}" >
< option value="${item.value}" >
</option >
</select >
因此在后台用replaceAll方法转换了"<" ,">"。
然后页面接收了一下:
<script type="text/javascript">
var isModify = "${isModify}";
if(isModify == 1){
document.getElementById("textarea1").innerHTML = "${contentTemp}";
alert("success");
}
</script>
然后在firebug里报错鸟,unterminated string literal
Other...
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在使用replaceAll()方法时需要注意,要用一个值去接收才能获取到转换后的字符串。看replaceAll()的API:
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".
|
content.replaceAll(">", ">").replaceAll("<", "<");
还以为直接这样content内容就会变了。。。怎么运行都不对。。。。(╯‵□′)╯︵┻━┻
妈蛋真是被自己蠢哭了。。。QAQ
---------------------------------------------------------------------------------------------------------------------------------------------------------------
发现问题是因为换行识别不了,不是一个完整的字符串,因此需要将/r/n转换一下。
@Override
protected String handleEdit(HttpServletRequest request,
HttpServletResponse response, Long objectId) throws Exception {
if(objectId != null ){
Element element = elementManager.getByObjectId(objectId);
String temp = element.getContent().replaceAll(">", ">").replaceAll("<", "<");
temp = temp.replace("\"", "\\"+"\"");
temp = temp.replaceAll("\r\n", "<br> ");
request.setAttribute("contentTemp", temp);
request.setAttribute("isModify", 1);
}
return super.handleEdit(request, response, objectId);
}
temp = temp.replace("\"", "\\"+"\""); //转换引号
temp = temp.replaceAll("\r\n", "<br> "); //转换换行
OK....终于能正常显示了。
数据库里是这样的: