ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JSP Action Tag (include)
    JSP 프로그래밍 2019. 12. 28. 02:04

    전 게시글에서는 액션태그 중 forward 방식으로,

    페이지를 이동하는 방식을 알아봤다.

    이 게시글에서는 액션태그 include 를 활용해서 

    JSP 를 모듈화 하는 예제를 알아보자.

     

     

    include 의 형식 번째,

     

    <jsp:include file="포함할 페이지이름" flush="true or false">

    <jsp:param name = "파라미터 이름" value="값"/>

    </jsp include>

     

     

    바로 예제로 알아보자 번째,

     

    예제를 만들기 전에 먼저 경로를 맞출건데,

    C:/web/template 폴더를 하나 만들고

    C:/web/module 폴더를 만들자.

     

    <!-- C:/web/module 폴더 안에 jsp파일들을 만들자 -->
    
    <!-- C:/web/module/top.jsp -->
    <%@ page contentType="text/html; charset="UTF-8" language="java" %>
    상단 메뉴 : 
    <a href="#">HOME</a>
    <a href="#">INFO</a>
    
    <!-- C:/web/module/bottom.jsp -->
    <%@ page contentType="text/html; charset="UTF-8" language="java" %>
    하단메뉴 : 소개 | 도움말 | 약관 | 사이트맵
    
    <!-- C:/web/module/left.jsp -->
    <%@ page contentType="text/html; charset="UTF-8" language="java" %>
    좌측메뉴:
    

     

    <!-- C:/web/template 폴더에 jsp 파일들을 만들자 -->
    
    <!-- C:/web/template/info_view.jsp -->
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <table width="100%" border="1" cellspacing="0" cellpadding="0">
        <tr>
            <td>제품번호</td> <td>XXXX</td>
        </tr>
        <tr>
            <td>가격</td> <td>10,000원</td>
        </tr>
    </table>
    <!-- infoSub.jsp의 페이지를 여기에 include 한다. -->
    <!-- jsp:param 태그로 파라미터 이름을 type 로 설정하고 값은 "B" 로 초기화했다.-->
    <jsp:include page="infoSub.jsp" flush="false">
        <jsp:param name="type" value="B"/>
    </jsp:include>
    
    
    <!-- C:/web/template/infoSub.jsp -->
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <!-- 파라미터 이름이 type 인 값을 가져온다.(위에서 값을 B로 주었기에 B를 가져올 것이다.) -->
    <%
        String type = request.getParameter("type");
        if(type!=null){
    %>
    <br>
    <table width="100%" border="1" cellpadding="0" cellspacing="0">
        <tr>
            <td>타입</td>
            <!--Expression 사용해서 타입은 B가 될 것이고.-->
            <td><b><%= type%></b></td>
        </tr>
    
        <tr>
            <td>특징</td>
            <td>
            <!--조건식으로 파라미터 값이 A일때, B일때 나눠놓았다.-->
                <%if(type.equals("A")){ %>
                강한 내구성.
                <%}else if (type.equals("B")){ %>
                뛰어난 대처능력.
                <%}%>
            </td>
        </tr>
    </table>
    <%}%>
    <!-- jsp파일은 위의 경로와 같이 C:/web/template -->
    
    <!-- C:/web/template/template.jsp -->
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <!-- template 는 틀이라고 생각하면 되고 위에서 만들었던건 틀에 들어갈 구성요소들이라고 생각하자 -->
    
    <!-- 여기서 따로 pageTitle , contentPage 를 정의하는데, 밑에서 만든 mainPage가 컨트롤러 역할을 할 것이다. -->
    <%
        String pageTitle = (String)request.getAttribute("PAGETITLE");
        String contentPage = request.getParameter("CONTENTPAGE");
    %>
    <html>
    <head>
        <title><%= pageTitle%></title>
    </head>
    <body>
    <table width="400" border="1" cellpadding="2" cellspacing="0">
        <tr>
            <td colspan="2">
                <jsp:include page="/module/top.jsp" flush="false" />
            </td>
        </tr>
        <tr>
            <td width="100" valign="top">
                <jsp:include page="/module/left.jsp" flush="false"/>
            </td>
            <td width="300" valign="top">
                <jsp:include page="<%=contentPage%>" flush="false"/>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <jsp:include page="/module/bottom.jsp" flush="false"/>
            </td>
        </tr>
    </table>
    
    </body>
    </html>
    
    
    <!-- C:/web/template/mainPage.jsp -->
    <!-- 위에서 잠깐 말했듯이 mainPage 는 Controller 역할을 하게된다. -->
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <% request.setAttribute("PAGETITLE","여기서 타이틀 설정..");%>
    <!-- 메인 페이지에 접속하면 template.jsp로 이동하고 , 파라미터 이름은 CONTENTPAGE , 값은 info_view.jsp 이다. -->
    <jsp:forward page="template.jsp">
        <jsp:param name="CONTENTPAGE" value="info_view.jsp"/>
    </jsp:forward>

     

    실제로 클라이언트가 접속하는 것은 mainPage 지만 모듈화 시켰기에 관리자가 나중에 관리하기가 수월하다는 장점이 있다.

     

    'JSP 프로그래밍' 카테고리의 다른 글

    JSP JavaBeans  (0) 2019.12.28
    JSP Action Tag (forward)  (0) 2019.12.28
    JSP 내장객체  (0) 2019.12.26
    JSP 지시어 , 에러처리  (0) 2019.12.23
    JSP 시작하기  (0) 2019.12.23

    댓글

Designed by Tistory.