Scenario: 게시판을 만들고나서 이제 글내용을 불러와보니 엔터등 <br> 인식이 안되는 치명적인 현상이 있엇다.

 db에서 불러올때 엔터를 <br>로 인식하게 하는 util은 쉽게 가져올수 있엇는데, 

 <%= utils.getContent(String)%> 이런식으로 써야했다. 나는 db에서 Content를 불러올때 ${dto.content} 이런식으로 불러오는데

 아무리 짱구를 굴려봐도 <%= utils.getContent( ${dto.content} )%> 이런식으로 쓸 수 가 없었다.

그래서 custom el function을 만들기로 했다 (처음엔 1시간이면 만들줄 알았지....)



solution: (아래)


1.먼저 Util파일을 만든다 (이 유틸은 디비에서 글을 불러올때 엔터를 <br>로 대입해주는 유틸이다)

주의점: 만약 bean을 사용하면 메소드를 static으로 선언해줄 필요가 없지만, 우리는 custom el funtion을 만들어야 하기때문에 무조건 static으로 만들어야한다

ContentForm.java

Java 0.56 KB
  1. public class ContentForm {
  2.     public static String getContent(String comment) {
  3.         return br(" ""<br>", comment);
  4.     }
  5.  
  6.     public static String br(String first, String brTag, String comment) {
  7.         StringBuffer buffer = new StringBuffer();
  8.         StringTokenizer st = new StringTokenizer(comment, "\n");
  9.         int count = st.countTokens();
  10.         buffer.append(first);
  11.         int i = 1;
  12.         while (st.hasMoreTokens()) {
  13.             if (== count) {
  14.                 buffer.append(st.nextToken());
  15.             } else {
  16.                 buffer.append(st.nextToken() + brTag);
  17.             }
  18.             i++;
  19.         }
  20.         return buffer.toString();
  21.     }
  22. }


2.tld 파일을 만들어준다

el-funtion.tld

XML 0.82 KB
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2.  
  3. <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  5.    version="2.0">
  6.  
  7.     <description>CommonUtil functions library</description>
  8.     <display-name>CommonUtil functions</display-name>
  9.     <tlib-version>1.1</tlib-version>
  10.     <short-name>mobizaUtil</short-name>
  11.     <uri>tld/el-funtion.tld</uri>
  12.  
  13.     <function>
  14.         <description>글내용 엔터 등, 줄바꿈 인식하게하기</description>
  15.         <name>getContent</name>
  16.         <function-class>경로.ContentForm</function-class>
  17.         <function-signature>java.lang.String getContent(java.lang.String)</function-signature>
  18.     </function>
  19. </taglib>


3. web.xml에 uri와 tld 파일 경로를 맵핑시켜준다 (생략가능)

web.xml

XML 0.25 KB
  1.     <!-- Custom UL TAG을 쓰기위함이다 -->
  2.     <jsp-config>
  3.         <taglib>
  4.             <taglib-uri>
  5.               tld/el-funtion.tld
  6.             </taglib-uri>
  7.             <taglib-location>
  8.              {프로젝트상 실제경로}/el-function.tld
  9.             </taglib-location>
  10.         </taglib>
  11.     </jsp-config>


4.jsp 페이지 내에서 사용예

만약 web.xml에서 맵핑을 하지 않는다면

<%@ taglib uri="WEB-INF/tld/el-funtion.tld" prefix="moutil" %>

이런식으로 실제 파일이 위치한 경로를 맵핑시켜주어도 무방하다.

ex.jsp

  1. HTML 0.12 KB
  1. <%@ taglib uri="tld/el-funtion.tld" prefix="moutil" %>
  2.  
  3.  
  4. <body>
  5.     내용:${moutil:getContent(dto.bContent) }
  6. <body>


+ Recent posts