๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

[JSP] JSTL ํ™œ์šฉ ๋ฐฉ๋ฒ• - JSP์—์„œ ์ž๋ฐ” ์ฝ”๋“œ ์ œ๊ฑฐ

by Leica 2020. 2. 15.
๋ฐ˜์‘ํ˜•
  • ๊ด€๋ จ ๊ธ€

- [JSP] JSTL ์‚ฌ์šฉ ๋ฐฉ๋ฒ• - ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹ค์šด๋กœ๋“œ ๋ฐ ์„ธํŒ…

- [JSP] JSTL ์‚ฌ์šฉ ๋ฐฉ๋ฒ• - ์ฃผ์š” ํƒœ๊ทธ ๋ฌธ๋ฒ• ์ •๋ฆฌ

 

[JSP] JSTL ํ™œ์šฉ ๋ฐฉ๋ฒ• - JSP์—์„œ ์ž๋ฐ” ์ฝ”๋“œ ์ œ๊ฑฐ

 

๋‹ค์Œ์€ ํšŒ์› ๋ชฉ๋ก์„ ์ถœ๋ ฅํ•˜๋Š” JSP์ด๋‹ค. JSTL, EL์„ ์‚ฌ์šฉํ•ด์„œ ์ด 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
34
35
36
37
38
39
<%@ page import="com.atoz_develop.spms.vo.Student" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%
    Student loginMember = (Student) session.getAttribute("student");
%>
<html>
<head>
    <title>ํ•™์ƒ ๋ชฉ๋ก</title>
</head>
<body>
<jsp:include page="/Header.jsp"/>
<h1>ํ•™์ƒ ๋ชฉ๋ก</h1>
<p><a href="add">์‹ ๊ทœ ํ•™์ƒ</a></p>
<%
    List<Student> students = (ArrayList<Student>)request.getAttribute("students");
    for(Student student: students) {
        if(loginMember != null && "admin".equals(loginMember.getStudentNo())) {
%>
<input type="button" value="์‚ญ์ œ" onClick="location.href='delete?student_no=<%=student.getStudentNo()%>'" />
<%
        }
%>
<%=student.getStudentNo()%>,
<%=student.getDepartment()%>,
<a href="update?student_no=<%=student.getStudentNo()%>"><%=student.getStudentName()%></a>,
<%=student.getGrade()%>,
<%=student.getGender()%>,
<%=student.getAge()%>,
<%=student.getPhoneNumber()%>,
<%=student.getAddress()%><br>
<%
    }
%>
<jsp:include page="/Tail.jsp"/>
</body>
</html>
 
cs

 

์šฐ์„  import ์ง€์‹œ์ž์™€ loginMember, students๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ฝ”๋“œ, for๋ฌธ์„ ์ œ๊ฑฐํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  JSTL Core ํƒœ๊ทธ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ taglib ์ง€์‹œ์ž๋ฅผ ์„ ์–ธํ•œ๋‹ค.

 

 

for๋ฌธ์„ <c:forEach>๋กœ, if๋ฌธ์„ <c:if>๋กœ ๋Œ€์ฒดํ•˜๊ณ  ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํ‘œํ˜„์‹ <%= %>์„ EL ํ‘œํ˜„์‹์œผ๋กœ ๋Œ€์ฒดํ•œ๋‹ค.

 

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
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>ํ•™์ƒ ๋ชฉ๋ก</title>
</head>
<body>
<jsp:include page="/Header.jsp"/>
<h1>ํ•™์ƒ ๋ชฉ๋ก</h1>
<p><a href="add">์‹ ๊ทœ ํ•™์ƒ</a></p>
<c:forEach var="student" items="${students}">
    <c:if test="${sessionScope.student.studentNo == 'admin'}">
        <input type="button" value="์‚ญ์ œ" onClick="location.href='delete?student_no=${student.studentNo}'" />
    </c:if>
    ${student.studentNo},
    ${student.department},
    <a href="update?student_no=${student.studentNo}">${student.studentName}</a>,
    ${student.grade},
    ${student.gender},
    ${student.age},
    ${student.phoneNumber},
    ${student.address}<br>
</c:forEach>
<jsp:include page="/Tail.jsp"/>
</body>
</html>
cs

 

์‹คํ–‰ ๊ฒฐ๊ณผ

 

์ด์ œ ์ด jsp ํŒŒ์ผ์—๋Š” ์ž๋ฐ” ์ฝ”๋“œ๊ฐ€ ์™„์ „ํžˆ ์ œ๊ฑฐ๋˜์–ด ์†Œ์Šค๊ฐ€ ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•˜๊ณ  ์ง๊ด€์ ์ด๋ฉฐ ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋ฐ”๋€Œ์—ˆ๋‹ค.


  • ๊ด€๋ จ ๊ธ€

- [JSP] JSTL ์‚ฌ์šฉ ๋ฐฉ๋ฒ• - ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹ค์šด๋กœ๋“œ ๋ฐ ์„ธํŒ…

- [JSP] JSTL ์‚ฌ์šฉ ๋ฐฉ๋ฒ• - ์ฃผ์š” ํƒœ๊ทธ ๋ฌธ๋ฒ• ์ •๋ฆฌ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€