새소식

java/study

JSP 태그

  • -

커스텀액션

커스텀 액션을 만드는 방법

.tag

  1. @tag:@page 비슷 ,정의 속성도 매우비슷
  1. body-content
    1. 단일 태그<태그명/>:empty
    1. 내용물이 있는 태그 <태그명>~</태그명>
    1. scriptless:스크립틀릿 태그를 제외한(자바코드)
      1. el식,html사용
      1. 을써야지만 el식 인식
        1. <%@tag body-content="scriptless" pageEncoding="UTF-8"%>
    1. tagdependent
      1. 스크립틀릿,el식 제외한 내용
      1. <%@tag body-content="tagedependent" pageEncoding="UTF-8"%>
    1. trimDirectiveWhitespaces:공백제거
      1. <%@ tag trimDirectiveWhitespaces="true"%>
    1. <jsp:dobody/>:태그 안쪽에 입력된 컨텐츠
      <%@page contentType="text/html; charset=utf-8"%><--스크립트형식은 안됀다--> <%@ taglib prefix="util" tagdir="/WEB-INF/tags"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <c:set var="num" value="100"/> <util:box> <h1>안녕하세요</h1> ${num} <c:forEach var="i"begin="1"end="10"> ${i}<br> </c:forEach> </util:box>
      <%@tag body-content="scriptless" pageEncoding="UTF-8"%> <%--@tag body-content="tagedependent" pageEncoding="UTF-8"--%> <%@ tag trimDirectiveWhitespaces="true"%> <div style="border:1px dashed blue; padding:10px;"> <jsp:doBody/> </div>
    1. rtexprvalue-true: el식 변수 허용(기본값)

      ㄴ false:el식 변수 허용 안함

      ㄴ 직접입력 정수,문자

      <%@page contentType="text/html; charset=utf-8"%><--스크립트형식은 안됀다--> <%@ taglib prefix="util" tagdir="/WEB-INF/tags"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <c:set var="num1" value="100"/> <c:set var="num2" value="200"/> <util:max num1="${num1}"num2="${num2}"/> <util:max num1="100" num2="100"/>
      <%@ tag body-content="empty" pageEncoding="UTF-8"%> <%@ tag trimDirectiveWhitespaces="true"%> <%@attribute name="num1"type="java.lang.Integer"required="true"rtexprvalue="false"%> <%@attribute name="num2"type="java.lang.Integer"required="true"rtexprvalue="True"%>
    <%@tag body-content="empty" pageEncoding="UTF-8"%><%--한글 인코딩--%> <%@attribute name="size"><----> 자바 변수 : <%=size%><br> EL식 속성:${size}<br> -------------------------------<br> 한글
    <%@tag body-content="empty" pageEncoding="UTF-8"%><%--한글 인코딩--%> <%@taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%><%--태그라이브러리--%> <%@attribute name="size"type="java.lang.Integer"%><----> <div> <% <c:forEach begin="1"end="${size}"> - </c:forEach> %> </div>

@inlcude

@attribute

  1. name=”attributename”
  1. attrubute=java변수,el식 attributename
  1. type
    1. java.lang.String:기본값
    1. java.lang.integer:정수(기본형x,래퍼클래스)
      <%@tag body-content="empty" pageEncoding="UTF-8"%><%--한글 인코딩--%> <%@attribute name="size"type="java.lang.Integer"%><%--자바 라이브러리가져오기?--%> <div> <% for(int i=0;i<size;i++)<--사이즈 보다 작을때까지--> { out.print("-"); } %> </div>
    1. java.lang.long
    1. java.lang.double:실수

required

  1. false:필수항목x(defult)
  1. true:필수항목()
    1. 이속성을 입력하지 않으면 오류가 발생한다
    1. 번역단계에서 오류발생
    <%@tag body-content="empty" pageEncoding="UTF-8"%><%--한글 인코딩--%> <%@taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%> <%@attribute name="color" required="true"%><%--필수항목--%> <%@attribute name="size"type="java.lang.Integer"%><%----%> <%@attribute name="color"%> <div style="color:${color}"}> <% <c:forEach begin="1"end="${size}"> - </c:forEach> %> </div>

dynamic-attributes

  1. 속성명을 지정하지 않고사용
    1. map형태
    1. EL식 →마침표(.),[]
    1. 모든 속성의 자료형이string
    1. 자바코드(map)
      1. 속성값을 가져와야 가능
        1. pageContext→jspContext
    1. required속성 설정불가
<%@page contentType="text/html; charset=utf-8"%> <%@taglib prefix="util"tagdir="/WEB-INF/tags"%> <util:line size="30"color="orange"/>

<%@tag body-content="empty" pageEncoding="UTF-8"%> <%@tag trimDirectiveWhitespaces="true"%> <%@tag dynamic-attributes="attrs"%> ${attrs.color}<br> ${attrs.size}
<%@page contentType="text/html;charset=utf-8"%> <%@taglib prefix="util"tagdir="/WEB-INF/tags"%> <util:line2 size="30" color="orange"/>

단점 : 필수항목을 체크하지못한다

<%@page contentType="text/html; charset=utf-8"%><--스크립트형식은 안됀다--> <%@ taglib prefix="util" tagdir="/WEB-INF/tags"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <c:set var="num" value="100"/> <util:box> <h1>안녕하세요</h1> ${num} <c:forEach var="i"begin="1"end="10"> ${i}<br> </c:forEach> </util:box>
<%@tag body-content="scriptless" pageEncoding="UTF-8"%> <%@ tag trimDirectiveWhitespaces="true"%> <div style="border:1px dashed blue; padding:10px;"> <jsp:doBody/> </div>

@variable

  1. name-given:변수로 사용할 속성명
  1. variable-class=”자료형”:기본값은 java.lang.String
  1. scope
    1. AT_BRGIN:태그 위쪽
    1. AT_END:태그 닫힌 바로 아래쪽
    1. NESTED: 태그 안쪽
      <%@ tag body-content="empty" pageEncoding="UTF-8"%> <%@ tag trimDirectiveWhitespaces="true"%> <%@attribute name="num1"type="java.lang.Integer"required="true"rtexprvalue="false"%> <%@attribute name="num2"type="java.lang.Integer"required="true"rtexprvalue="True"%> <%@ variable name-given="max" variable-class="java.lang.Integer" scope="AT_END"%> <% int max = num1; if(max<num2)max=num2; jspContext.setAttribute("max",max); %>
      <%@page contentType="text/html; charset=utf-8"%><--스크립트형식은 안됀다--> <%@ taglib prefix="util" tagdir="/WEB-INF/tags"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%-- <c:set var="num1" value="100"/> <c:set var="num2" value="200"/> <util:max num1="${num1}"num2="${num2}"/> --%> <util:max num1="100" num2="100"/> ${max}

      <%@ tag body-content="empty" pageEncoding="UTF-8"%> <%@ tag body-content="scriptless" pageEncoding="UTF-8"%> <%@ tag trimDirectiveWhitespaces="true"%> <%@attribute name="num1"type="java.lang.Integer"required="true"rtexprvalue="false"%> <%@attribute name="num2"type="java.lang.Integer"required="true"rtexprvalue="True"%> <%@ variable name-given="max" variable-class="java.lang.Integer" scope="AT_END"%> <%@ variable name-given="max" variable-class="java.lang.Integer" scope="NESTED"%> <% int max = num1; if(max<num2)max=num2; jspContext.setAttribute("max",max); %> <jsp:doBody/>
      <%@page contentType="text/html; charset=utf-8"%> <%@ taglib prefix="util" tagdir="/WEB-INF/tags"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%-- <c:set var="num1" value="100"/> <c:set var="num2" value="200"/> <util:max num1="${num1}"num2="${num2}"/> <util:max num1="100" num2="100"/> ${max} --%> <util:max num1="100" num2="200"> <h1>큰수:${max}</h1> </util:max>
  1. name-from-attribute:직접지정
    1. jsp에 없는 기능을 태그로 만든다
    1. 관례적으로 var을 많이쓰고 실제 값은 alias=”max”
    1. simpletag 공부?
    <%@ tag body-content="empty" pageEncoding="UTF-8"%> <%@ tag body-content="scriptless" pageEncoding="UTF-8"%> <%@ tag trimDirectiveWhitespaces="true"%> <%@attribute name="var" required="ture" rtexprvalue="false"%> <%@attribute name="num1"type="java.lang.Integer"required="true"rtexprvalue="false"%> <%@attribute name="num2"type="java.lang.Integer"required="true"rtexprvalue="True"%> <%@ variable name-given="max" variable-class="java.lang.Integer" scope="AT_END"%> <%@ variable name-given="max" variable-class="java.lang.Integer" scope="NESTED"%> <%@ variable name-from-attribute="var" alias="max" variable-class="java.lang.Integer" scope="NESTED"%> <% int max = num1; if(max<num2)max=num2; jspContext.setAttribute("max",max); %> <jsp:doBody/>

@taglib

  1. 태그를 쓰는 이유
    <%@ page contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head></head> <body> <header> <h1>사이트 상단 공통 영역</h1> <header> <main> <h1>본문내용</h1> </main> <footer> <h1>사이트 하단 공통 영역</h1> </footer> </body> </html>

    <%@ page contentType="text/html; charset=utf-8"%> <%@ taglib prefix="layout" tagdir="/WEB-INF/tag/layouts"%> <layout:common> <h1>본문내용</h1> </layout:common>
    <%@ tag body-content="scriptless" pageEncoding="UTF-8"%> <%@ tag trimDirectiveWhitespaces="true"%> <%@ page contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head></head> <body> <header> <h1>사이트 상단 공통 영역</h1> <header> <jsp:doBody/> <footer> <h1>사이트 하단 공통 영역</h1> </footer> </body> </html>

    <%@ page contentType="text/html; charset=utf-8"%> <%@ taglib prefix="layout" tagdir="/WEB-INF/tag/layouts"%> <layout:common> <jsp:attribute name="header"> <h1>헤더!</h1> </jsp:attribute> <jsp:attribute name="footer"> <h1>푸터!</h1> </jsp:attribute> <jsp:body> <h1>본문내용</h1> </jsp:body> </layout:common>

    attribute 가 무조건 body위에 있어야한다 단점이라면 단점?

    <%@ tag body-content="scriptless" pageEncoding="UTF-8"%> <%@ tag trimDirectiveWhitespaces="true"%> <%@ page contentType="text/html; charset=utf-8"%> <%@ attribute name="header" fragment="true"%> <%@ attribute name="footer" fragment="true"%> <!DOCTYPE html> <html> <head></head> <body> <header> <jsp:invoke fragment="header"> <header> <jsp:doBody/> <footer> <jsp:invoke fragment="header"> </footer> </body> </html>

<%@tag body-content="empty" pageEncoding="UTF-8"%> <%@tag import="java.util.*"%> <%@tag trimDirectiveWhitespaces="true"%> <%@tag dynamic-attributes="attrs"%> <% Map<String,String>_attrs=jspContext.getAttribute("attrs");<%--불러와야 쓸수있다--%> out.print(_attrs.get("color")); out.print("<br>"); out.print(_attrs.get("size"));<%--동적애트리뷰트는 맵형태로 되어잇다? 맵형태란?--%> %> ${attrs.color}<br> ${attrs.size}

tag파일 →번역

내장객체

httpservelt request request

HttpServelt Response reqspomse

session

config

jspContext


참고

맵형태

맵형태란, 자바에서 key-value 쌍으로 이루어진 데이터를 다룰 때 사용하는 자료구조입니다. 맵에는 여러 가지 구조가 있지만, 일반적으로 Map 인터페이스를 구현한 HashMap 클래스가 많이 사용됩니다. 맵의 키(key)는 중복될 수 없으며, 값(value)은 중복될 수 있습니다. 이러한 맵 형태는 JSP에서 동적 애트리뷰트(dynamic-attributes)를 통해 사용될 수 있습니다.

cookies seesion을 알아야 만들수 있다?

회원가입과 로그인

자바객체


Uploaded by N2T

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.