본문 바로가기

JAVA/JSP & Servlet

expression language 태그라이브러리(TLD) [자바의 정적 메서드 호출] -설정-

설정

 


WEB-INF안에 tlds폴더 생성,

C:\javaproject\apache-tomcat-7.0.37\webapps\examples\WEB-INF\jsp2

 

디렉토리 안의 jsp2-example-taglib.tld파일을 복사하여 tlds폴더안에 넣은 후

 

math-functions.tld로 이름 변경

 

 

 

 

 

 

 

그리고 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> double sqrt(double) </function-signature >
    </function >
</ taglib>

 

 

 

그 다음 web.xml파일을 열고 다음과 같은 <taglib>엘리먼트 추가하여 tld파일 등록

 

<?xml version="1.0" encoding="ISO-8859-1"?>

 

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0">

  <display-name>Welcome to Tomcat</display-name>

  <description>Welcome to Tomcat</description>

       

        <context-param>

               <param-name>tablename</param-name>

               <param-value>board1</param-value>

        </context-param>

       

        <jsp-config>

        <taglib>

               <taglib-uri>http://www.test.com/test/math-functions.tld</taglib-uri>

               <taglib-location>/WEB-INF/tlds/math-functions.tld</taglib-location>

        </taglib>

        </jsp-config>

 

 

</web-app>

 

 

 

 

 

호출 


 

<%@ 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>

 

<h3>제곱근 구하기</h3>

25 제곱근은? ${m:squareroot(25) }<br>

 

</body>

</html>