이전 포스팅에 있던 2008/12/31 - [java] - Custom Tags in JSP Pages가 tomcat6에서 잘되던게 jeus5에서 실행되지 않는다. 질문을 올려도 답도 없고(짜증..) 그래서 이방식으로 바꾸었다. servlet spec 2.4인데 web.xml에 따로 설정해준것은 없다. java method는 static이어야 한다.
java class
package kr.co.test.common;
import java.util.Calendar;
import java.util.Date;
public class TagFunctions {
public static String stringShorten(String inputString, int limitLength) {
if (inputString.length() > limitLength) {
return inputString.substring(0, limitLength) + "..";
}
else {
return inputString;
}
}
public static boolean isDateNew(String compareDate, int comparePeriod) {
Date today = new Date();
Calendar nowCalendar = Calendar.getInstance();
nowCalendar.setTime(today);// 오늘로 설정.
Calendar compareCalendar = Calendar.getInstance();
compareCalendar.set(Integer.parseInt(compareDate.substring(0,4)), Integer.parseInt(compareDate.substring(4,6))-1, Integer.parseInt(compareDate.substring(6,8))); // 기준일로 설정. month의 경우 해당월수-1을 해줍니다.
//System.out.println("현재일자 : " + nowCalendar.get(Calendar.YEAR) + "년 " + (nowCalendar.get(Calendar.MONTH) + 1) + "월 " + nowCalendar.get(Calendar.DATE) + "일<br/>");
//System.out.println("비교일자 : " + compareCalendar.get(Calendar.YEAR) + "년 " + (compareCalendar.get(Calendar.MONTH) + 1) + "월 " + compareCalendar.get(Calendar.DATE) + "일<br/>");
int count = 0;
while (!compareCalendar.after(nowCalendar)) {
//while(compareCalendar(nowCalendar)){
count++;
compareCalendar.add(Calendar.DATE, 1); // 다음날로 바뀜
//System.out.println ( compareCalendar.get ( Calendar.YEAR ) + "년 " + ( compareCalendar.get ( Calendar.MONTH ) + 1 ) + "월 " + compareCalendar.get ( Calendar.DATE ) + "일" );
}
//System.out.println("기준일로부터 " + (count) + "일이 지났습니다.");
boolean isNew = false;
if(comparePeriod >= count){
isNew = true;
}
return isNew;
}
/**
* 문자인코딩..
* new String(inputString.getBytes("8859_1"), "EUC-KR");
* new String(inputString.getBytes("8859_1"), "UTF-8");
* new String(inputString.getBytes("EUC-KR"), "8859_1");
* new String(inputString.getBytes("EUC-KR"), "UTF-8");
* new String(inputString.getBytes("UTF-8"), "8859_1");
* new String(inputString.getBytes("UTF-8"), "EUC-KR");
* @param inputString
* @param inCharset
* @param outCharset
* @return
*/
public static String stringEncode(String inputString, String inCharset, String outCharset) {
if (inputString == null) {
return "";
}
try {
inputString = new String(inputString.getBytes(inCharset), outCharset);
}
catch (Exception e) {
}
return inputString;
}
}
/tld/custom-tag.tld
<?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>Custom Tag Library</description>
<display-name>custom tag library</display-name>
<tlib-version>1.0</tlib-version>
<short-name>custom tag</short-name>
<uri>http://test.co.kr/tag/functions</uri>
<function>
<description>글자 축약</description>
<name>shorten</name>
<function-class>kr.co.test.common.TagFunctions</function-class>
<function-signature>
java.lang.String stringShorten(java.lang.String, int)
</function-signature>
</function>
<function>
<description>날짜 비교</description>
<name>isnew</name>
<function-class>kr.co.test.common.TagFunctions</function-class>
<function-signature>
boolean isDateNew(java.lang.String, int)
</function-signature>
</function>
<function>
<description>문자인코딩</description>
<name>encode</name>
<function-class>kr.co.test.common.TagFunctions</function-class>
<function-signature>
java.lang.String stringEncode(java.lang.String, java.lang.String, java.lang.String)
</function-signature>
</function>
</taglib>
jsp
<%@ taglib prefix="tf" uri="http://test.co.kr/tag/functions" %>
${tf:shorten(tf:encode(‘내용입니다.’, '8859_1', 'EUC-KR'), 5)}
<c:if test="${tf:isnew(‘20090101’, 3) == true}"><img src="new.gif"/></c:if>
'java' 카테고리의 다른 글
자바 가변인자 (variable argument type) (0) | 2010.05.10 |
---|---|
구글날씨 api 이용하기 (5) | 2009.01.23 |
Custom Tags in JSP Pages (0) | 2008.12.31 |
Java -classpath 옵션 (1) | 2008.04.29 |
ClassLoader를 이용한 서버 종류 알아내기 (1) | 2008.03.19 |