JSP/함수

JSTL 에서 excape 문자 치환하기

nineDeveloper 2015. 2. 3. 09:59
728x90
반응형

JSTL, EL 에서 문자열을 치환하여로 할때 JSTL 에서 기본적으로 제공하는 fn 함수를 사용하면 편리하다.


fn 에서 제공되는 메소드 리스트는 아래와 같다.


http://download.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/fn/tld-summary.html

 


Function Summary
boolean contains( java.lang.String, java.lang.String) Tests if an input string contains the specified substring.
boolean containsIgnoreCase( java.lang.String, java.lang.String) Tests if an input string contains the specified substring in a case insensitive way.
boolean endsWith( java.lang.String, java.lang.String) Tests if an input string ends with the specified suffix.
java.lang.String escapeXml( java.lang.String) Escapes characters that could be interpreted as XML markup.
int indexOf( java.lang.String, java.lang.String) Returns the index withing a string of the first occurrence of a specified substring.
java.lang.String join( java.lang.String[], java.lang.String) Joins all elements of an array into a string.
int length( java.lang.Object) Returns the number of items in a collection, or the number of characters in a string.
java.lang.String replace( java.lang.String, java.lang.String, java.lang.String) Returns a string resulting from replacing in an input string all occurrences of a "before" string into an "after" substring.
java.lang.String[] split( java.lang.String, java.lang.String) Splits a string into an array of substrings.
boolean startsWith( java.lang.String, java.lang.String) Tests if an input string starts with the specified prefix.
java.lang.String substring( java.lang.String, int, int) Returns a subset of a string.
java.lang.String substringAfter( java.lang.String, java.lang.String) Returns a subset of a string following a specific substring.
java.lang.String substringBefore( java.lang.String, java.lang.String) Returns a subset of a string before a specific substring.
java.lang.String toLowerCase( java.lang.String) Converts all of the characters of a string to lower case.
java.lang.String toUpperCase( java.lang.String) Converts all of the characters of a string to upper case.
java.lang.String trim( java.lang.String) Removes white spaces from both ends of a string.


위 메소드 중에서 fn:replace 를 사용할때에 \n 에 대한 치환이 정상적으로 이뤄지지 않는다. 

그럴 경우에는 아래와 같이 처리하면 된다.


<% pageContext.setAttribute("lineChar", "\n"); %>

...


${fn:replace( 대상 문자열, newLineChar, "<br/>")}


위처럼 처리하면 정상적으로 escape 에 대한 문자열 치환이 가능하다.



 

728x90
반응형