//액션 태그, Action Tags
- JSP 페이지 내에서 어떤 동작을 하도록 지시하는 태그
a. 기본 액션 태그
b. 확장 액션 태그
c. 사용자 정의 액션 태그
// 기본 액션 태그
1. forward
- 현재 페이지에서 다른 특정 페이지로 전환할 때 사용한다.
- URL은 현재 페이지가 유지된다.
<%@ 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>
<h1>main.jsp 페이지입니다.</h1>
<jsp:forward page="sub.jsp"/>
</body>
</html>
<%@ 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>
<h1>sub.jsp 페이지입니다.</h1>
</body>
</html>
2. include
- 현재 페이지에 다른 페이지를 삽입할 때 사용한다.
<%@ 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>
<h1>include1.jsp 페이지입니다.</h1>
<jsp:include page="include2.jsp" flush="true"/>
<h1>다시 include1.jsp 페이지입니다.</h1>
</body>
</html>
<%@ 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>
<h1>include2.jsp 페이지입니다.</h1>
</body>
</html>
3. param
- forward 및 include 태그에 데이터 전달을 목적으로 사용되는 태그
- name과 value로 이루어져 있다.
<%@ 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>
<jsp:forward page="param_forward2.jsp">
<jsp:param name="id" value="sorrel012"/>
<jsp:param name="pw" value="1234"/>
</jsp:forward>
</body>
</html>
<%@ 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>
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
%>
<h1>param_forward2.jsp입니다.</h1>
아이디: <%= id %><br>
비밀번호: <%= pw %><br>
</body>
</html>
// 확장 액션 태그: JSTL, JSP Standard Tag Library
- 자바 서버페이지 표준 태그 라이브러리
- 프로그래밍 기능이 있는 태그 모음
- 별도로 다운로드 받아야 한다.(https://mvnrepository.com/)
- 태그라이브러리를 먼저 불러와야 한다.<%@ taglib @>
lib | URI | Prefix | ex |
Core | http://java.sun.com/jsp/jstl/core | c | <c:tag |
XML Processing | http://java.sun.com/jsp/jstl/xml | x | <x:tag |
I18N formatting | http://java.sun.com/jsp/jstl/fmt | fmt | <fmt:tag |
SQL | http://java.sun.com/jsp/jstl/sql | sql | <sql:tag |
Functions | http://java.sun.com/jsp/jstl/functions | fn | fn:function() |
// Core : 기본적인 라이브러리로 출력, 제어문, 반복문 같은 기능이 포함되어 있다.
- 출력 태그: <c:out>
<c:out value="출력값" default="기본값" escapeXml="true or false"/>
- 변수 설정 태그 : <c:set>
<c:set var="변수명" value="설정값" target="객체" property="값" scope="범위"/>
- 변수 제거 태그 : <c:remove>
<c:remove var="변수명" scope="범위"/>
- 예외 처리 태그 : <c:catch>
<c:catch var="변수명">
- 제어문(if) 태그: <c:if>
<c:if test="조건" var="조건처리 변수명" scope="범위">
- 제어문(switch) 태그 : <c:choose>
<c:choose>
<c:when test="조건">처리 내용</c:when>
<c:otherwise>처리 내용</c:otherwise>
</c:choose>
- 반복(for) 태그 : <c:forEach>
<c:forEach items="객체명" begin="시작인덱스" end="끝인덱스" step="증감식"
var="변수명" varStatus="상태변수">
- 페이지 이동 태그 : <c:redirect>
<c:redirect url="url">
- 파라미터 전달 태그 : <c:param>
<c:param name="파라미터명" value="값">
package com.test.mvc;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/address.do")
public class Address extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//DB 작업 > select > 주소록 몇명?
//DB 작업 담당자 > "AddressDAO"
AddressDAO dao = new AddressDAO();
int count = dao.getCount();
int[] nums = { 10, 20, 30, 40, 50 };
List<Integer> nums2 = new ArrayList<Integer>();
nums2.add(100);
nums2.add(200);
nums2.add(300);
Map<String, Integer> nums3 = new HashMap<String, Integer>();
nums3.put("kor", 100);
nums3.put("eng", 90);
nums3.put("math", 80);
AddressVO vo = new AddressVO();
vo.setSeq("1");
vo.setName("홍길동");
vo.setAge("20");
vo.setTel("010-1234-5678");
vo.setAddress("서울시 강남구 역삼동");
String[] names = {"홍길동", "아무개", "하하하", "호호호", "후후후"};
Calendar birthday = Calendar.getInstance();
birthday.set(1997, 12, 17);
req.setAttribute("count", count);
req.setAttribute("nums", nums);
req.setAttribute("nums2", nums2);
req.setAttribute("nums3", nums3);
req.setAttribute("vo", vo);
req.setAttribute("a", 10);
req.setAttribute("b", 3);
req.setAttribute("names", names);
//CSV
req.setAttribute("colors", "빨강,노랑,파랑:검정,초록");
req.setAttribute("birthday", birthday);
RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/mvc/address.jsp");
dispatcher.forward(req, resp);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<h1>Address</h1>
<div>
<div>주소록 총 인원수: <%= request.getAttribute("count") %></div>
<div>주소록 총 인원수: ${count}</div>
<div>주소록 총 인원수: <%= (int)request.getAttribute("count") *2 %></div>
<div>주소록 총 인원수: ${count*2}</div>
<hr>
<!-- 리터럴 표현 -->
<div>${100}</div>
<div>${3.14}</div>
<div>${"홍길동"}</div>
<div>${'홍길동'}</div>
<div>${true}</div>
<div>${null}</div>
<hr>
<!-- 배열 -->
<div>${ nums }</div>
<div>${ nums[0] }</div>
<hr>
<!-- List -->
<div>${ nums2 }</div>
<div>${ nums2[0] }</div>
<hr>
<!-- Map -->
<div>${ nums3 }</div>
<div>${ nums3["math"] }</div>
<div>${ nums3.get("kor") }</div>
<div>${ nums3.eng }</div>
<hr>
<!-- Object -->
<div>${ vo }</div>
<div>${ vo.seq }</div>
<div>${ vo.getName() }</div>
<div>${ vo["name"] }</div>
<div>${ vo.name }</div>
<div>${ vo.age }</div>
<div>${ vo.tel }</div>
<div>${ vo.address }</div>
<hr>
<!-- 연산자 -->
<div>a: ${a}</div>
<div>b: ${b}</div>
<div>a + b: <%= (int)request.getAttribute("a") + (int)request.getAttribute("b") %></div>
<div>a + b: ${a + b}</div>
<div>a - b: ${a - b}</div>
<div>a * b: ${a * b}</div>
<div>a / b: ${a / b}</div>
<div>a div b: ${a div b}</div>
<div>a % b: ${a % b}</div>
<div>a mod b: ${a mod b}</div>
<hr>
<div>${a > b}</div>
<div>${a gt b}</div>
<div>${a >= b}</div>
<div>${a ge b}</div>
<div>${a < b}</div>
<div>${a lt b}</div>
<div>${a <= b}</div>
<div>${a le b}</div>
<div>${a == b}</div>
<div>${a eq b}</div>
<div>${a != b}</div>
<div>${a ne b}</div>
<hr>
<div>${a > 10 && b < 5 }</div>
<div>${a > 10 and b < 5 }</div>
<div>${a > 10 || b < 5 }</div>
<div>${a > 10 or b < 5 }</div>
<div>${!(a > 10)}</div>
<div>${not(a > 10)}</div>
<hr>
<div>${a > 0 ? "양수" : "음수" }</div>
<hr>
<%
//pageContext > request > session > application
pageContext.setAttribute("age", 20);
request.setAttribute("age", 30);
session.setAttribute("age", 40);
application.setAttribute("age", 50);
%>
<div>나이: <%= pageContext.getAttribute("age") %></div>
<div>나이: <%= request.getAttribute("age") %></div>
<hr>
<div>나이: ${pageScope.age}</div>
<div>나이: ${requestScope.age}</div>
<div>나이: ${sessionScope.age}</div>
<div>나이: ${applicationScope.age}</div>
<!--
JSTL, JSP Standard Tag Library
-->
<!-- 값 출력 명령 -->
<c:out value="안녕하세요"></c:out>
<c:out value="${count}"></c:out>
${count}
<c:out value="${count2}">값이 없음</c:out>
${count2}
<hr>
<!-- 변수 생성(pageContext 내장 변수) -->
<c:set var="n1" value="100"></c:set>
<%--
<c:set var="n1" value="100" scope="request"></c:set>
<c:set var="n1" value="100" scope="session"></c:set>
<c:set var="n1" value="100" scope="application"></c:set>
--%>
${n1}
<div>${pageScope.n1}</div>
<div>${requestScope.n1}</div>
<div>${sessionScope.n1}</div>
<div>${applicationScope.n1}</div>
<!-- 변수 수정 -->
<c:set var="age" value="11"></c:set>
<c:set var="age" value="22"></c:set>
<div>${age}</div>
<!-- 변수 삭제 -->
<c:remove var="age"></c:remove>
<div>age: ${empty age}</div>
<hr>
<!-- <c:set var="num" value="10" /> -->
<c:set var="num" value="-10" />
<!-- 조건문(if) -->
<c:if test="${num > 0}">
<div>${num}은 양수입니다.</div>
</c:if>
<c:if test="${num <= 0}">
<div>${num}은 양수가 아닙니다.</div>
</c:if>
<hr>
<!-- 조건문(choose - when) -->
<c:choose>
<c:when test="${num > 0 }">양수</c:when>
<c:when test="${num < 0 }">음수</c:when>
<c:when test="${num == 0 }">제로</c:when>
<c:otherwise>기본값</c:otherwise>
</c:choose>
<hr>
<!-- 반복문 -->
<!-- for(String name : names) -->
<c:forEach items="${names}" var="name"> <!-- itemts랑 var 순서 바뀌어도 된다. -->
<div>${name }</div>
</c:forEach>
<hr>
<c:forEach var="i" begin="1" end="5" step="1"> <!-- step 음수 불가 -->
<div>${i}</div>
</c:forEach>
<hr>
<c:forEach items="${names}" var="name" begin="2" end="4">
<div>${name }</div>
</c:forEach>
<hr>
<c:forEach items="${names}" var="name" varStatus="status">
<div>
${status.index}.
${status.count}.
${status.first}.
${status.last}.
${name}
</div>
</c:forEach>
<hr>
<c:forEach items="${colors}" var="name">
<div>${name}</div>
</c:forEach>
<hr>
<c:forTokens items="${colors}" delims=":" var="name">
<div>${name}</div>
</c:forTokens>
<hr>
<c:url var="link" value="http://localhost:8090/view.do">
<c:param name="name" value="hong"></c:param>
<c:param name="age" value="20"></c:param>
<c:param name="address" value="서울"></c:param>
</c:url>
<a href="${link}">링크</a>
<!-- resonse.sendRediruct(URL)로 변환 -->
<hr>
<div>내 생일: <fmt:formatDate value="${birthday.time}" pattern="yyyy-MM-dd E a HH:mm:ss"/></div>
</div>
<br><br><br><br><br><br>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
'서버 > Servlet-JSP' 카테고리의 다른 글
JSP 내장 객체 - Session, Application (0) | 2023.01.13 |
---|---|
JSP 내장 객체 - request, response, pageContext (0) | 2023.01.03 |
JSP 구성 요소 - JSP 지시자 (0) | 2023.01.01 |
JSP 구성 요소 - 스크립트 (0) | 2023.01.01 |
Servlet Parameter (0) | 2022.12.30 |