MapleStory Finger Point

๐ŸŸค JAVA/๐ŸŸค JSP

[JSP] JSTL (JSP Standard Tag Library)

HYEJU01 2024. 6. 28. 00:06


[ JSTL (JSP Standard Tag Library) ]
์ž๋ฐ” ์ œ์–ด๋ฌธ์„ ํƒœ๊ทธํ™” ์‹œํ‚จ๋‹ค. => ์ž๋ฐ” ์ฝ”๋“œ๊ฐ€ ๋‹ค ์‚ฌ๋ผ์ง„๋‹ค.

  • ( JSP์˜ ๊ฒฝ์šฐ HTMLํƒœ๊ทธ์™€ ๊ฐ™์ด ์‚ฌ์šฉ๋˜์–ด ์ „์ฒด์ ์ธ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง.)
  • ๋ณ„๋„์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ ํ•ด์ค˜์•ผํ•จ

 

java

   
        // ์กฐ๊ฑด๋ฌธ ์˜ˆ์‹œ
        if ((Integer) request.getAttribute("age") >= 18) {
            request.setAttribute("status", "You are an adult.");
        } else {
            request.setAttribute("status", "You are not an adult.");
        }

 

 

jsp 

    <ul>
        <%
            List<String> fruits = (List<String>) request.getAttribute("fruits");
            for (String fruit : fruits) {
        %>
            <li><%= fruit %></li>
        <%
            }
        %>
    </ul>

 

jstl

    <ul>
        <!-- ๋ฐ˜๋ณต๋ฌธ ์˜ˆ์‹œ -->
        <c:forEach var="fruit" items="${fruits}">
            <li>${fruit}</li>
        </c:forEach>
    </ul>

 

 

 

 

 

 

[ JSTL ์„ค์น˜ํ•˜๊ธฐ ]

 

jakarta-taglibs-standard-1.1.2
jstl / standard
* lib ์— ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ !
(์ถ”๊ฐ€์‹œ ๋ฒ„์ „ ๋ฌธ์ œ๋กœ ์˜ˆ์™ธ) ํ†ฐ์บฃ 10๋ฒ„์ „ -> ์„œ๋ธ”๋ฆฟ์ด ์ž์นด๋ฅดํƒ€ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋ณ€๊ฒฝ jstl ๋„ ์ž์นด๋ฅดํƒ€ ๋ฒ„์ „์œผ๋กœ ๋‹ค์šด

 

 

 

 

 

[ JSTL Core Library ]

  • Core ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๊ธฐ๋ณธ์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ถœ๋ ฅ, ์ œ์–ด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ ๊ฐ™์€ ๊ธฐ๋Šฅ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฝ”์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐ˜๋“œ์‹œ JSP ํŒŒ์ผ ๋‚ด์˜ ์ง€์‹œ์ž ํƒœ๊ทธ ์ž‘์„ฑํ•œ๋‹ค.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

 

 

 

 

 

 

 

[ JSTL  ์˜ˆ์ œ ]

 

//jstl.form.jsp
form action="jstl_if.jsp" method="post"
input ์ด๋ฆ„
input ๋‚˜์ด
input ํ™•์ธ


//jstl_if.jsp

1) ์„ ์–ธ๋ถ€์— ํ‘œ๊ธฐ
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
2) 
<c: ์”จ์ฝœ๋ก  ํ•˜๋ฉด ์—ฌ๋Ÿฌ ํƒœ๊ทธ๋“ค์ด ๋‚˜์˜ค๋Š”๋ฐ ์“ธ์ˆ˜์žˆ๋•…

// <c:set> </c:set>
// <c:set />   
// ํƒœ๊ทธ ๊ทœ์น™ : ๋‹ซ๋Š” ๋ฐฉ๋ฒ• ๋‘๊ฐœ

<c:set var="a" value="10" />  // a = 10
<c:out value="ํ™๊ธธ๋™"/>
<c:out value="${a }"/>  //el ํƒœ๊ทธ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ

<c:if test="true">
<p> ๋ฌด์กฐ๊ฑด ์‹คํ–‰๋˜๋Š” ๋ฌธ์žฅ </p> 
</c:if>

<c:if test="${a >= 10}">
<p> ๋ฌด์กฐ๊ฑด ์‹คํ–‰๋˜๋Š” ๋ฌธ์žฅ </p> 
</c:if>

<c:if test="${param.age >= 20}">
<p> ์„ฑ์ธ ์ž…๋‹ˆ๋Œ• ~</p> 
</c:if>

 

 

<%
 String age = request.getParmeter("age");
int x = Integer.parseInt(age);
%>
<% if (x>=20){ %>
์„ฑ์ธ
<% } %>


//----> ์ค„์—ฌ์ง

<c: if test="${param.name == 'ํ™๊ธธ๋™' }">
//jsp ๋ฌธ์žฅ์€ == ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ

 

 

* jstl.form.jsp
form action="jstl_choose.jsp" method="post"
input ์ด๋ฆ„
input ๋‚˜์ด
input ํ™•์ธ

1) ํผ ์ž‘์„ฑ
2) ์„ ์–ธ๋ฌธ taglic
3) // if ~ else ๊ตฌ๋ฌธ 
<c:choose>
<c:when test="${param.name == 'ํ™๊ธธ๋™' }">
๊ธธ๋™์ด </c:when>
<c:when test="${param.name == '์ด์ˆœ์‹ ' }">
์ˆœ์‹ ์ด </c:when>
<c:otherwise>๋‘˜๋‹ค ์•„๋‹˜</c:otherwise>

 

<c:choose>
<c:when test="${param.age >= 20}">
์„ฑ์ธ
</c:when>
<c:otherwise> ๋ฏธ์„ฑ๋…„์ž </c:otherwise>
</c:choose>
------------------------------
* jstl.foreach.jsp
1) ํผ์ž‘์„ฑ
2) ์„ ์–ธ๋ฌธ 
3) // foreach ๋ฌธ
<% 
int sum = 0;
for (int i = 1; i <= 100; i+=2){
sum +i;
}
%>
<%= sum %>
------------------------------
<c:set var="sum" value="0"/>
<c:forEach var="i" begin="1" end="100" step="2">
<c:set var="sum" value="${sum + i}" />
<c:forEach>
๊ฒฐ๊ณผ : ${sum}
//์Šคํ… ์ƒ๋žต์‹œ 1
------------------------------
<c:forEach var="i" begin="1" end="9">
3 x ${1} = ${3*i} 
<c:forEach>
------------------------------
<c:forEach var="i" begin="1" end="9">
<c:forEach var="j" begin="1" end="9">
${i} x ${j} = ${i*j} 
<c:forEach>
<c:forEach>

 

------------------------------
<%
int[] arr = {1,2,3,4,5}
for (int a : arr){
out.println(a);
}
%>

<c:set var="arr" value="<% new int[] {1,2,3,4,5} %> "/>

// vatStatus for๋ฌธ ์ƒํƒœ๋ฅผ ์•Œ์ˆ˜์ž‡๋Š” ๋ณ€์ˆ˜ ~
<c:forEach var"a" items="{$arr}" varStatus="s">
${s}
${s.index} // ์ธ๋ฑ์Šค ๊ฐ’
${s.count} // ๋ฒˆํ˜ธ ์•Œ๋ ค์คŒ 
</c:forEach>

------------------------------
* jstl_fmt.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 

// parseNumber, paseDate, formatNumber, formantDate
parse -> ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ณ€๊ฒฝ
format -> ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ ํ˜•์‹์„ ๋ฐ”๊ฟˆ

// ์ˆซ์ž์˜ ํ‘œํ˜„ ํ˜•์‹์„ ๋ฐ”๊ฟˆ
<c:set var="a" value="2024" />
<fmt:formatNumber value="${a}" pattern="0000000" />
<fmt:formatNumber value="${a}" pattern="0000000.00" />

// ๋‚ ์งœ์˜ ํ‘œํ˜„ ํ˜•์‹์„ ๋ฐ”๊ฟˆ
<c:set var ="b" value="<%=new Date()%> "/>
<fmt:formatDate value="${b}" pattern="yyyy๋…„MM์›”dd์ผ HH:mm:ss"/>
<fmt:formatDate value="${b}" pattern="yyyy-MM-dd HH:mm:ss"/>

// ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜
<c:set var="c" value="1.100"/>
<fmt:parseNumber value="${c}" pattern="0.000" />

// ๋ฌธ์ž๋ฅผ ๋‚ ์งœ๋กœ ๋ณ€ํ™˜
<c:set var="str" value="2024-06-27"/>
<fmt:parseDate var="result" value="${str}" pattern="yyyy-MM-dd" />

${result } ๋กœ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ

<c:set var="e" value="2020-05-03 21:30:22"/>
<c:set var="f" value="<%new Date() %>" /> 

<fmt:parseDate var="c" value="${e}" pattern="yyyy-MM-dd HH:mm:ss" />
<fmt:formatDate value="${c}" pattern="yyyy๋…„MM์›”dd์ผ HH์‹œmm๋ถ„ss์ดˆ" />
<fmt:formatDate value="${f}" pattern="yyyy๋…„MM์›”dd์ผ HH์‹œmm๋ถ„ss์ดˆ"/>



<!--<c:forEach var="dto" items="${list }">
<c:if test="${dto.departmentId eq param.departmentId}">
<c:set var="name_find" value="${dto.departmentName}"/>
</c:if>
</c:forEach>-->