JSP의 표현식(Expression)을 조금 더 효율적이고 간단하게 작성할 수 있도록 고안된 언어
화면에 표현하고자 하는 자바코드를 ${value}형식으로 작성함(value 작성법은 여럭가지 존재)
EL의 특징
1. get 이라는 단어 사용 X
왜? 표현 언어 == 출력용 언어 == 출력은 얻어와서 밖에 못함
2. EL은 null과 빈칸으로 출력함(null과 관련된 것은 모두 빈칸)
EL로 Parmeter 얻어와서 출력하는 방법 ${param.name 속성값}
1) JSP 표현식 :
<%= request.getParameter("inputName") %> /
<%= request.getParameter("inputAge") %> /
<%= request.getParameter("inputAddress") %>
<%= request.getParameter("inputAddress2") %>
<!-- null -->
<br><br>
2) EL(표현 언어) :
${ param.inputName } /
${ param.inputAge} /
${ param.inputAddress }
${ param.inputAddress2 }
<!-- 빈칸 -->
<h3>request에서 속성(Attribute) 얻어오기</h3>
<pre>
Servlet에서 추가된 속성을 표현(출력)하려는 경우
request에 세팅된 속성(Attribute)의 Key값만 작성하여 출력할 수 있다!
(import, getAttribute(), 다운캐스팅, 변수 저장 모두 생략)
\${속성Key}
\${속성Key.필드명}
(단, getter가 작성되어 있어야지만 가능)
</pre>
<%
String hobby = (String)request.getAttribute("hobby");
Person person = (Person)request.getAttribute("person");
%>
1) JSP 표현식 : <%= hobby %>
<!-- Person클래스의 toString() 출력 -->
<br> <%=person %>
<!-- Person 클래스의 getter를 이용해서 얻어와서 출력 -->
<br> <%= person.getName() %>
<br> <%= person.getAge() %>
<br> <%= person.getAddress() %>
<br><br>
2) EL(표현 언어) : ${hobby}
<br> ${ person }
<br> ${ person.name }
<br> ${ person.age }
<br> ${ person.address }
<hr>
<h3>null 처리 방법</h3>
<pre>
EL에서 null을 출력해야 되는 경우 ""(빈 문자열)을 출력한다.
+ NullPointerException이 발생하는 코드에서도 ""(빈 문자열)을 출력한다.
+ EL은 null인 경우를 확인할 때 empty를 통해서 확인할 수 있다.
</pre>
<% List<String> list =null; %>
1) JSP 표현식 : <%= list %> <br>
<%= list ==null %>
<br><br>
2) EL(표현 언어) : ${list}<br>
${empty null }
<h3 style="color:red">EL의 empty는 null과 비어있는 컬렉션을 비어있는 것으로 취급함</h3>
<%
list = new ArrayList<String>();
//list가 ArrayList 객체를 참조 == null 아님
// 참조하고 있는 ArrayList에 내용 없음 == 비어있음
%>
${empty list }
${list == null }
'괴발개발 > java' 카테고리의 다른 글
| 특정 게시판에서 일정한 범위의 목록 조회 width 페이지 네이션Page nation (0) | 2024.07.09 |
|---|---|
| Lombok 라이브러리 (0) | 2024.06.28 |
| Servlet -1 이론 (0) | 2024.06.24 |
| 컬렉션(Collection) (0) | 2024.05.08 |
| 다형성 (1) | 2024.05.01 |