728x90
반응형
#EL(Expression Language) 표현식 문법 및 사용방법
#데이터 추출 표현식
${ }
#EL의 범위
pageScope
requestScope
sessionScope
applicationScope
예제 1)
1. forward.jsp
<%@page import="kr.ac.green.Dummy"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
request.setAttribute("emptyArr", new Vector<String>().toArray());
Dummy d = new Dummy();
d.setNum(100);
d.setTitle("newTitle");
Map<String, String> map = new Hashtable<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
Vector<String> list = new Vector<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
request.setAttribute("map", map);
request.setAttribute("list", list);
request.setAttribute("arr", new int[]{1,2,3,4,5});
request.setAttribute("dummy", d);
request.setAttribute("some", "other");
session.setAttribute("yourName", "james");
%>
<jsp:forward page="el_ex.jsp">
<jsp:param value="paramValue" name="paramName"/>
</jsp:forward>
</body>
</html>
2. el_ex.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@page import="kr.ac.green.Dummy"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%--
EL의 범위
pageScope
requestScope
sessionScope
applicationScope
EL vs <%= %>차이(값이 null일 경우)
표현식은 null
EL은 아무것도 안나온다.
getter가 있어야 한다.
--%>
<%
Dummy d = (Dummy)request.getAttribute("dummy");
%>
<!-- 비어있는지 여부 -->
${empty emptyArr }
<hr>
연산 :
<%= 1+2+3 %> : ${1+2+3 } : ${10 > 11 }
<hr>
map : ${map["key2"] } : ${map.key3 }
<hr>
<!-- 리스트나 배열은 대괄호 -->
list : ${list[0] }
<hr>
기존 parameter : <%= request.getParameter("paramName") %>
<%= request.getParameter("paramSome") %>
<hr>
EL parameter : ${param.paramName } : ${param.paramSome }
<hr>
기존방식 : <%= request.getAttribute("some").toString() %>
: <%= session.getAttribute("yourName").toString() %>
: <%= application.getAttribute("myName") %>
: <%= d.getNum() %> : <%= d.getTitle() %>
<hr>
EL : ${ requestScope.some } : ${ sessionScope.yourName } : ${ applicationScope.myName }
: ${ requestScope.dummy.num } : ${ requestScope.dummy.title }
<hr>
EL other : ${ some } : ${ yourName } : ${ myName }
: ${ dummy.num } : ${ dummy.title }
다른 방법 : ${dummy["num"] } : ${dummy["title"] }
<hr>
배열 : ${ arr[2] }
</body>
</html>
3. Dummy.java
package kr.ac.green;
public class Dummy {
private int num;
private String title;
public Dummy() {
}
public Dummy(int num, String title) {
super();
this.num = num;
this.title = title;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
728x90
반응형
'IT > JSP' 카테고리의 다른 글
[JSP/JSTL] - fn:indexOf(string, substring) 사용법 (0) | 2021.07.27 |
---|---|
[JSP] - 33. JSTL(JSP Standard Tag Library) 사용 방법 (0) | 2020.08.10 |
[JSP] - 31. DBCP(커넥션 풀) 이란 ? (0) | 2020.08.08 |
[JSP] - 30. 이클립스에서 JUnit 단위 테스트 하는 방법 (0) | 2020.08.08 |
[JSP] - 29. 서블릿(Servlet) 이란 (0) | 2020.07.28 |
댓글