gpt4 book ai didi

html - 从数据库获取 html 标记并显示在 div 中

转载 作者:行者123 更新时间:2023-11-29 14:11:15 28 4
gpt4 key购买 nike

我在 MySql 中有一个表,其中有 html 标记。我试图在 div 中加载该数据,但它没有显示 html,而是按原样显示 html 标记。

而不是显示'苏希尔'

显示<div><h3>Sushil</h3></div> .

我使用 JSP 作为服务器端组件。它使用 JSTL 标记从数据库获取数据。

我不知道如何解决这个问题。

最佳答案

这是我几年前制作的一个旧类,可能有用,像这样使用:

out.println(HTML.decode(contentFromDB));



package com.spectotechnologies.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
* @author Alexandre Lavoie
*/
public class HTML
{
private static final int HTML_ENTITIES_MINUS_CHARACTER = 160;

private static final int HTML_ENTITIES_START_CHARACTER = 32;

private static final String HTML_ENCODING_TABLE[] = {
"&nbsp;", //
"&#33;", // !
"&quot;", // "
"&#35;", // #
"&#36;", // $
"&#37;", // %
"&amp;", // &
"&#39;", // '
"&#40;", // (
"&#41;", // )
"&#42;", // *
"&#43;", // +
"&#44;", // ,
"&#45;", // -
"&#46;", // .
"&frasl;", // /
"&#48;", // 0
"&#49;", // 1
"&#50;", // 2
"&#51;", // 3
"&#52;", // 4
"&#53;", // 5
"&#54;", // 6
"&#55;", // 7
"&#56;", // 8
"&#57;", // 9
"&#58;", // :
"&#59;", // ;
"&lt;", // <
"&#61;", // =
"&gt;", // >
"&#63;", // ?
"&#64;", // @
"&#65;", // A
"&#66;", // B
"&#67;", // C
"&#68;", // D
"&#69;", // E
"&#70;", // F
"&#71;", // G
"&#72;", // H
"&#73;", // I
"&#74;", // J
"&#75;", // K
"&#76;", // L
"&#77;", // M
"&#78;", // N
"&#79;", // O
"&#80;", // P
"&#81;", // Q
"&#82;", // R
"&#83;", // S
"&#84;", // T
"&#85;", // U
"&#86;", // V
"&#87;", // W
"&#88;", // X
"&#89;", // Y
"&#90;", // Z
"&#91;", // [
"&#92;", // /
"&#93;", // ]
"&#94;", // ^
"&#95;", // _
"&#96;", // `
"&#97;", // â
"&#98;", // b
"&#99;", // c
"&#100;", // d
"&#101;", // e
"&#102;", // f
"&#103;", // g
"&#104;", // h
"&#105;", // i
"&#106;", // j
"&#107;", // k
"&#108;", // l
"&#109;", // m
"&#110;", // n
"&#111;", // o
"&#112;", // p
"&#113;", // q
"&#114;", // r
"&#115;", // s
"&#116;", // t
"&#117;", // u
"&#118;", // v
"&#119;", // w
"&#120;", // x
"&#121;", // y
"&#122;", // z
"&#123;", // {
"&#124;", // |
"&#125;", // }
"&#126;", // ~
"&#127;", //
"&#128;", // €
"&#129;", // �
"&#130;", // ‚
"&#131;", // ƒ
"&#132;", // „
"&#133;", // …
"&#134;", // †
"&#135;", // ‡
"&#136;", // ˆ
"&permil;", // ‰
"&#138;", // Š
"&lsaquo;", // ‹
"&#140;", // Œ
"&#141;", // �
"&#142;", // Ž
"&#143;", // �
"&#144;", // �
"&#145;", // ‘
"&#146;", // ’
"&#147;", // “
"&#148;", // ”
"&#149;", // •
"&ndash;", // –
"&mdash;", // —
"&#152;", // ˜
"&trade;", // ™
"&#154;", // š
"&rsaquo;", // ›
"&#156;", // œ
"&#157;", // �
"&#158;", // ž
"&#159;", // Ÿ
"&#160;", //  
"&iexcl;", // ¡
"&cent;", // ¢
"&pound;", // £
"&curren;", // ¤
"&yen;", // ¥
"&brvbar;", // ¦
"&sect;", // §
"&uml;", // ¨
"&copy;", // ©
"&ordf;", // ª
"&laquo;", // «
"&not;", // ¬
"&shy;", // ­
"&reg;", // ®
"&macr;", // ¯
"&deg;", // °
"&plusmn;", // ±
"&sup2;", // ²
"&sup3;", // ³
"&acute;", // ´
"&micro;", // µ
"&para;", // ¶
"&middot;", // ·
"&cedil;", // ¸
"&sup1;", // ¹
"&ordm;", // º
"&raquo;", // »
"&frac14;", // ¼
"&frac12;", // ½
"&frac34;", // ¾
"&iquest;", // ¿
"&Agrave;", // À
"&Aacute;", // Á
"&Acirc;", // Â
"&Atilde;", // Ã
"&Auml;", // Ä
"&Aring;", // Å
"&AElig;", // Æ
"&Ccedil;", // Ç
"&Egrave;", // È
"&Eacute;", // É
"&Ecirc;", // Ê
"&Euml;", // Ë
"&Igrave;", // Ì
"&Iacute;", // Í
"&Icirc;", // Î
"&Iuml;", // Ï
"&ETH;", // Ð
"&Ntilde;", // Ñ
"&Ograve;", // Ò
"&Oacute;", // Ó
"&Ocirc;", // Ô
"&Otilde;", // Õ
"&Ouml;", // Ö
"&times;", // ×
"&Oslash;", // Ø
"&Ugrave;", // Ù
"&Uacute;", // Ú
"&Ucirc;", // Û
"&Uuml;", // Ü
"&Yacute;", // Ý
"&THORN;", // Þ
"&szlig;", // ß
"&agrave;", // à
"&aacute;", // á
"&acirc;", // â
"&atilde;", // ã
"&auml;", // ä
"&aring;", // å
"&aelig;", // æ
"&ccedil;", // ç
"&egrave;", // è
"&eacute;", // é
"&ecirc;", // ê
"&euml;", // ë
"&igrave;", // ì
"&iacute;", // í
"&icirc;", // î
"&iuml;", // ï
"&eth;", // ð
"&ntilde;", // ñ
"&ograve;", // ò
"&oacute;", // ó
"&ocirc;", // ô
"&otilde;", // õ
"&ouml;", // ö
"&divide;", // ÷
"&oslash;", // ø
"&ugrave;", // ù
"&uacute;", // ú
"&ucirc;", // û
"&uuml;", // ü
"&yacute;", // ý
"&thorn;", // þ
"&yuml;" // ÿ
};

public static String encode(String p_sToEncode)
{
int nCountChars;
String sResult = "";
String sFound;

// Check for characters to replace till end of string
for(nCountChars = 0;nCountChars < p_sToEncode.length();nCountChars++)
{
// Init to current character
sFound = "" + p_sToEncode.charAt(nCountChars);

// Check if character is in the list
if(p_sToEncode.charAt(nCountChars) >= HTML_ENTITIES_MINUS_CHARACTER)
{
sFound = HTML_ENCODING_TABLE[p_sToEncode.charAt(nCountChars) - HTML_ENTITIES_START_CHARACTER];
}

// Add to result
sResult += sFound;
}

return sResult;
}

public static String decode(String p_sToDecode)
{
Pattern oPattern;
Matcher oMatcher;
String sCode;
int nCode;

oPattern = Pattern.compile("&\\#?[0-9,a-z,A-Z]*;");

oMatcher = oPattern.matcher(p_sToDecode);

StringBuffer oBuffer = new StringBuffer(p_sToDecode.length());

while(oMatcher.find())
{
sCode = oMatcher.group(0);

for(nCode = 0;nCode < HTML_ENCODING_TABLE.length;nCode++)
{
if(sCode.equals(HTML_ENCODING_TABLE[nCode]))
{
sCode = "" + (char)(HTML_ENTITIES_START_CHARACTER + nCode);
break;
}
}

oMatcher.appendReplacement(oBuffer,Matcher.quoteReplacement(sCode));
}

oMatcher.appendTail(oBuffer);

return oBuffer.toString();
}
}

希望这有帮助!

关于html - 从数据库获取 html 标记并显示在 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13477687/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com