站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题急救啊!!!用jsp从mysql中调出内容无法自动换行

急救啊!!!用jsp从mysql中调出内容无法自动换行

摘要: 急救啊!!!用jsp从mysql中调出内容无法自动换行


我做一个简单的jsp网站,但是我在后台textarea表单输入后,能够存到数据库里了,但是在取出数据库里的内容显示在页面上是却是整段这样一行输出,不能自动分行,也不能分段;
在线等


希望那位好心人,能给出完正的代码!
先谢谢了


最简单就是在这些内容前面加
,后面加
呵呵


要不就把'/n' replace成 "
"


多容易啊,呵呵,LZ连HTML都没掌握啊……


你可以在网上查一下
有把文本转换成HTML格式的
就是把换行转成

把>转成&gt
把<转成&lt的函数



加了
后却不超过了表格的宽度,就是说这样一整段都是在一行里面


我找了一个

/**
* 字符串编码器类,将字符串转换为指定格式.

*

* 参数字典:

* src - source 来源的简写

* dst - destnation 目的的简写

* fnd - find 查找的简写

* rep - replace 替换的简写

* idx - index 索引,下标的简写

* enc - encoding 编码的简写

*

* 例子:

* <%=ArticleFormat.htmlTextEncoder(yourString)%>
*/
public class StringEncoder
{
/**
* 将字符串src中的子字符串fnd全部替换为新子字符串rep.

* 功能相当于java sdk 1.4的String.replaceAll方法.

* 不同之处在于查找时不是使用正则表达式而是普通字符串.
*/
public static String replaceAll(String src, String fnd, String rep) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;

int idx = dst.indexOf(fnd);

while (idx >= 0)
{
dst = dst.substring(0, idx) + rep + dst.substring(idx + fnd.length(), dst.length());
idx = dst.indexOf(fnd, idx + rep.length());
}

return dst;
}

/**
* 转换为HTML编码.

*/
public static String htmlEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;
dst = replaceAll(dst, "<", "&lt;");
dst = replaceAll(dst, ">", "&rt;");
dst = replaceAll(dst, "/"", "&quot;");
dst = replaceAll(dst, "'", "&#039;");

return dst;
}

/**
* 转换为HTML文字编码.

*/
public static String htmlTextEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;
dst = replaceAll(dst, "<", "&lt;");
dst = replaceAll(dst, ">", "&rt;");
dst = replaceAll(dst, "/"", "&quot;");
dst = replaceAll(dst, "'", "&#039;");
dst = replaceAll(dst, " ", "&nbsp;");
dst = replaceAll(dst, "/r/n", "
");
dst = replaceAll(dst, "/r", "
");
dst = replaceAll(dst, "/n", "
");

return dst;
}

/**
* 转换为URL编码.

*/
public static String urlEncoder(String src, String enc) throws Exception
{
return java.net.URLEncoder.encode(src, enc) ;
}

/**
* 转换为XML编码.

*/
public static String xmlEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;
dst = replaceAll(dst, "&", "&amp;");
dst = replaceAll(dst, "<", "&lt;");
dst = replaceAll(dst, ">", "&gt;");
dst = replaceAll(dst, "/"", "&quot;");
dst = replaceAll(dst, "/'", "&acute;");

return dst;
}

/**
* 转换为SQL编码.

*/
public static String sqlEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

return replaceAll(src, "'", "''");
}

/**
* 转换为javascript编码.

*/
public static String jsEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;
dst = replaceAll(dst, "'", "//'");
dst = replaceAll(dst, "/"", "///"");
//dst = replaceAll(dst, "/r/n", "///n"); // 和/n转换有冲突
dst = replaceAll(dst, "/n", "///n");
dst = replaceAll(dst, "/r", "///n");

return dst;
}
}



刚才那位老兄twinking() ( ):
所说的:有把文本转换成HTML格式的
就是把换行转成

把>转成&gt
把<转成&lt的函数

我也试过了,可以换行的问题解决了,但是同样出现一个问题就是,我想修改数据库里的内容是却出现了html代码,就是说当我把数据库里的内容取到textarea表单后就有问题了



/**
* 转换为SQL编码.

*/
public static String sqlEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

return replaceAll(src, "'", "''");
}

这个我不太懂是有什么作用?



就是说我现在的问题是:用了那个老兄说的方法:"把换行转成

把>转成&gt
把<转成&lt的函数"
能正确显示页面.
但是当我在第二次从后台取出数据后("修改数据"),表单里却是有html 代码,随便增加(减少空格)后,提交表单后在前台页面显示
&amp;这样的代码,
我想能不能刚有一种方法可以再次反过来



↑返回目录
前一篇: webwork/hibernate可能还有spring的环境下mysql的中文问题到底怎么做才好????
后一篇: JavaBean的问题.