SiteMesh로 레이아웃 적용하기
1. SiteMesh 2.3을 다운로드 하여, sitemesh-x.jar 파일을 [web-app]/WEB-INF/lib 폴더에 복사한다.
(http://www.opensymphony.com/sitemesh/download.html)
2. [web-app]/WEB-INF/sitemesh.xml 파일을 생성한다. (선택적이다.)
<property name="decorators-file" value="/WEB-INF/decorators.xml" />
<excludes file="${decorators-file}" />
<page-parsers>
<parser content-type="text/html"
class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
<parser content-type="text/html;charset=UTF-8"
class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
</page-parsers>
<decorator-mappers>
<mapper class=
"com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}" />
</mapper>
</decorator-mappers>
</sitemesh>
3. [web-app]/WEB-INF/web.xml파일에 sitemesh가 제공한는 PageFilter 를 추가한다.
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
/로 들어오는 모든 요청에 대해 PageFilter를 적용한다.
PageFilter는 요청 URL과 매칭되는 데코레이터를 검색한뒤,
매칭되는 decorator가 발견될 경우 결과 HTML에 매칭되는 decorator를 적용한다.
따라서 decorator가 적용되어야 하는 URL의 경우 반드시 PageFilter에 적용시켜 준다.
4. 다음으로 [web-app]/WEB-INF/decorators.xml 파일을 생성한다.
[web-app]/decorators 폴더에 적용될 decorator 파일을 추가 한다.
[web-app]/decorators/main.jsp
1: <%--
2: % This is the main decorator for all SOMECOMPANY INTRANET pages.
3: % It includes standard caching, style sheet, header, footer and copyright notice.
4: --%>
5: <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
6: <%@ include file="/includes/cache.jsp" %>
7: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
8: <html>
9: <head>
10: <title><decorator:title default="INTRANET" /></title>
11: <decorator:head />
12: <%@ include file="/includes/style.jsp" %>
13: </head>
14: <body bgcolor="#FFFFFF" background="<%=request.getContextPath()%>/images/bg.gif">
15: <script type="text/javascript">window.status = "Loading: <decorator:title default="INTRANET" />...";</script>
16: <%@ include file="/includes/header.jsp"%>
17: <table width="100%" border="0" cellspacing="0" cellpadding="0">
18: <tr>
19: <td height="20" nowrap> </td>
20: </tr>
21: <tr>
22: <td width="1%" nowrap> </td>
23: <td width="16%" valign="top" nowrap>
23: <script type="text/javascript">window.status = "Loading: Navigation...";</script>
24: <%@ include file="/includes/navigation.jsp" %>
25: </td>
26: <td width="2%" nowrap> </td>
27: <td valign="top">
28: <br>
29: <script type="text/javascript">window.status = "Loading: Document body...";</script>
30: <div class="docBody"><decorator:body /></div>
31: </td>
32: <td width="1%" nowrap> </td>
33: </tr>
34: </table>
35: <br>
36: <%@ include file="/includes/footer.jsp" %>
37: <%@ include file="/includes/copyright.jsp" %>
38: <script type="text/javascript">window.status = "Done";</script>
39: </body>
40: </html>
자신의 웹시스템 레이아웃에 맞게 변경 하면된다.
5. 이제 [web-app]/WEB-INF/decorators.xml 파일에 decorator를 mapping 시킨다.
<decorator name="sub" page="sub.jsp">
<pattern>/sub/*</pattern>
</decorator>
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
/sub/test.jsp 요청이 들어오는 경우 'sub' decorator가 적용되며,
그 외 다른 경로로 들어오는 경우 'main' decorator가 적용된다.
그리고 servlet mapping시 주의 사항
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>main/*</url-pattern>
</servlet-mapping>
이 경우, 해당 servlet mapping에 decorator를 적용 하고자 할 경우,
decorators.xml파일을 다음과 같이 처리 해준다.
<pattern>/main</pattern>
</decorator>