이스케이프 문자열(Escape Sequence)을 처리하기 위한 다양한 방법들.
EL ¶
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> ${fn:replace(article.contents,'<','<')}
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> ${fn:escapeXml(article.contents)}
JAVA ¶
tmp = article.getContents(); tmp = tmp.replaceAll("&","&"); //tmp = tmp.replaceAll("\"","""); tmp = tmp.replaceAll("'","'"); tmp = tmp.replaceAll("\"","""); tmp = tmp.replaceAll("<","<"); tmp = tmp.replaceAll(">",">"); tmp = tmp.replaceAll("\r\n","<br>"); tmp = tmp.replaceAll("\n","<br>"); article.setContents(tmp);
Struts Commons Lang ¶
import org.apache.commons.lang.StringEscapeUtils tmp = article.getContents(); tmp = StringEscapeUtils.escapeHtml(tmp); tmp = tmp.replaceAll("\n","<br>"); tmp = tmp.replaceAll("\n","<br>");
escapeXml() 이나, unescapeHtml() 등 다양한 함수가 존재한다. 그러나 StringUtils를 사용시 한글 문제가 있다.(모든 종류의 한글이 유니코드 문자 번호로 변형된다.)
Struts Taglibs : Beans ¶
<%@ taglib uri="/tags/struts-bean" prefix="bean" %> <bean:write name="article" property="contents" filter="true">
JSTL ¶
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:out value="${article.contents}" escapeXml="true">