Servlet / JSP #16 View & Tag

underlier12·2020년 1월 30일
0

SERVLET&JSP

목록 보기
16/16

16. View & Tag

View 페이지 은닉

Control 단과 View 단을 분리하고도 View 단에서 실행하면 NullPointerException이 발생한다. 하지만 사용자가 요청할 수 없는 상태로 View 페이지를 은닉하여야 바람직하겠다. (즉, WEB-INF 폴더 내에 View 페이지들 혹은 View 페이지들이 모여 있는 폴더들을 옮긴다)

View 흐름 제어

View에서 흐름 제어를 위한 자바 코드 블록을 제거하는 작업을 진행한다. 이 때 jstl이라는 라이브러리를 받아야 하며 아래와 같은 링크에서 받을 수 있다.

그럼 아래와 같은 Action Tag를 사용할 수 있게 되어 jsp 내 반복문을 위한 자바 코드를 제거할 수 있다. (<forEach> </forEach>)

흐름 제어 코드 대체

위의 설명과 같이 jsp 내 MVC 패턴을 맞추고자 반복문 자바코드를 태그로 치환한다. 이 때 지시어를 추가하는데 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>라는 내용이며 이 때 uri 내용이 틀리지 않도록 유의한다.

list.jsp

<%@page import="java.util.List"%>
<%@page import="com.newlecture.web.entity.Notice"%>
<%@ 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>
    <title>코딩 전문가를 만들기 위한 온라인 강의 시스템</title>
    <meta charset="UTF-8">
    <title>공지사항목록</title>
    
    <link href="/css/customer/layout.css" type="text/css" rel="stylesheet" />
    <style>
    
        #visual .content-container{	
            height:inherit;
            display:flex; 
            align-items: center;
            
            background: url("../../images/customer/visual.png") no-repeat center;
        }
    </style>
</head>

... 중략
			
			<div class="notice margin-top">
				<h3 class="hidden">공지사항 목록</h3>
				<table class="table">
					<thead>
						<tr>
							<th class="w60">번호</th>
							<th class="expand">제목</th>
							<th class="w100">작성자</th>
							<th class="w100">작성일</th>
							<th class="w60">조회수</th>
						</tr>
					</thead>
					<tbody>
					
					<c:forEach var="n" items="${list }">
					
					<tr>
						<td>${n.id }</td>
						<td class="title indent text-align-left"><a href="detail?id=${n.id }">${n.title }</a></td>
						<td>${n.writerId }</td>
						<td>${n.regdate }</td>
						<td>${n.hit }</td>
					</tr>
					
					</c:forEach>
										
					</tbody>
				</table>
			</div>
			
    ... 후략

저장소(pageContext 등)에 저장된 값만 EL에서 사용할 수 있다

JSTL

JSTL(JSP Standard Tag Library)는 다음과 같이 5가지 기능을 포함하고 있으나 SQL, XML의 경우 현재에는 View 단의 구성을 해칠 수 있기에 배제하고 Core, Formating, Functions를 사용한다.

그 종류는 다음과 같다.

이전에 지시어를 포함시켰는데 그 이유는 만약 그대로 사용한다면 jsp를 servlet으로 변환시키는 Jasper가 분간할 수가 없다. 따라서 접두사를 두어 이는 해당 uri에서 제공하는 태그임을 명시해주어 원하는 기능대로 구동시킬 수 있게 하는 것이다.

Tag를 직접 만들어 사용한다면 TagSupport 클래스를 상속받아 만들 수 있으며 이 때 Tag Descriptor를 작성하고 파일 확장자를 {파일명}.tld로 해야 한다. (tag library descriptor) 이 때 반드시 uri가 포함되어야 하며 그 이유는 다른 Tag이지만 같은 이름이더라도 uri가 다르면 달리 식별할 수 있기 때문이다.

그리고 TagSupport를 상속받은 뒤 doStartTag(), doEndTag(), doAfterBody() 를 오버라이드하면 된다.

그래서 ForTag는 Jasper가 uri를 인식하고 해당 클래스로 보내 지정된 동작을 하도록 돕는다.

중간 정리

profile
logos and alogos

0개의 댓글