EL Tag

MIN·2023년 12월 10일
0

Servlet/JSP

목록 보기
16/21
post-thumbnail

🍉 EL Tag란?

기존 표현식에서 attribute 및 parameter 값을 출력할 때 코드가 긴 점을 개선한 형태다.
기본적으로 표현식에 포함되는 EL Tag는 출력문에 해당한다.

다음 내용에서 EL Tag를 이용하여 각 자료형 및 객체들을 어떻게 출력하는지 살펴보자!

🍉 attribute 처리

  • 내장 객체에 저장된 attribute의 이름을 지정하면 값을 출력한다.
  • Object 타입의 객체가 저장되므로, 출력시에는 객체의 toString() 결과를 출력한다.
  • 배열 및 List에는 [ ]형태로 index를 지정할 수 있다.
  • 자바 빈즈 객체는 . 연산자를 이용하여 필드이름을 지정하면 getter를 호출하여 출력한다.
  • Map 형태의 객체는 [''] 안에 key 이름을 넣어서 value를 출력한다.

💡 String

<% 
	String str = "Hello, world"; 
    pageContext.setAttribute("str", str);
%>

<p>표현식 : <%=str %></p>
<p>EL Tag : ${str }</p>

💡 Array

<% 
	Integer[] arr = {4, 8, 2, 7, 6}; 
    pageContext.setAttribute("arr", arr);
%>

<p>표현식 : <%=arr[0] %>, <%=arr[1] %></p>
<p>EL Tag : ${arr[0] }, ${arr[1] }</p>

💡 List

<% List<Integer> list = Arrays.asList(arr); %>
<!-- 위에있는 arr를 가져와서 list로 만들었음!-->

<% pageContext.setAttribute("list", list); %>

<p>표현식 : 
	<%=list.get(0) %>, <%=list.get(1) %>,
    <!--attribute를 사용하지 않을 떄는 편리하지만-->
	<%=((List<Integer>)pageContext.getAttribute("list")).get(2) %>
	<!-- Attribute에서 꺼내쓸 때 EL Tag를 쓰지않으면 불편하다! -->
</p>

<p>EL Tag : 
	${list[0] }, ${list[1] }, ${list[2] }
</p>

💡 Map

<% 
	HashMap<String, String> map = new HashMap<>();
    map.put("key1", "value1");
	map.put("key2", "value2");
    
	pageContext.setAttribute("map", map);
%>

<p>표현식 : 
	<%=map.get("key1") %>, <%=map.get("key2") %>,
	<%=((HashMap<String,String>)pageContext.getAttribute("map")).get("key2") %>
</p>
<p>EL Tag : ${map.apple }, ${map['banana'] }</p>
<!-- ${map.banana }, ${map["banana"] }, ${map['banana'] } 3가지 모두 같은 표현 -->

💡 객체(object)

<%
   TestDTO ob1 = new TestDTO();
   ob1.setName("김철수");
   ob1.setAge(20);
   
   pageContext.setAttribute("ob1", ob1);
%>
  
<p>표현식 : 
	<%=ob1.getName() %>, <%=ob1.getAge() %>,
    <!-- attribute 사용안할때-->
    
	<%=((TestDTO)pageContext.getAttribute("ob1")).getName() %>,
	<%=((TestDTO)pageContext.getAttribute("ob1")).getAge() %>
     <!-- attribute 사용할때-->
</p>

<p>EL Tag : ${ob1.name }, ${ob1.age }</p>
  

💡 List 내의 객체

<%
   TestDTO ob1 = new TestDTO();
   ob1.setName("김철수");
   ob1.setAge(20);
   
   TestDTO ob2 = new TestDTO();
   ob1.setName("이영희");
   ob1.setAge(25);
   
   ArrayList<TestDTO> arrayList = new ArrayList<>();
   arrayList.add(ob1);
   arrayList.add(ob2);
   
   pageContext.setAttribute("arrayList", arrayList);
%>

<p>표현식 : 
	<%=arrayList.get(1).getName() %>
	<%=((ArrayList<TestDTO>)pageContext.getAttribute("arrayList")).get(1).getAge() %>,
</p>

<p>EL Tag : ${arrayList[1].name }, ${arrayList[1].age }</p>


🍉 parameter 처리

<form>
  <input type="text" name= "name">
</form>

<p>표현식 : <%=request.getParameter("name") %></p>
<p>EL Tag : ${param.name }</p>
<!-- param: 예약어! -->




🔥 한줄평
너무...너무 어렵다...
사용하면 편하다는 것은 알겠는데 정확하게 어떤 역할을 하는지 파악하는데 시간이 오래 걸릴 것 같다..

profile
기초부터 시작하는 감쟈 ※ 소개글 확인해주세요!! ※

0개의 댓글