[국비학원 기록/JSP] JSTL - <c:url>, <c:redirect>,<c:out> 형식
my code archive
article thumbnail
반응형

<c:url> 형식

<c:url var="url이 저장된 변수 이름" value="url 경로" scope="">
  [<c:param name="매개변수 이름" value="전달값"/>]
  ....
</c:url>

 

url.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 
<c:set var="contextPath" value="${pageContext.request.contextPath }"/>
<%-- <c:url> 태그로 이동할 페이지 설정 --%>
<c:url var="url1" value="../jstl01/member01.jsp">
    <c:param name="id" value="ezen300"/>
    <c:param name="pwd" value="0311" />                    <%-- 이동할 페이지로 전달할 데이터 삽입 --%>
    <c:param name="name" value="이순신" />
    <c:param name="email" value="ezen300@gmail.com" />
</c:url>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>c:url 테스트</title>
</head>
<body>
    <%-- <a href="${contextPath}/jstl01/member1.jsp">회원 정보 출력</a> --%>
    <a href="${url1 }">회원 정보 출력</a>
</body>
</html>
cs

 

member01.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- core 태그 라이브러리를 사용하기 위해서 반드시 선언해야 한다. -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    request.setCharacterEncoding("utf-8");
%>
 
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>회원 정보 출력</title>
</head>
<body>
    <table border="1" align="center">
        <tr align="center" bgcolor="#99ccff">
            <td width="7%"><b>아이디</b></td>                <!-- EL로 변수에 바로 접근하여 값 출력 -->
            <td width="7%"><b>비밀번호</b></td>
            <td width="7%"><b>이름</b></td>
            <td width="7%"><b>이메일</b></td>
        </tr>
        <tr align="center">
            <td>${param.id }</td>
            <td>${param.pwd }</td>
            <td>${param.name }</td>
            <td>${param.email }</td>
        </tr>
    </table>
</body>
</html>
cs

 

실행 화면

url.jsp를 실행해서 <a> 태크 링크를 클릭하면

url 패턴에 적용된대로 member01.jsp 페이지로 이동한다.

 

 

<c:redirect> 형식

-response.sendRedirect() 기능을 대체함

<c:redirect url="redirect할 url">
 [<c:param name="매개변수 이름" value="전달값"/>]
 .....
</c:redirect>

 

url02.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ 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>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JSTL - url</title>
</head>
<body>
    <h4>url 태그로 링크 걸기</h4>
    <c:url value="../jstl01/inc/urlPage.jsp" var="url">
        <c:param name="userParam1" value="ezen500"/>
        <c:param name="userParam2">ezen900</c:param>
    </c:url>
    
    <a href="${url }">urlPage.jsp 바로가기</a>
</body>
</html>
cs

 

urlPage.jsp
1
2
3
4
5
6
7
8
9
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<h4>urlPage.jsp</h4>
<ul>
    <li>저장된 값 :</li>
    <li>매개변수 1 : ${param.userParam1 }</li>
    <li>매개변수 2 : ${param.userParam2 }</li>
 
cs

 

실행 화면

url02.jsp를 실행하면 urlPage.jsp로 리다이렉트.

 

<c:out>형식

 

-화면에 지정한 값을 출력하는 기능

<c:out value="출력할 값" default="기본값" escapeXml="boolean값"/>

 

out.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<%@ 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>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JSTL - out</title>
</head>
<body>
    <c:set var="iTag">
        i태그는 <i>기울기</i>를 표현합니다.
    </c:set>
    
    <h4>기본 사용</h4>
    <c:out value="${iTag}"/>
    
    <%-- escapeXml이 false일  변환되는 문자
        
        <        &lt;
        >        &gt;
        &        &amp;
        
        escapeXml 속성이 true이면 value의 값이 그대로 화면에 출력됨
        escapeXml 속성이 false이면 value의 값이 특수문자로 변환되어 화면에 출력됨
        
    --%>
    <h4>escapeXml 속성</h4>
    <c:out value="${iTag }" escapeXml="false"/>
    
    <h2>escapeXml 변환하기</h2>
    <h2>
        <pre>
            <c:out value="&lt;" escapeXml="true"/>
            <c:out value="&lt;" escapeXml="false"/>
            <c:out value="&gt;" escapeXml="true"/>
            <c:out value="&gt;" escapeXml="false"/>
            <c:out value="&amp;" escapeXml="true"/>
            <c:out value="&amp;" escapeXml="false"/>
        </pre>
    </h2>
    
    <h4>default 속성</h4>
    <c:out value="${param.name }" default="이름 없음"/>
    <c:out value="" default="빈 문자열도 값입니다."/>
</body>
</html>
cs

 

실행 화면

 

 escapeXml이 false일 때 변환되는 문자

< &lt;
> &gt;
& &amp;

escapeXml 속성이 true이면 value의 값이 그대로 화면에 출력됨
escapeXml 속성이 false이면 value의 값이 특수문자로 변환되어 화면에 출력됨

반응형
profile

my code archive

@얼레벌레 개발자👩‍💻

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

반응형