[포스코DX|JSTL|6주] 25일차 수업
포스코DX X 비트교육센터 6기
JSP Template
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ page language="java" contentType="text/html; charset=${encoding}"
pageEncoding="${encoding}"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="${encoding}">
<title>Insert title here</title>
</head>
<body>
${cursor}
</body>
</html>
jsp : if문
jstl : if문
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ 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>
<!-- ?color=red -->
<c:choose>
<c:when test='${param.c == "red"}'><h1 style="color:red">Hello World</h1></c:when>
<c:when test='${param.c == "blue"}'><h1 style="color:blue">Hello World</h1></c:when>
<c:when test='${param.c == "green"}'><h1 style="color:green">Hello World</h1></c:when>
<c:otherwise><h1>Hello World</h1></c:otherwise>
</c:choose>
</body>
</html>
jsp : for문
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String row = request.getParameter("r");
String col = request.getParameter("c");
int nRow = Integer.parseInt(row);
int nCol = Integer.parseInt(col);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="10">
<%
for(int i = 0; i< nRow; i++){
%>
<tr>
<%
for(int j=0; j< nCol; j++){
%>
<td>Cell(<%=i %>,<%=j %>)
</td>
<% } %>
</tr>
<%
}%>
</table>
</body>
</html>
jstl : for문
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ 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>
<table border="1" cellspacing="0" cellpadding="10">
<c:forEach begin="0" end="${param.r }" step="1" var="i">
<tr>
<c:forEach begin="0" end="${param.c }" step="1" var="j">
<td>Cell(${i-1 },${j-1 })</td>
</c:forEach>
</tr>
</c:forEach>
</table>
</body>
</html>
값을 사용자가 안넣었을 때, set으로 처리해줄 수 있다.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ 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>
<c:set var="row" value="${param.r }" />
<c:set var="col" value="${param.c }" />
<c:if test="${empty row }">
<c:set car="row" value="3" />
</c:if>
<c:if test="${empty col }">
<c:set car="col" value="3" />
</c:if>
<table border="1" cellspacing="0" cellpadding="10">
<c:forEach begin="0" end="${row -1 }" step="1" var="i">
<tr>
<c:forEach begin="0" end="${col -1}" step="1" var="j">
<td>Cell(${i },${j })</td>
</c:forEach>
</tr>
</c:forEach>
</table>
</body>
</html>
(+) 응용
<c:set var="count" value="${fn:length(list) }" />
<c:forEach items="${list }" var="vo" varStatus="status">
<li>
<table>
<tr>
<td>${count - status.index }</td>
– user와 board는 비식별관계로 1대 다
create table user(
no int not null auto_increment,
name varchar(45) not null,
email varchar(200) not null,
password varchar(64) not null,
gender enum('female', 'male') not null,
join_date date not null,
primary key(no)
);
create table board(
no INT not null auto_increment,
title varchar(200) not null,
contents TEXT not null,
hit INT not null, -- views 숫자
reg_date datetime not null,
g_no int not null,
o_no int not null,
depth int not null,
primary key(no)
);