본문 바로가기

JAVA/JSP & Servlet

expression language 태그라이브러리(TLD) [직접 작성한 메서드 호출]

 

 

 

 

 

package util;

 

public class MyMath {

        public static int sum(int start, int end){

               int sum = 0;

               for(int cnt = start ; cnt <= end ; cnt++)

                       sum += cnt;

               return sum;

        }

 

}

 

 

 

 

 


 

 

 

 

 

 

그리고 math-functions.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">

    <tlib-version>1.0</tlib-version>

    <short-name>math</short-name>

    <function>

        <name>squareroot</name>

        <function-class>java.lang.Math</function-class>

        <function-signature>doudle sqrt(double)</function-signature>

    </function>

   

    <function>

        <name>total</name>

        <function-class>util.MyMath</function-class>

        <function-signature>int sum(int, int)</function-signature>

    </function>

</taglib> 

 

 

 

 

 

호출


호출 jsp페이지

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@taglib prefix="m" uri="http://www.test.com/test/math-functions.tld" %>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>익스프레션 : 태그라이브러리(TLD) : 내가 만든 메서드 사용하기</title>

</head>

<body>

 

1부터 100까지의 합은?<br><br>

: ${m:total(1,100) }<br>

<br>

 

        <!--

        http://localhost/ElEx01/TldEx02_MyMethod.jsp?num1=1&num2=100

        --------------------------

        1부터 100까지의 합은?

       

        : 5050

        --------------------------

         -->

         

${param.num1 }부터 ${param.num2 }까지의 합은?<br><br>

: ${m:total(param.num1, param.num2) }<br>

 

</body>

</html>