블로그 이미지
헤이장

Notice

Recent Post

Recent Comment

Recent Trackback

calendar

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
  • total
  • today
  • yesterday
2009. 7. 27. 20:38 Web/Jsp

EL 태그로 함수를 호출 하려면 어떻게 해야할까? 

1. 사용할 정적(static) 메서드를 만든다


GetPi.java


package elf;

public class GetPi {
    public static double getValue(Double plus) {
        return 3.14 + plus;
    }
}


 elf.tld

<?xml version="1.0" encoding="UTF-8"?>

<taglib version="2.0" 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 web-jsptaglibrary_2_0.xsd">
    <tlib-version>1.0</tlib-version>
    <short-name>elf</short-name>
    <uri>elfunction</uri>
    <function>
        <name>pi</name>
        <function-class>elf.GetPi</function-class>
        <function-signature>
          double getValue(java.lang.Double)
        </function-signature>
    </function>
</taglib>


uri 는 태그 라이브러리에 등록하는 부분
name 은 JSP 에서 사용할 메서드 이름
function-class 는 실제 사용 클래스
function-signature 는 사용할 메서드 형태 (인자값 사용가능) 

3. JSP 에 taglib 지시자를 코딩한다
4. EL 에서 함수를 호출한다

elfTest.jsp


<
%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="elf" uri="elfunction"%>

<html>
    <body>
        <h1>PI + 6.55 = </h1>
        <h1>${elf:pi(6.55)}</h1>
    </body>
</html>

결과는

PI + 6.55 = 9.69

posted by 헤이장