티스토리 뷰

FRONT-END/JSP

[JSP] JSTL

진심스테이크 2018. 5. 4. 20:19

 

EL (Expression Language) : 표현 언어

- 처음에는 JSTL의 부분으로 사용 되었으나 JSP2.0 부터는 기본 스펙에 포함

- 자바 빈즈 속성 값을 보다 쉽고 제약을 덜 받는 방법으로 사용하기 위해 나옴

 

 

기본 문법

- 표현 언어네는 "$"로 시작

- 모든 내용은 "{표현식}"과 같이 구성됨

- 표현식에서는 기본적으로 변수명 혹은 속성명, 메소드명 구조로 이루어짐

- 숫자, 문자열, boolean, null 도 올 수 있음

- 연산 가능

 

 

구성 요소

1. CORE : 공통 필수 기능

- for, switch 문등 자바 코드를 쓸 수 있게 해줌

2. XML : XML 문서 처리 관련 기능

3. L18N : 국제화 지원 관련 기능

4. SQL : JDBC를 이용한 DB처리 기능

 

 


 

 

태그

 

<c:out> : 출력

 

 

<c:set> : 입력

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=EUC-KR">
<title>JSTL</title>
</head>
<body bgcolor="#FFFFFF">
    <h3>&lt;c:set&gt;</h3>
    <c:set value="HELLO WORLD" var="msg" />
    msg:${msg}
    <br> msg:<%=pageContext.getAttribute("msg")%>
</body>
</html>

 

 

<c:remove> : 삭제

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <h3>&lt;c:remove&gt;</h3>
    <c:set value="batman" var="msg" />
    msg:
    <c:out value="${msg }" />
    <br>
    <c:remove var="msg" />
    after remove
    <c:out value="${msg }" />
</body>
</html>

 

 

<c:catch> : 예외처리

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <h3>&lt;c:catch&gt;</h3>
    <c:catch var="errMsg">
        <%=9 / 0%>
    </c:catch>
    error message:${errMsg }
</body>
</html>

 

 

<c:if> : if문

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <h3>&lt;c:if&gt;</h3>
    <c:set value="superman" var="msg" />
    msg:${msg}
    <br>
    <c:if test="${msg == 'superman' }" var="result" />
    test result : ${result }
</body>
</html>

 

 

<c:choose>, <c:when>, <c:otherwise> : 조건문

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <h3>&lt;c:choose&gt;</h3>
    <form>
        <select name="aaa">
            <option>-</option>
            <option>a</option>
            <option>b</option>
            <option>c</option>
            <option>d</option>
        </select> <input type="submit">
    </form>

    <c:choose>
        <c:when test="${param.sel=='a' }">a를 선택</c:when>
        <c:when test="${param.sel=='b' }">b를 선택</c:when>
        <c:when test="${param.sel=='c' }">c를 선택</c:when>
        <c:when test="${param.sel=='d' }">a,b,c 외를 선택</c:when>
        <c:otherwise>a,b,c,d를 선택하시오</c:otherwise>
    </c:choose>
</body>
</html>

 

 

<c:forEach> : 컬렉션의 객체의 크기만큼 반복

 

 

<c:forTokens> : 구별 단위

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <h3>&lt;c:forTokens&gt;</h3>
    <c:forTokens items="123-456-789" delims="-" var="sel">
${sel }<br>
    </c:forTokens>
</body>
</html>

 

 

<c:import> : 특정 URL 페이지를 현재 페이지에 포함

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <h3>&lt;c:import&gt;</h3>
    다음은 import를 이용해 포함한 것입니다
    <hr>

    <c:import url="set.jsp" var="myurl" />
    <c:out value="${myurl }" escapeXml="false" />
</body>
</html>

 

 

<c:uri> : URL 정보 다름

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <h3>&lt;c:url&gt;</h3>
    <c:url value="choose.jsp" var="target">
        <c:param name="sel">a</c:param>
    </c:url>

    <hr>
    단순 출력 : ${target }
    <br> 링크 연동 :
    <a href="${target }">choose.jsp-a선택</a>
</body>
</html>
 

<c:redirect> : response.sendRedirect( ), <jsp:forward> 대신 사용

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <h3>%lt;c:redirect&gt;</h3>
    <c:redirect url="choose.jsp">
        <c:param name="sel">a</c:param>
    </c:redirect>
</body>
</html>

 

 

<c:param> : 파라미터 값 설정

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <form action="EL02.jsp" method="post">
        <table border=1>
            <tr>
                <td>이름</td>
                <td><input type="text" name="name" value="superman"></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="입력"></td>
            </tr>
        </table>
    </form>
</body>
</html>

 

- EL02.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>${param.name}
</body>
</html>

 

 


 

 

ElTest.java

package el.test;

public class ElTest {
    private String[] productList = { "test1", "test2", "test3", "test4", "test5" };
    private int num1 = 30;
    private int num2 = 50;

    public String[] getProductList() {
        return productList;
    }

    public void setProductList(String[] productList) {
        this.productList = productList;
    }

    public int getNum1() {
        return num1;
    }

    public void setNum1(int num1) {
        this.num1 = num1;
    }

    public int getNum2() {
        return num2;
    }

    public void setNum2(int num2) {
        this.num2 = num2;
    }
}

 

 

ElTest.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <form name=from1 method="post" action="sel.jsp">
        <jsp:useBean id="eltest" class="el.test.ElTest" scope="session"></jsp:useBean>

        <select name="sel">
            <%
                for (String item : eltest.getProductList()) {
                    out.println("<option>" + item + "</option>");
                }
            %>
        </select> <br> <input type="submit" value="선택" />
    </form>
</body>
</html>

 

 

set.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    선택한 상품 : ${ param.sel}
    <br> num1 값 : ${eltest.num1 }
    <br> num2 값 : ${eltest.num2 }
    <br> num1 + num2 = ${eltest.num1 + eltest.num2 }
</body>
</html>

 

 

'FRONT-END > JSP' 카테고리의 다른 글

[JSP] JSTL <c:forEach>로 변경  (0) 2018.05.06
[JSP] MVC PATTERN  (0) 2018.05.02
[JSP] JAVABEAN  (0) 2018.04.16
[JSP] ACTION TAG - 액션 태그  (0) 2018.04.16
[JSP] 간단한 장바구니  (0) 2018.04.13
댓글