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

解决方案 »

  1.   

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

  2.   

    要不就把'\n' replace成 "<br>"
      

  3.   

    你可以在网上查一下
    有把文本转换成HTML格式的
    就是把换行转成<br>
    把>转成&gt
    把<转成&lt的函数
      

  4.   

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

  5.   

    我找了一个/**
     * 字符串编码器类,将字符串转换为指定格式.<br>
     * <br>
     * 参数字典:<br>
     * src - source     来源的简写<br>
     * dst - destnation 目的的简写<br>
     * fnd - find       查找的简写<br>
     * rep - replace    替换的简写<br>
     * idx - index      索引,下标的简写<br>
     * enc - encoding   编码的简写<br>
     * <br>
     * 例子:<br>
     * <%=ArticleFormat.htmlTextEncoder(yourString)%>
     */
    public class StringEncoder
    {
        /**
         * 将字符串src中的子字符串fnd全部替换为新子字符串rep.<br>
         * 功能相当于java sdk 1.4的String.replaceAll方法.<br>
         * 不同之处在于查找时不是使用正则表达式而是普通字符串.
         */
        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编码.<br>
         */
        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文字编码.<br>
         */
        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", "<br>");
            dst = replaceAll(dst, "\r",   "<br>");
            dst = replaceAll(dst, "\n",   "<br>");
            
            return dst;
        }    /**
         * 转换为URL编码.<br>
         */
        public static String urlEncoder(String src, String enc) throws Exception
        {
            return java.net.URLEncoder.encode(src, enc) ;
        }    /**
         * 转换为XML编码.<br>
         */
        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编码.<br>
         */
        public static String sqlEncoder(String src) throws Exception
        {
            if (src == null || src.equals(""))
            {
                return "";
            }
            
            return replaceAll(src, "'", "''");
        }    /**
         * 转换为javascript编码.<br>
         */
        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;
        }
    }
      

  6.   

    刚才那位老兄twinking() ( ):
    所说的:有把文本转换成HTML格式的
    就是把换行转成<br>
    把>转成&gt
    把<转成&lt的函数我也试过了,可以换行的问题解决了,但是同样出现一个问题就是,我想修改数据库里的内容是却出现了html代码,就是说当我把数据库里的内容取到textarea表单后就有问题了
      

  7.   

    /**
         * 转换为SQL编码.<br>
         */
        public static String sqlEncoder(String src) throws Exception
        {
            if (src == null || src.equals(""))
            {
                return "";
            }
            
            return replaceAll(src, "'", "''");
        }这个我不太懂是有什么作用?
      

  8.   

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