본문 바로가기

Web Programming

표현 언어 (Expression Language) JSTL (JSP Standard Tag Library)

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
JSTL이라는 놈에 대해서 들어 본적이 있습니다. 그때는 JSP 도 없었던 시절이었기때문에 -_-.. JSTL을 알면 모합니까.. xml 처럼 외계어 같은데 ㅋㅋㅋ 그렇스빈다.. ^^ 그럼 이 표현언어에 대해서 한번 알아볼까요... 보통 JSP 를 사용해서 뭔가를 가져올려면..

<H2>
   <jsp:useBean id="test" class="TestBean" />
   <%= test.getName() %>
</H2>


이런 식으로 정의를 해줘야 합니다. 줄줄이 따져 볼까요? 일단 TestBean 이라는 클래스로 test 라는 빈(Bean) 객체를 생성한다고 해줬습니다. 이름은 test 이구요.. 그담줄에선 test에 있는 getName 이라는 메소드를 호출하는 군요..

근데 표현언어를 사용하면.. -_-;; 뭐냐..
<H2>
   ${test.name}
</H2>
헐.. 끗 -_-;; 이렇게 표현언어를 사용하면 application, session, request, page 범위에 있는 빈(Bean) 객체에 보다 쉽게 접근할 수 있어서 좋다고 하더군요.. 점마는 자바하고 똑같은 연산자를 사용 할 수 있다군요~~

그래서 예제를 한번 더 만들어 봤습니다 ^^ 한번 보시지요~~

public class Product {
   // 상품 목록!
   private String[] productList = { "Canon EOS 400D", "Canon 1Ds Mark III",
           "Nikon D300", "Nikon D3" };

   private int num1 = 10;

   private int num2 = 20;

   public int getNum1() {
       return num1;
   }

   public int getNum2() {
       return num2;
   }

   public String[] getProductList() {
       return productList;
   }

}
이건 그냥 모델 부분 입니다.. -_-// 호호..

그다음 jsp 파일 두개를 보시죠~ 먼저 ProductList.jsp 파일입니다.

<%@ page language="java" contentType="text/html; charset=EUC-KR"
   pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
       <title>JSTL 예제</title>
   </head>
   <body>
       <center>
           <h2> 상품 목록 이지롱</h2>
           <hr>
           <form name =form1 method=POST action="ProductSel.jsp">
               <jsp:useBean id="product" class="jstlTest.Product" scope="session"></jsp:useBean>
               <select name="sel">
                   <%
                       for(int i=0; i < product.getProductList().length ; i++)
                           out.println("<option>" + product.getProductList()[i] + "</option>");
                   %>
               </select>
               <input type="submit" value="선택"/>
           </form>
       </center>
   </body>
</html>



form 태그에서 값을 넘겨 받을 놈인 ProductSel.jsp 입니다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
   pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
       <title>골른거 보기</title>
   </head>
   <body>
       <center>
           <h2> 님이 고른거에여 </h2>
           <hr>
           1. 선택한 상품은 : ${param.sel } <br>
           2. num1 + num2 = ${product.num1+product.num2} <br>
       </center>
   </body>
</html>

우하하.. 뭔가 미묘하게 틀립니다 -_-;; 흐흐.. 한번 잘 살펴보도록 하죠.. 첫번째에 있는 jsp는 그냥.. JSP 언어로 짜놓은겁니다.. 자바언어로... 근데 두번째꺼는 표현언어를 사용했죠..
1. 선택한 상품은 : ${param.sel } <br>
2. num1 + num2 = ${product.num1+product.num2} <br>
만약에 저게 JSP 언어로 되어있었다면...
<jsp:useBean id="product" class="jstlTest.Product" scope="session"></jsp:useBean>
1. 선택한 상품은 : <%= request.getParameter("sel") %> <br>
2. num1 + num2 = <%= product.getNum1() + product.getNum2() %> <br>
이렇게 변해야 똑같은 결과가 나옵니다.. 거참 신기하지요... -_-?? ㅋㅋㅋㅋ 표현언어는 참 좋은거 같군효..