现在的位置: 首页 > 综合 > 正文

unterminated string literal 解决办法(转义字符问题)

2014年06月26日 ⁄ 综合 ⁄ 共 2205字 ⁄ 字号 评论关闭

存入数据库中的字段是

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

String java.lang.String.replace(CharSequence
target,CharSequence replacement)

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".

Parameters:
target The sequence of char values to be replaced
replacement The replacement sequence of char values
Returns:
The resulting string
Throws:

我一开始直接

content.replaceAll("&gt;", ">").replaceAll("&lt;", "<");

还以为直接这样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("&gt;", ">").replaceAll("&lt;", "<");
			temp = temp.replace("\"", "\\"+"\"");
			temp = temp.replaceAll("\r\n", "<br>&nbsp;&nbsp;");
			request.setAttribute("contentTemp", temp);
			request.setAttribute("isModify", 1);
		}
		return super.handleEdit(request, response, objectId);
	}

temp = temp.replace("\"", "\\"+"\"");  //转换引号
temp = temp.replaceAll("\r\n", "<br>&nbsp;&nbsp;");   //转换换行

OK....终于能正常显示了。

数据库里是这样的:

抱歉!评论已关闭.