일단 설정해야한다 mvnrepository 가자
https://mvnrepository.com/artifact/javax.servlet/jstl
내가 착하니 파일을 올려둔다

lib에 투하하자
https://daesuni.github.io/jstl/ <<참고사이트
https://blog.naver.com/wonminst/90096004269 <<설명
- JSTL : JSTL + EL의 조합 HTML 코드 내에 java 코드인 스크립틀릿 <%= student %>를 ${student}로, <%=if %>문을 <c:if>, <%=for%>문을 <c:forEach>로 대체하여 사용
- 요녀석을 먼저 만들어줘야한다
- <c:set> -변수명에 값을 할당
- <c:out>- 값을 출력
- <c:if> -조건식에 해당하는 블럭과 사용될 scope설정
- <c:choose> -다른 언어의 switch와 비슷
- <c:when>- switch문의 case에 해당
- <c:otherwise>- switch문의 default에 해당
- <c:forEach> -다른언어의 loop문 items 속성에 배열을 할당할 수 있음
- scope =page가 생략되니까 el로 읽을 수 있구나를 생각하자 ||기본값은 page이다.
1.value안적고 <c:set var="age">30</c:set>이렇게 적으면 value적은것 처럼 되는군
2. target이 예전에 setProperty에서 쓰던 name같은데?
- target : 프로퍼티 값을 설정할 대상 객체. 표현식(<%=변수%>)이나 EL 변수(${varName})를 지정. 대상 객체는 자바빈 객체 or Map.
<%@page import="vo.Person"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>JSTL</h1>
<hr>
<c:set var="name" value="짱구" scope="page"/>
${name }<br>
<hr>
<c:set var="msg" value="Hello"/>
\${msg } =${msg }<br>
<c:set var="age">
30
</c:set>
\${age } =${age }<br>
<hr>
<c:set var ="p" value="<%=new Person()%>"/>
<c:set target="${p }" property="name" value="짱구"/>
<c:set target="${p }" property="age" value="12"/>
\${p } = ${p }<br>
<hr>
<c:set var="add" value="${10+5 }"/>
\${add } = ${add }<br>
<c:set var="flag" value="${10>5 }"/>
\${flag } = ${flag }<br>
</body>
</html>
package vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String name;
private int age;
@Override
public String toString() {
return name +"," +age;
}
}
- c:out - 값 출력
https://2ham-s.tistory.com/274 <<굳이 쓰는이유(보안성)
- escapeXml="false" = html태그를 인식// -기본값 true
- default: 값 없을 때 나오는 놈
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL-out</title>
</head>
<body>
<c:set var="iTag">
i태그는 <i>기울임</i>을 표현한다
</c:set>
<h4>기본사용</h4>
<c:out value="${iTag }"/><br>
${iTag }<br>
<hr>
<h4>escapeXml 속성</h4>
<c:out value="${iTag }" escapeXml="false"/>
<hr>
<%
String s = "gg";
pageContext.setAttribute("person", s);
%>
<h4>default속성</h4>
<c:out value="${param.name }" default="이름없음"/><br>
<c:out value="${person }" default="빈 문자열도 값이다"/><br>
<c:out value="${s}" default="빈 문자열도 값이다"/><br>
<c:out value="${a}" default="빈 문자열도 값이다"/><br>
<c:out value="" default="빈 문자열도 값이다"/><br>
<hr>
<h4>remove</h4>
<c:remove var="iTag"/>
<c:out value="${iTag }" default="없다"/>
</body>
</html>
- scope지정해주고 출력하고 삭제해보고
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:set var="txt" value="page-txt"/>
<c:set var="txt" value="request-txt" scope="request"/>
<c:set var="txt" value="session-txt" scope="session"/>
<c:set var="txt" value="application-txt" scope="application"/>
<h4>txt변수값 출력</h4>
txt: ${txt }<br>
txt: <c:out value="${txt }"/><br>
txt:${requestScope.txt }<br>
<c:remove var="txt" scope="page"/>
<hr>
<h4>삭제후 출력</h4>
txt: ${txt }<br>
txt: <c:out value="${txt }"/><br>
txt: ${requestScope.txt }<br>
txt: ${sessionScope.txt }<br>
</body>
</html>
- JSTL에는 if else 문이 없다 (if문)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>색상을 선택</h3>
<form action="04JSTL_result.jsp" >
<select name="color">
<option value="1">빨강</option>
<option value="2">초록</option>
<option value="3">파랑</option>
</select>
<input type="submit" value="바꾸기">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>색상 적용 페이지</title>
</head>
<body>
<h3>색 : ${param}</h3>
<h3>색 : ${param.color}</h3>
<hr>
<c:if test="${param.color==1 }">
<h3>파라미터로 받은 색 : <span style="color:red;">색깔</span></h3>
</c:if>
<c:if test="${param.color==2 }">
<h3>파라미터로 받은 색 : <span style="color:green;">색깔</span></h3>
</c:if>
<c:if test="${param.color==3 }">
<h3>파라미터로 받은 색 : <span style="color:blue;">색깔</span></h3>
</c:if>
</body>
</html>
- 그래서 다중조건 처리하는녀석이 있다
다중조건 처리 -choose, when, otherwise(else)
https://dullyshin.github.io/2019/08/08/JSTL-if/
- <c:choose> 태그: 조건에 따라 다른 블록을 실행할 수 있도록 합니다. <c:choose> 태그는 하나 이상의 <c:when> 또는 <c:otherwise> 태그로 구성됩니다.
- <c:when> 태그: 조건이 참인 경우 해당 블록을 실행합니다. test 속성을 사용하여 조건을 지정할 수 있습니다. test 속성에는 EL(표현 언어)을 사용하여 조건을 표현합니다.
- <c:otherwise> 태그: <c:when> 태그의 조건이 모두 거짓인 경우 실행되는 블록입니다. <c:otherwise> 태그는 <c:choose> 블록의 마지막에 위치해야 합니다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>다중조건 처리 -choose, when, otherwise</title>
</head>
<body>
<h3>파라미터 보내기</h3>
<form action="05JSTL_result.jsp">
<select name="fruit">
<option value="1">사과</option>
<option value="2">멜론</option>
<option value="3">바나나</option>
</select>
<input type="submit" value="전송">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>오늘 먹을 과일은?</h3>
<c:choose>
<c:when test="${param.fruit ==1}">
<span style="color:red;">사과</span>
</c:when>
<c:when test="${param.fruit ==2}">
<span style="color:green;">멜론</span>
</c:when>
<c:otherwise>
<span style="color:yellow; background-color: black">바나나</span>
</c:otherwise>
</c:choose>
</body>
</html>

마지막 보스를 처리하자
- <c:forEach>
- var 속성을 사용하여 현재 반복 요소에 대한 변수를 설정하고, items 속성을 사용하여 반복할 컬렉션 또는 배열을 지정합니다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>foreach</title>
</head>
<body>
<%
String [] movieList ={"타이타닉","인타임","해리포터","분노의 질주"};
pageContext.setAttribute("ml", movieList);
%>
<c:forEach var="movie" items="${ml}">
${movie }<br>
</c:forEach>
</body>
</html>
- varStatus -> 반복문의 현재 상태에 대한 정보를 얻을 수 있습니다. varStatus 속성은 Status 객체를 참조할 변수를 지정합니다.
- ${status.index}: 현재 반복 요소의 인덱스 값을 출력합니다. 인덱스는 0부터 시작합니다.
- ${status.count}: 현재 반복이 몇 번째인지를 출력합니다. 카운트는 1부터 시작합니다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>foreach</title>
</head>
<body>
<%
String [] movieList ={"타이타닉","인타임","해리포터","분노의 질주"};
pageContext.setAttribute("ml", movieList);
%>
<table border="1" style="width:100%; text-align: center;">
<tr>
<th>index</th>
<th>count</th>
<th>title</th>
</tr>
<%--for(String movie : ml); --%>
<c:forEach var="movie" items="${ml}" varStatus="status">
<tr>
<td>${status.index }</td>
<td>${status.count }</td>
<td>${movie }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
- first last 사용하기
1) ${status.index ==1}
2) ${status.count ==2}
1,2둘 중 하나쓰자 둘다 같으니깐~
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>foreach</title>
</head>
<body>
<%
String [] movieList ={"타이타닉","인타임","해리포터","분노의 질주"};
pageContext.setAttribute("ml", movieList);
%>
<table border="1" style="width:300px; text-align: center;">
<tr>
<th>index</th>
<th>count</th>
<th>title</th>
</tr>
<%--for(String movie : ml); --%>
<c:forEach var="movie" items="${ml}" varStatus="status">
<tr>
<td>${status.index }</td>
<td>${status.count }</td>
<td>${movie }</td>
</tr>
</c:forEach>
</table>
<hr>
<h3>first,last 사용하기</h3>
<ul>
<c:forEach var="mv" items="${ml}" varStatus="status">
<c:choose>
<c:when test="${status.first }">
<li style="font-weight:bold; color: red;">${mv}</li>
</c:when>
<%-- ${status.index ==1} --%>
<c:when test="${status.count ==2}">
<li style="font-weight:bold; color: orange;">${mv}</li>
</c:when>
<c:when test="${status.last }">
<li style="font-weight:bold; color: blue;">${mv}</li>
</c:when>
<c:otherwise>
<li style="font-weight:bold; color: green;">${mv}</li>
</c:otherwise>
</c:choose>
</c:forEach>
</ul>
</body>
</html>
not status.last
'JSP > 이론' 카테고리의 다른 글
fmt:formatNumber(groupingUsed, pattern, percent ,type)//fmt:formatDate (0) | 2023.06.15 |
---|---|
JSTL/foreach/(begin, end)//다중파라미터(checkbox)//c:forTokens //c:redirect (0) | 2023.06.15 |
EL복습시간 + ${per.name} << name을 적으면 name의 getter를 가져온다 (0) | 2023.06.15 |
객체 bean사용 비슷한 것(property * /setattribute ) (0) | 2023.06.14 |
el로 넘기고 객체로 넘기고 // param으로 el값 가져오기 (0) | 2023.06.14 |