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

存入数据库中的字段是

<select  name="${item.name}" id="${item.id}" style="${item.styles}" class="commonInput longtext ${item.classes}" >
< option  value="${item.value}" >
</option  >
</select >

显示在页面需要显示为如下:


因此在后台用replaceAll方法转换了"<" ,">"。

然后页面接收了一下:

然后在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(">", ">").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", "
  "); request.setAttribute("contentTemp", temp); request.setAttribute("isModify", 1); } return super.handleEdit(request, response, objectId); }

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



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


unterminated string literal 解决办法(转义字符问题)_第1张图片unterminated string literal 解决办法(转义字符问题)_第2张图片

数据库里是这样的:

unterminated string literal 解决办法(转义字符问题)_第3张图片

你可能感兴趣的:(bug)