Hi~ Summers!!

taglib 만들기 본문

개발/JSP&HTML

taglib 만들기

eNaNII 2013. 10. 2. 15:13

1. WEB-INF/tld/밑에 파일을 만든다.

예) f.tld

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="UTF-8" ?>
 
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
 
  <description>JSTL 1.1 functions library</description>
  <display-name>JSTL XML</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>s</short-name>
 
  <function>
   <description>String Replace</description>
   <name>htmlCharToSpecialChar</name>
   <function-class>my.test.util.StringUtil</function-class>
   <function-signature>java.lang.String htmlCharToSpecialChar(java.lang.String , boolean)</function-signature>
   <example>
    ${s:htmlCharToSpecialChar(str, true)}
   </example>
  </function>
 
  <function>
   <description>String Replace</description>
   <name>specialCharToHtmlChar</name>
   <function-class>my.test.util.StringUtil</function-class>
   <function-signature>java.lang.String specialCharToHtmlChar(java.lang.String , boolean)</function-signature>
   <example>
    ${s:specialCharToHtmlChar(str, true)}
   </example>
  </function>
 
  <function>
   <description>String Replace</description>
   <name>htmlCharToEmpty</name>
   <function-class>my.test.util.StringUtil</function-class>
   <function-signature>java.lang.String htmlCharToEmpty(java.lang.String)</function-signature>
   <example>
    ${s:htmlCharToEmpty(str)}
   </example>
  </function>
 
</taglib>

 

 

2. my.test.util 패키지 안에 StringUtil 클래스를 만든다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package my.test.util;
 
 
public class StringUtil {
 
/**
 *<pre>
 * 인자로 받은 String을 특수문자로 변환한다.
 * @param 문자열, boolean값 (textarea 인지 그냥 text 인지) 
 * @return 변환된 str
 *</pre>
 */
 public static String htmlCharToSpecialChar(String str, boolean txtArea){
 
  str = str.replaceAll("<""&lt;");
  str = str.replaceAll(">""&gt;");
  str = str.replaceAll(" ""&nbsp;");
 
  if(txtArea == true)
   str = str.replaceAll("\r\n""<br/>");
  else
   str = str.replaceAll("\n""<br/>");
 
  str = str.replaceAll("'""&#39;");
  str = str.replaceAll("\"""&quot;");
 
  return str;
 
 }
 
 
/**
 *<pre>
 * 인자로 받은 String의 특수문자를 일반문자로 변환한다.
 * @param 문자열, boolean값 (textarea 인지 그냥 text 인지) 
 * @return 변환된 str
 *</pre>
 */
 public static String specialCharToHtmlChar(String str, boolean txtArea){
 
  str = str.replaceAll("&lt;""<");
  str = str.replaceAll("&gt;"">");
  str = str.replaceAll("&nbsp;"" ");
 
  if(txtArea == true){
   str = str.replaceAll("<br/>""\r\n");
   str = str.replaceAll("<br>""\r\n");
  }else{
   str = str.replaceAll("<br/>""\n");
   str = str.replaceAll("<br>""\n");
  }
 
  str = str.replaceAll("&#39;""'");
  str = str.replaceAll("&quot;""\"");
 
  return str;
 }
 
 
/**
 *<pre>
 * 인자로 받은 String의 html문자를 공백으로 치환한다.
 * @param 문자열 
 * @return 변환된 str
 *</pre>
 */
 public static String htmlCharToEmpty(String str){
 
  str = str.replaceAll("<""");
  str = str.replaceAll(">""");
  str = str.replaceAll(" """);
  str = str.replaceAll("'""");
  str = str.replaceAll("\"""");
 
  return str;
 
 }
 
}

 

 

3. jsp에서 사용한다.

1
2
3
<%@ taglib prefix="f" uri="/WEB-INF/tld/f.tld" %>
 
${f:specialCharToHtmlChar(content, true)}



[출처] 나만의 taglib 만들기|작성자 joypheonix

Comments