2013. 8. 16. 10:00

● 표현 언어(EL)에서 자바 클래스의 메소드에 접근하는 방법

 ① 자바의 클래스 파일을 작성한다.

 ② 클래스  파일을 작성하고 나면 태그라이브러리에 대한 설정정보를 담고 있는  TLD 파일을 작성한다.

 ③ web.xml 파일에  TLD 파일을 사용할 수 있는 설정 정보를 추가한다.

 ④ 자바클래스의 메소드에 접근하는  JSP 페이지를 작성한다.


● 표현 언어(EL)에서 자바 클래스의 메소드에 접근하는 방법

  자바 클래스 파일 ↔  TLD파일 ↔ web.xml ↔  JSP 페이지


● 사용 예

1) 클래스 작성

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.jsp.test;
  
public class Compute  {
    public static int add(String x, String y)  {
       int a = 0;
       int b = 0;
       try {
         a = Integer.parseInt(x);
         b = Integer.parseInt(y);
       }catch(Exception  e)  {}
       return a + b;
    }
}
2) "elFunctions.tld" 파일을 작성하여 "웹 루트\WEB-INF\tlds" 폴더에 저장한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--?xml version="1.0" encoding="utf-8" ?-->
                        web-jsptaglibrary_2_0.xsd" version="2.0">
       
     <description>EL에서 함수실행</description>
     <tlib-version>1.0</tlib-version>
     <short-name>ELfunctions</short-name>
     <uri>/ELFunctions</uri>
     <function>
         <description> x와 y의 합</description>
         <name>add</name>
         <function-class>com.jsp.test.Compute</function-class>
         <function-signature>
              int  add(java.lang.String,  java.lang.String)
         </function-signature>
    </function>
</taglib>
3) "웹 루트\WEB-INF\web.xml" 파일 수정
1
2
3
4
5
6
7
8
9
10
11
12
13
<!--?xml version="1.0" encoding="utf-8"?-->
              http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
    :
   <taglib>
         <taglib-uri>
             /WEB-INF/tlds/elFunctions.tld
         </taglib-uri>
         <taglib-location>
             /WEB-INF/tlds/elFunctions.tld
         </taglib-location>
     </taglib>
</web-app>
4) JSP 페이지 작성 - elFunctionTest.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@page contentType="text/html;charset=utf-8"%>
<%@taglib prefix="test" uri="/WEB-INF/tlds/elFunctions.tld" %>
<%request.setCharacterEncoding("utf-8");%>
<h3>표현 언어에서 함수사용하기  -두 숫자의 덧셈</h3>
<p>
</p><form action="elFunctionTest.jsp" method="post">
    X : <input type="text" name="x" value="${param['x']}">
    <br>/
    Y : <input type="text" name="y" value="${param['y']}">
    <input type="submit" value="덧셈">
</form>
<p>
합은 : ${test:add(param["x"],param["y"])} 입니다.
</p>




'JSP > JspServlet' 카테고리의 다른 글

XML 기본구조  (0) 2013.08.16
Tomcat web.xml  (0) 2013.08.16
서블릿에서 out.print()  (0) 2013.08.16
tag : core  (0) 2013.08.16
서버 도메인 확인  (0) 2013.08.16
Posted by 1+1은?