본문 바로가기
STUDY/JSP

Jsp 11일차 - JSTL(JSP Standard Tag Library)

by Anne of Green Galbes 2019. 2. 25.

1. EL(Expression Language) : 표현언어

○ 표현식(<%=..%>)을 대신하는 효과를 가진다.

○ null값을 가지는 변수에 대해 좀 더 관대하고 데이터형을 자동으로 변환해 준다.

○ ${ }

<%@ page contentType="text/html; charset=UTF-8"%>

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

request.setAttribute("result", "테스트");

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>EL</title>

</head>


<body>

<!-- action에 아무것도 적지 않으면 자기자신을 리로딩한다 -->

<form action="" method="post">

수1: <input type="text" name="su1"></br>

수2: <input type="text" name="su2"></br>

<input type="submit" value="결과"></br>

</form>

<%=request.getAttribute("result") %></br>

${result }</br>


<!-- su1의 값 받기 -->

su1: <%request.getAttribute("su1"); %></br>

su1:${param.su1 }</br>


결과 : ${param.su1 + param.su2 }</br>

${param.su1 }은 ${param.su1%2==0?"짝수":"홀수" }</br>

${10*20 }</br>

</body>

</html>

결과
○ 수1과 수2에 값을 입력하지 않은 상황에서는 ${param.su1 }의 값이 화면에 보이지 않는다

○ 수1과 수2 를 입력한 후 결과 버튼을 눌렀을 때


2. JSTL(JSP Standard Tag Library)

○ 사용자가 만든 태그를 커스텀 태그라고 하는데, 이 중에 자주 사용하는 태그를 표준으로 만들어 놓은 것

○ 태그를 사용자가 만들어 사용할 수 있다

○ 다운로드
lib > jstl.jar / standard.jar파일을 WEB-INF의 lib폴더에 복사
○ 처리영역 4가지
- core, format, xml, sql

○ JSTL을 불러오는 작업

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

이 작업을 해 줘야지 JSTL을 사용할 수 있다.

다이아몬드로 있는 것들이 JSTL


3. Core

<c:if test="조건문">

test5.jsp

◈ ${param.변수명 }

form의 변수를 사용할 경우에 쓴다

<%@ page contentType="text/html; charset=UTF-8"%>

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

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>구구단</title>

</head>


<body>

<form action="" method="post">

수 : <input type="text" name="su" /><br/>

<input type="submit" value="결과" /><br/>

</form>

<br/>


<c:if test="${!empty param.su }">

<c:if test="${param.su % 2 == 0 }">

${param.su }:짝수<br/>

</c:if>

<c:if test="${param.su % 2 ==1 }">

${param.su }:홀수<br/>

</c:if>

</c:if>

</body>


</html>

결과


② <c:forEach var="변수" begin="시작값" end="마지막값" step="증가값">

(예제 1)

gugudan.jsp

◈ 코드 해석
<c:forEach var="i" begin="2" end="9" step="1">
→ for(int i=2;i<=9;i++)
i는 2부터 9보다 크거나 같을때 까지 1씩 증가

<%@ page contentType="text/html; charset=UTF-8"%>

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

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>


<body>

<form action="" method="post">

단:<input type="text" name="dan" /><br/>

<input type="submit" value="결과" /><br/>

</form>

<br/>


<c:if test="${!empty param.dan }">

<table>

<tr>

<td align="center">**${param.dan }단**</td>

</tr>

<c:forEach var="i" begin="2" end="9" step="1">

<tr><td>${param.dan } × ${i } = ${param.dan*i }</td></tr>

</c:forEach>

</table>

</c:if>

</body>


</html>

결과


(예제 2) VO를 이용하한 <c:forEach>

DataVO.java

package com.svt;

public class DataVO {

private String name;

private int age;

public DataVO(String name, int age){

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

test6.jsp

○ VO에 값을 넣음

<%@page import="java.util.ArrayList"%>

<%@page import="com.svt.DataVO"%>

<%@page import="java.util.List"%>

<%@ page contentType="text/html; charset=UTF-8"%>

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

List<DataVO> lists = new ArrayList<DataVO>();

DataVO vo = new DataVO("배수지",25);

lists.add(vo);

vo = new DataVO("안상희",30);

lists.add(vo);

vo = new DataVO("이슬기",32);

lists.add(vo);

vo = new DataVO("천송이",40);

lists.add(vo);

request.setAttribute("lists", lists);

%>

<jsp:forward page="result.jsp" />

result.jsp

확장 for문도 사용 가능하다

<c:forEach var="vo" items="${lists }">

→for(DataVO vo : lists)

<%@ page contentType="text/html; charset=UTF-8"%>

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

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<table>

<tr>

<td width="100">이름</td>

<td width="100">나이</td>

</tr>

<c:forEach var="vo" items="${lists }">

<tr>

<td>${vo.name }</td>

<td>${vo.age }</td>

</tr>

</c:forEach>

</table>


</body>

</html>

결과


③ <c:set> : 변수 초기화
test5.jsp
◈ 코드해석
<c:set var="result" value="1" />
→ result라는 변수의 초기값을 1로 설정

<c:forEach var="a" begin="1" end="${param.su2 }" step="1">

<c:set var="result" value="${result * param.su1 }" />

${param.su1 }^${a} = ${result }<br/>

</c:forEach>

a가 su2보다 작을때까지

result = result*su1

<%@ page contentType="text/html; charset=UTF-8"%>

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

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title></title>

</head>


<body>


<form action="" method="post">

수1:<input type="text" name="su1"><br/>

수2:<input type="text" name="su2"><br/>

<input type="submit" value="결과"><br/>

</form>

<br/><br/>

<c:if test="${!empty param.su1 }">

<c:set var="result" value="1" /> <!-- 변수 초기화 -->

<c:forEach var="a" begin="1" end="${param.su2 }" step="1">

<c:set var="result" value="${result * param.su1 }" />

${param.su1 }^${a} = ${result }<br/>

</c:forEach>

</c:if>


</body>


</html>

결과


<c:choose> : switch-case문
test5.jsp
<c:choose>
<c:when test="조건1">
조건1이 참일때 실행
</c:when>

<c:when test="조건2">
조건2가 참일때 실행
</c:when>

<c:otherwise>
조건1, 조거2 둘 다 만족하지 않을 때 실행
</c:otherwise>
</c:choose>

<%@ page contentType="text/html; charset=UTF-8"%>

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

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title></title>

</head>


<body>

<form action="" method="post">

수 : <input type="text" name="su" /><br/>

<input type="submit" value="결과" /><br/>

</form>

<c:if test="${!empty param.su }">

<c:choose>

<c:when test="${param.su%3==0 && param.su%4==0 }">

${param.su }는 3과 4의 배수<br/>

</c:when>

<c:when test="${param.su%4==0 }">

${param.su }는 4의 배수<br/>

</c:when>

<c:when test="${param.su%3==0 }">

${param.su }는 3의 배수<br/>

</c:when>

<c:otherwise>

${param.su }는 3과 4의 배수가 아니다.<br/>

</c:otherwise>

</c:choose>

</c:if>

</body>


</html>

결과


⑤ <c:import>

<%@ page contentType="text/html; charset=UTF-8"%>

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

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>


<body>

<c:set var="url" value="gugudan.jsp"/>

<c:import url="${url }" var="uu">

<c:param name="dan" value="5" />

</c:import>

</body>


</html>

결과
value값을 5로 줬기 때문에 단에 다른 숫자를 넣어도 무조건 5단만 화면에 출력된다


◈ 실제로 출력은 <c:out>이 해준다

escapeXml="false" 이 없으면 코딩이 화면에 출력된다


'STUDY > JSP' 카테고리의 다른 글

게시판(서블릿) - BoardServlet.jave  (0) 2019.02.26
Jsp 11일차 - DBCP  (0) 2019.02.25
Jsp 11일차 - 서블릿(Servlet)  (0) 2019.02.25
Jsp 8일차 - 게시판(자바스크립트)  (0) 2019.02.21
Jsp 8일차 - 게시판(css작성)  (0) 2019.02.21

댓글