JSP - EL ํํ์ ๋ฌธ๋ฒ๊ณผ ์ฌ์ฉ ๋ฐฉ๋ฒ
EL(Expression Language)์ ์๋ฐ ๋น์ ํ๋กํผํฐ, ๊ฐ์ JSP์ ํํ์ <%= %>์ด๋ ์ก์ ํ๊ทธ <jsp:useBean>๋ฅผ ์ฌ์ฉํ๋๊ฒ ๋ณด๋ค ์ฝ๊ณ ๊ฐ๊ฒฐํ๊ฒ ๊บผ๋ผ์ ์๊ฒ ํ๋ ๊ธฐ์ ์ด๋ค.
๋ํ static ๋ฉ์๋๋ฅผ ํธ์ถํ ์๋ ์๋๋ฐ JSP์์๋ ์ฃผ๋ก ์๋ธ๋ฆฟ ๋ณด๊ด์(JspContext, ServletRequest, HttpSession, ServletContext)์์ ๊ฐ์ ๊บผ๋ผ ๋ ์ฌ์ฉํ๋ค.
EL ํ๊ธฐ๋ฒ
${ }
- JSP๊ฐ ์คํ๋ ๋ ์ฆ์ ๋ฐ์๋๋ค. (Immediate evaluation)
- ๊ฐ์ฒด ํ๋กํผํฐ ๊ฐ์ ๊บผ๋ผ๋ ์ฃผ๋ก ์ฌ์ฉ
#{ }
- ์์คํ ์์ ํ์ํ๋ค๊ณ ํ๋จ๋ ๋ ๊ทธ ๊ฐ์ ์ฌ์ฉํ๋ค. (Deferred evaluation)
- ์ฌ์ฉ์ ์ ๋ ฅ๊ฐ์ ๊ฐ์ฒด์ ํ๋กํผํฐ์ ๋ด๋ ์ฉ๋๋ก ์ฃผ๋ก ์ฌ์ฉ
${member.no} ๋๋ ${member["no"])
์ EL ํํ์์ ์๋ฐ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
1
2
|
Member obj = (Member) pageContext.findAttribute("member");
int value = obj.getNo();
|
cs |
PageContext์ findAttribute()๋ ์๋ธ๋ฆฟ ๋ณด๊ด์๋ฅผ ์์๋๋ก ๋ชจ๋ ๋ค์ ธ์ ๊ฐ์ฒด๋ฅผ ์ฐพ๋๋ค.
๋ง์ง๋ง ๋ณด๊ด์์๋ ์๋ค๋ฉด null์ ๋ฐํํ๋ค.
JspContext → ServletRequest → HttpSession → ServletContext
EL์์ ๋ณด๊ด์ ๊ฒ์ ๋ฒ์ ์ง์
${member.no} ์ฒ๋ผ ๋ณด๊ด์๋ฅผ ์ง์ ํ์ง ์์ผ๋ฉด ๋ชจ๋ ๋ณด๊ด์๋ฅผ ๊ฒ์ํ์ง๋ง ํน์ ๋ณด๊ด์์์๋ง ์ฐพ๋๋ก ์ง์ ํ ์ ์๋ค.
๋ค์์ EL์์ ๋ณด๊ด์๋ฅผ ์ฐธ์กฐํ ๋ ์ฌ์ฉํ๋ ์ด๋ฆ์ด๋ค.
์ด๋ฆ | ๋ณด๊ด์ |
pageScope | JspContext |
requestScope | ServletRequest |
sessionScope | HttpSession |
applicationScope | ServletContext |
๋ฐ๋ผ์ ${requestScope.member.no}์ ๊ฐ์ด ์ฐ๋ฉด ServletRequest์์๋ง ํด๋น ๊ฐ์ฒด๋ฅผ ์ฐพ๋๋ค.
์ฐธ๊ณ ๋ก ์ด๋ ๋ค์ ์๋ฐ ์ฝ๋์ ๊ฐ๋ค.
1
2
|
Member obj = (Member) request.getAttribute("member");
int value = obj.getNo();
|
cs |
EL ๊ธฐ๋ณธ ๊ฐ์ฒด
๋ค์์ JSP์์ ๊ธฐ๋ณธ์ผ๋ก ์ ๊ณตํ๋ EL ๊ฐ์ฒด์ด๋ค.
๊ฐ์ฒด | ์ค๋ช | ์ฝ๋ |
pageContext | JSP์ PageContext ๊ฐ์ฒด | |
servletContext | ServletContext ๊ฐ์ฒด | ${pageContext.servletContext.๊ฐ์ฒด๋ช } |
session | HttpSession ๊ฐ์ฒด | ${pageContext.session.๊ฐ์ฒด๋ช } |
request | ServletRequest ๊ฐ์ฒด | ${pageContext.request.๊ฐ์ฒด๋ช } |
response | ServletResponse ๊ฐ์ฒด | |
param | ์์ฒญ ๋งค๊ฐ๋ณ์์ ๊ฐ ์กฐํ | ${param.๋งค๊ฐ๋ณ์๋ช } |
paramValues | ์์ฒญ ๋งค๊ฐ๋ณ์์ ๊ฐ ๋ฐฐ์ด ์กฐํ | ${paramValues.๋งค๊ฐ๋ณ์๋ช } |
header | HTTP ํค๋์ ๊ฐ ์กฐํ | ${header.ํค๋๋ช } |
headerValues | HTTP ํค๋์ ๊ฐ ๋ฐฐ์ด ์กฐํ | ${headerValues.ํค๋๋ช } |
cookie | ์ฟ ํค ๊ฐ ์กฐํ | ${cookie.์ฟ ํค๋ช } |
initParam | ์ปจํ ์คํธ ์ด๊ธฐํ ๋งค๊ฐ๋ณ์์ ๊ฐ ์กฐํ | ${initParam.๋งค๊ฐ๋ณ์๋ช } |
pageScope | page ๋ณด๊ด์์ ๊ฐ ์กฐํ | ${pageScope.๊ฐ์ฒด๋ช } |
requestScope | request ๋ณด๊ด์์ ๊ฐ ์กฐํ | ${requestScope.๊ฐ์ฒด๋ช } |
sessionScope | session ๋ณด๊ด์์ ๊ฐ ์กฐํ | ${sessionScope.๊ฐ์ฒด๋ช } |
applicationScope | application ๋ณด๊ด์์ ๊ฐ ์กฐํ | ${applicationScope.๊ฐ์ฒด๋ช } |
EL ๋ฆฌํฐ๋ด ํํ์
๋ฌธ์์ด, ์ ์, ๋ถ๋์์์ , Boolean, Null์ ์ฌ์ฉํ ์ ์๋ค.
1
2
3
4
5
6
|
๋ฌธ์์ด : ${"test"}
๋ฌธ์์ด : ${'test'}
์ ์ : ${20}
๋ถ๋์์์ : ${3.14}
Boolean : ${true}
null : ${null}
|
cs |
${null}์ ๋น ๋ฌธ์์ด(" ")๋ก ์ถ๋ ฅ๋๋ค.
EL ํํ์์ผ๋ก ๊ฐ ๊บผ๋ด๊ธฐ
๋ฐฐ์ด์์ ๊ฐ ๊บผ๋ด๊ธฐ
1
2
3
4
|
<%
pageContext.setAttribute("scores", new int[]{90, 80, 70, 100});
%>
${scores[2]}
|
cs |
์คํ ๊ฒฐ๊ณผ : 70
List ๊ฐ์ฒด์์ ๊ฐ ๊บผ๋ด๊ธฐ
1
2
3
4
5
6
7
8
9
|
<%
List<String> list = new LinkedList<>();
list.add("๊ฐ๋");
list.add("๋ค๋ผ");
list.add("๋ง๋ฐ");
pageContext.setAttribute("list", list);
%>
${list[1]}
|
cs |
์คํ ๊ฒฐ๊ณผ : ๋ค๋ผ
Map ๊ฐ์ฒด์์ ๊ฐ ๊บผ๋ด๊ธฐ
1
2
3
4
5
6
7
8
|
<%
Map<String, String> map = new HashMap<>();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
pageContext.setAttribute("map", map);
%>
${map["1"]}
|
cs |
์คํ ๊ฒฐ๊ณผ : a
์๋ฐ ๊ฐ์ฒด์์ ํ๋กํผํฐ ๊ฐ ๊บผ๋ด๊ธฐ
1
2
3
4
5
6
7
8
|
<%
pageContext.setAttribute("student",
new Student()
.setStudentNo("202012121")
.setStudentName("ํ๊ธธ๋")
);
%>
${student.studentNo} - ${student.studentName}
|
cs |
์คํ ๊ฒฐ๊ณผ : 202012121 - ํ๊ธธ๋
ResourceBundle ๊ฐ์ฒด์์ ๊ฐ ๊บผ๋ด๊ธฐ
src/MyResourceBundle.java
1
2
3
4
5
6
7
8
9
10
11
|
public class MyResourceBundle extends ListResourceBundle {
@Override
protected Object[][] getContents() {
return new Object[][] {
{"OK", "ํ์ธ"},
{"Cancle", "์ทจ์"},
{"Reset", "์ฌ์ค์ "},
{"Submit", "์ ์ถ"}
};
}
}
|
cs |
JSP
1
2
3
4
|
<%
pageContext.setAttribute("myRB", ResourceBundle.getBundle("MyResourceBundle"));
%>
${myRB.OK}
|
cs |
์คํ ๊ฒฐ๊ณผ : ํ์ธ
EL ํํ์์ ์ฐ์ฐ์
EL์์ ์ง์ํ๋ ์ฃผ์ ์ฐ์ฐ์๋ ์ฐ์ ์ฐ์ฐ์, ๋ ผ๋ฆฌ ์ฐ์ฐ์, ๊ด๊ณ ์ฐ์ฐ์, empty ์ฐ์ฐ์, ์กฐ๊ฑด ์ฐ์ฐ์๊ฐ ์๋ค.
EL ์ฐ์ ์ฐ์ฐ์
ํํ์ | ์คํ ๊ฒฐ๊ณผ |
${10+20} | 30 |
${10-20} | -10 |
${10*20} | 200 |
${10/20} | 0.5 |
${10 div 20} | 0.5 |
${10 % 20} | 10 |
${10 mod 20} | 10 |
EL ๋ ผ๋ฆฌ ์ฐ์ฐ์
ํํ์ | ์คํ ๊ฒฐ๊ณผ |
${true && false} | false |
${true and false] | false |
${false || true} | true |
${false or true] | true |
${not true} | false |
${!true} | false |
EL ๊ด๊ณ ์ฐ์ฐ์
ํํ์ | ์ฐ์ฐ์ |
${10 == 11} | false |
${10 eq 11} | false |
${10 != 11} | true |
${10 ne 11} | true |
${10 < 11} | true |
${10 lt 11} | true |
${10 > 11} | false |
${10 gt 11} | false |
${10 <= 11} | true |
${10 le 11} | true |
${10 >= 11} | false |
${10 ge 11} | false |
empty ์ฐ์ฐ์
- ๊ฐ์ด null์ด๋ฉด true
- ๋ฌธ์์ด, ๋ฐฐ์ด, Map, Collection ๊ฐ์ฒด์ ํฌ๊ธฐ๊ฐ 0์ด๋ฉด true
- ๊ทธ ์ธ์๋ false
1
2
3
4
|
<%
pageContext.setAttribute("title", "EL ์ฐ์ฐ์");
%>
${empty title}, ${empty title2}
|
cs |
์คํ ๊ฒฐ๊ณผ : false, true
์กฐ๊ฑด ์ฐ์ฐ์
EL ์กฐ๊ฑด ์ฐ์ฐ์๋ ์๋ฐ 3ํญ ์ฐ์ฐ์์ ๋ฌธ๋ฒ์ด ๋์ผํ๋ค.
1
|
${10 > 20 ? "ํฌ๋ค" : "์๋ค"}
|
cs |
์คํ ๊ฒฐ๊ณผ : ์๋ค
EL ์์ฝ ํค์๋
and, or, not, eq, ne, lt, gt, le, ge, true, false, null, instanceof, empty, div, mod
์๋ธ๋ฆฟ ๋ณด๊ด์์ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ ๋ Key๊ฐ EL ์์ฝ ํค์๋์ด๋ฉด EL ํํ์์์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
1
2
3
4
|
<%
pageContext.setAttribute("ne", "EL ์์ฝ ํค์๋๋ฅผ Key๋ก ์ฌ์ฉ");
%>
${ne}
|
cs |
EL ํ์ฉ
๋ค์์ ํ์ ๊ฐ์ ์์ ์ ํ์ ์ ๋ณด ๋ณ๊ฒฝ ํ์ด์ง์ด๋ค.
StudentUpdateForm.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<%@ page import="com.atoz_develop.spms.vo.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
Student loginMember = (Student) session.getAttribute("student");
Student student = (Student) request.getAttribute("student");
%>
<html>
<head>
<title>ํ์ ์ ๋ณด</title>
</head>
<body>
<h1>ํ์์ ๋ณด</h1>
<form action='update' method='post'>
ํ๋ฒ: <input type='text' name='student_no' value='<%=student.getStudentNo()%>' readonly><br>
ํ๊ณผ: <input type='text' name='department' value='<%=student.getDepartment()%>'><br>
์ด๋ฆ: <input type='text' name='student_name' value='<%=student.getStudentName()%>'><br>
ํ๋
: <input type='text' name='grade' value='<%=student.getGrade()%>' readonly><br>
์ฑ๋ณ: <input type='text' name='gender' value='<%=student.getGender()%>' readonly><br>
๋์ด: <input type='text' name='age' value='<%=student.getAge()%>' readonly><br>
์ ํ๋ฒํธ: <input type='text' name='phone_number' value='<%=student.getPhoneNumber()%>'><br>
์ฃผ์: <input type='text' name='address' value='<%=student.getAddress()%>'><br>
<input type='submit' value='์์ '>
<%
if(loginMember != null && "admin".equals(loginMember.getStudentNo())) {
%>
<input type="button" value="์ญ์ " onClick="location.href='delete?student_no=<%=student.getStudentNo()%>'" />
<%
}
%>
<input type='button' value='์ทจ์' onClick='location.href="list"'>
</form>
</body>
</html>
|
cs |
<% %>์ session์์ ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ค๋ ์๋ฐ ์ฝ๋๋ฅผ ์์ฑํ๊ณ <%= %>๋ฅผ ์ฌ์ฉํ๋ ๋์ , EL ํํ์์ผ๋ก ๋ฐ๊พธ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
StudentUpdateForm.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ํ์ ์ ๋ณด</title>
</head>
<body>
<h1>ํ์์ ๋ณด</h1>
<form action='update' method='post'>
ํ๋ฒ: <input type='text' name='student_no' value='${requestScope.student.studentNo}' readonly><br>
ํ๊ณผ: <input type='text' name='department' value='${requestScope.student.department}'><br>
์ด๋ฆ: <input type='text' name='student_name' value='${requestScope.student.studentName}'><br>
ํ๋
: <input type='text' name='grade' value='${requestScope.student.grade}' readonly><br>
์ฑ๋ณ: <input type='text' name='gender' value='${requestScope.student.gender}' readonly><br>
๋์ด: <input type='text' name='age' value='${requestScope.student.age}' readonly><br>
์ ํ๋ฒํธ: <input type='text' name='phone_number' value='${requestScope.student.phoneNumber}'><br>
์ฃผ์: <input type='text' name='address' value='${requestScope.student.address}'><br>
<input type='submit' value='์์ '>
<input type="button" value="์ญ์ " onClick="location.href='delete?student_no=${student.studentNo}'"
style="display: ${'admin' eq sessionScope.student.studentNo ? inline-block : none}">
<input type='button' value='์ทจ์' onClick='location.href="list"'>
</form>
</body>
</html>
|
cs |
์ด์ ๊ฐ์ด EL์ ์ฌ์ฉํ๋ฉด <% %>์ ๊ฐ์ฒด๋ฅผ ๊บผ๋ด๋ ์ฝ๋๋ฅผ ์์ฑํ๊ฑฐ๋ <jsp:useBean>์ ์ฌ์ฉํ ํ์๊ฐ ์๊ณ <%= %>๋ณด๋ค ํจ์ฌ ๊ฐ๊ฒฐํ๊ฒ ์์ฑํ ์ ์๋ค.
์ด๋ ๊ฒ EL๋ง ์ฌ์ฉํด๋ ์ฝ๋๊ฐ ํจ์ฌ ๊ฐ๊ฒฐํด์ง์ง๋ง ๋์๊ฐ JSP์ ์๋ฐ๋ก ์์ฑ๋ ๋ฐ๋ณต๋ฌธ ๊ฐ์ ์ฝ๋๋ฅผ ์์ ๋ ค๋ฉด JSTL์ ์ฌ์ฉํด์ผ ํ๋ค.
- [JSP] JSTL ์ฌ์ฉ ๋ฐฉ๋ฒ - ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ค์ด๋ก๋ ๋ฐ ์ธํ
๋๊ธ