日期:2014-05-18  浏览次数:20663 次

简单问题的迷惑,请教!
我查询数据库中得到如下的记录:校领导慰问我校两位院士   [图],但是我把它放到text文本框中却变成了:校领导慰问我校两位院士,少了空格以后的   [图],
其中用到的程序片段为:

<input   name= "_title "   type= "text "   size= "60 "   maxlength= "100 "   value= <%=news.getTitle()%> >

看试简单,但实在不知道为什么会变成这样,在线等大虾指教。谢谢!

------解决方案--------------------
如果把记录输出到文本域的话就不会出现这个问题了。
如果要想输出到文本狂框,需要对原先存入数据库的那段文本进行解析,比如空格就变成&nbsp 保存
------解决方案--------------------
需要对字符串里的特殊字符进行替换...
------解决方案--------------------
将特殊字符转换成HTML格式

//Class toHTML
//CopyRight:Writed By Yun Leo(php4er@sohu.com)
//Last modify time:2003-8-31
//Method: public String toHTML(String old_string)
class toHTML
{
public String toHTML(String sStr)
{
if (sStr == null||sStr.equals( " "))
{
return sStr;
}

String sTmp = new String();
int i = 0;
while (i <= sStr.length()-1)
{
if (sStr.charAt(i) == '\n ')
{
sTmp = sTmp.concat( "
");
}
else if(sStr.charAt(i) == ' ')
{
sTmp = sTmp.concat( "%26amp;nbsp; ");
}
else if(sStr.charAt(i) == ' < ')
{
sTmp = sTmp.concat( " < ");
}
else if(sStr.charAt(i) == '> ')
{
sTmp = sTmp.concat( "> ");
}
else if(sStr.charAt(i) == 34) //34为双引号
{
sTmp = sTmp.concat( " " ");
}
else if(sStr.charAt(i) == 39) //若为单引号则用两个单引号代替
{
sTmp = sTmp.concat( " ' ' ");
}
else
{
sTmp = sTmp.concat(sStr.substring(i,i+1));
}
i++;
}
return sTmp;
}
}

------解决方案--------------------
//转换成Html格式
public String GetHtmlEncode(String StrValue) {
String str=StrValue;
str = str.replace( "& ", "&amp; ");
str = str.replace( " ' ", " ' ' ");
str = str.replace( "\ " ", "&quot; ");
str = str.replace( " ", "&nbsp; ");
str = str.replace( " < ", "&lt; ");
str = str.replace( "> ", "&gt; ");
str = str.replace( "\n ", " <br/> ");
return str;
}
//转换成textarea格式
public String ReadHtmlEncode(String StringValue) {
String str=StringValue;
str = str.replace( " <br/> ", "\n ");
str = str.replace( "&gt; ", "> ");
str = str.replace( "&lt; ", " < ");
str = str.replace( "&nbsp; ", " ");
str = str.replace( "&quot; ", "\ " ");
str = str.replace( " ' ' ", " ' ");
str = str.replace( "&amp; ", "& ");
return str;
}