1. JSTL์ด๋?
์ผ๋ฐ์ ์ผ๋ก ์๊ณ ์๋ JSTL์ด๋ JSTL + EL์ ์กฐํฉ์ ๋งํ๋ค.
HTML ์ฝ๋ ๋ด์ java ์ฝ๋์ธ ์คํฌ๋ฆฝํ๋ฆฟ <%= student %>๋ฅผ ${student}๋ก, <%=if %>๋ฌธ์ <c:if>, <%=for%>๋ฌธ์ <c:forEach>๋ก ๋์ฒดํ์ฌ ์ฌ์ฉํ๋ค.
01set.jsp
<%@page import="com.study.login.vo.UserVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<%request.setCharacterEncoding("UTF-8"); %>
<%@include file="/WEB-INF/inc/header.jsp" %>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@include file="/WEB-INF/inc/top.jsp" %>
<!-- el ๋ณ์ ์ ์ธ -->
<%
UserVO user = new UserVO("gks", "ํ", "1234", "๋ฐฅ");
//user ์ฃผ์๊ฐ
%>
<c:set var="aa" value="<%=user %>" scope="request"></c:set>
${aa }
<br>
<c:set var="b" value="${aa }"></c:set>
${b }
<!--var, value="" ์ ๋ค์ด๊ฐ๋ ๊ฐ ๊ณต๋ฐฑ ์ฃผ์ -->
</body>
</html>
set์ ํ์ฉํด์ jsp์์ ์ฌ์ฉํ ๋ณ์๋ฅผ ์ค์ ํ ์ ์๋ค. ์ด์ ๊ฐ์ ํ๊ทธ๋ฅผ ์ฝ์ดํ๊ทธ๋ผ๊ณ ํ๋ ๋ฐ, ์ฝ์ดํ๊ทธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๋ณ์ ์ค์ ์ด๋ if-else์ ๊ฐ์ ๋ ผ๋ฆฌ ์ฒ๋ฆฌ์ ์ฌ์ฉ๋๋ ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ๋์ฒดํ๋ ํ๊ทธ๋ฅผ ์ ๊ณตํ๋ค.
๋ํ ์ฝ์ด ํ๊ทธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด jspํ์ด์ง์ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>๋ฅผ ์ถ๊ฐํด์ผํ๋ค.(๋ฌผ๋ก jstl๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ฐ๋ก ๋ฐ์์ผํ๋ค.)
02input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%request.setCharacterEncoding("UTF-8"); %>
<%@include file="/WEB-INF/inc/header.jsp" %>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@include file="/WEB-INF/inc/top.jsp" %>
<form action="02if.jsp">
<input type="number" name="score">
<input type="submit" value="์ฑ์ ">
</form>
</body>
</html>
02if.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<%request.setCharacterEncoding("UTF-8"); %>
<%@include file="/WEB-INF/inc/header.jsp" %>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@include file="/WEB-INF/inc/top.jsp" %>
<%
int score =Integer.parseInt(request.getParameter("score"));
//90์ด์ ์, 80์ด์ ์ฐ, .......
%>
<c:if test="<%=score>=90 %>"> <h1>์</h1> </c:if> <!-- test์์ ๊ฐ์ด true false -->
<c:if test="<%=score>=80 && score<90 %>"> <h1>์ฐ</h1> </c:if>
<c:if test="<%=score>=70 && score<80%>"> <h1>๋ฏธ</h1> </c:if>
<c:if test="<%=score<70%>"> <h1>์</h1> </c:if>
<!-- c:if๋ else๊ฐ ์๋ค. -->
<hr>
<!-- switch : choose, case : when, default : otherwise -->
<c:choose>
<c:when test="<%=score>=90 %>"></c:when>
<c:when test="<%=score>=80 && score<90 %>"> <h1>์ฐ</h1> </c:when>
<c:when test="<%=score>=70 && score<80%>"> <h1>๋ฏธ</h1> </c:when>
<c:otherwise> <h1>์</h1> </c:otherwise>
</c:choose>
</body>
</html>
<c:if test=""></c:if> ์์ test=""์์ ์กฐ๊ฑด์์ ์์ฑํ๋ฉด ๋๋ค. ๊ทธ์์ ์กฐ๊ฑด์์ ๊ฒฐ๊ณผ(true, false)์ ๋ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ์ด ์ ํด์ง๋ค.
๋ํ c:if์๋ else๊ฐ ๋ฐ๋ก ์กด์ฌํ์ง ์๋๋ค.
switch๋ฌธ์ ์๋ฐ์ ์ฌ์ฉ๋ฒ์ด ๋ค๋ฅธ๋ฐ switch๋ choose๋ก case๋ when์ผ๋ก default๋ otherwise๋ก ์ฌ์ฉํ๋ค.
03for.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<%request.setCharacterEncoding("UTF-8"); %>
<%@include file="/WEB-INF/inc/header.jsp" %>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@include file="/WEB-INF/inc/top.jsp" %>
<c:forEach begin="1" end="10" var="i" step="2" varStatus="st">
${i } ๋ฒ์งธ <br>
index : ${st.index } <br>
count : ${st["count"] } <br>
first : ${st.first } <br>
last : ${st.last } <br>
<hr>
</c:forEach>
</body>
</html>
var์ ์๋ ๋ฌธ์๋ก c:forEachํ๊ทธ์์์ el์ฌ์ฉ
c:forEach์์ฑ : begin, end, var, step, item, varStatus
varStatus์์ฑ :
- index(๋ฃจํ ์คํ์์ ํ์ฌ ์ธ๋ฑ์ค)
- count(๋ฃจํ ์คํ ํ์)
- first(ํ์ฌ ์คํ์ด ์ฒซ ๋ฒ์งธ ์คํ์ธ ๊ฒฝ์ฐ true)
- last(ํ์ฌ ์คํ์ด ๋ฃจํ์ ๋ง์ง๋ง ์คํ์ธ ๊ฒฝ์ฐ true)
- current(์ปฌ๋ ์ ์ค ํ์ฌ ๋ฃจํ์์ ์ฌ์ฉํ ๊ฐ์ฒด)
- begin(begin์์ฑ๊ฐ)
- end(end์์ฑ๊ฐ)
- step(step์์ฑ๊ฐ)
'๐ Today I Learned(๊ฐ๋ฐ์ธ์ดํ์ต) > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Day07(el)array, collection (0) | 2021.10.13 |
---|---|
Day07(el)๊ตฌ๊ตฌ๋จ, ๋ณ์ฐ๊ธฐ (0) | 2021.10.13 |
Day07(el)lambda (0) | 2021.10.13 |
Day07(el)method (0) | 2021.10.13 |
Day07(el)์ธ๋ฏธ์ฝ๋ก ์ฐ์ฐ์ (0) | 2021.10.13 |