htmlentities Äquivalent in JSP?
-
11-07-2019 - |
Frage
Ich bin ein PHP-Kerl, aber ich habe einige kleine Projekt in JSP zu tun. Ich frage mich, ob es ein Äquivalent zu htmlentities Funktion ist (von php) in JSP.
Lösung
public static String stringToHTMLString(String string) {
StringBuffer sb = new StringBuffer(string.length());
// true if last char was blank
boolean lastWasBlankChar = false;
int len = string.length();
char c;
for (int i = 0; i < len; i++)
{
c = string.charAt(i);
if (c == ' ') {
// blank gets extra work,
// this solves the problem you get if you replace all
// blanks with , if you do that you loss
// word breaking
if (lastWasBlankChar) {
lastWasBlankChar = false;
sb.append(" ");
}
else {
lastWasBlankChar = true;
sb.append(' ');
}
}
else {
lastWasBlankChar = false;
//
// HTML Special Chars
if (c == '"')
sb.append(""");
else if (c == '&')
sb.append("&");
else if (c == '<')
sb.append("<");
else if (c == '>')
sb.append(">");
else if (c == '\n')
// Handle Newline
sb.append("<br/>");
else {
int ci = 0xffff & c;
if (ci < 160 )
// nothing special only 7 Bit
sb.append(c);
else {
// Not 7 Bit use the unicode system
sb.append("&#");
sb.append(new Integer(ci).toString());
sb.append(';');
}
}
}
}
return sb.toString();
}
Andere Tipps
public static String stringToHTMLString (String string) {...
Das gleiche tut Dienstprogramm von commons-lang Bibliothek:
org.apache.commons.lang.StringEscapeUtils.escapeHtml
exportieren Sie es einfach in kundenspezifische TLD - und Sie erhalten eine praktische Methode für jsp bekommen
.Ich schlage vor, mit escapeXml auf true Attribut von JSTL des gesetzt direkt in JSP
<c:out value="${string}" escapeXml="true" />
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow