Saturday, May 21, 2005

SiteMesh

หลังจากลองไล่ดู code ที่ AppFuse generate ให้
ก็พบว่ามีการนำ SiteMesh มาใช้
เจ้า sitemesh ก็คือ decoration framework (ทำหน้าที่เหมือน Tiles ใน Struts)
ทำหน้าที่ intercept response จาก web application
แล้วทำการ extract เฉพาะส่วน body, head
เพื่อนำไปแสดงบน template ที่ได้เตรียมไว้
อธิบายยากแท้ ดูรูปเอาแล้วกัน



เท่าที่อ่านดู feature หรือ การใช้งาน
ก็ดูดี คงต้องทดลองใช้งานก่อน
จึงจะรู้ว่าดีกว่า Tiles หรือเปล่า
แต่เท่าที่จำได้เวลาใช้ tiles จะน่าเบื่อ
ตอนเขียน xml definition ของ tiles
ซึ่งซ้ำไปซ้ำมา และมี Naming ของ page
เกิดขึ้นอีก 1 กลุ่ม (ต้องตั้งชื่อให้กับ
ผลลัพท์ของการจับคู่ template กับ content)

feature ของ sitemesh
  • Determine decorator based on path of requested page.
  • Use different decorators based on time, locale or browser.
  • Use simplified decorators for search-engine robots.
  • Switch decorators based on a URL parameter, request attribute or meta-tag.
  • Use custom decorators based on user's saved settings...


ในการ config ใช้ sitemesh ก็เพียงแต่ใส่ filter, filter-mapping เข้าไปใน web.xml
<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>
<!-- These are needed by Tomcat 5 for forwards -->
<!--dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher-->
</filter-mapping>


เริ่มแรกก็สร้าง file sitemesh.xml ไว้ใต้ WEB-INF
parser tag เป็นตัวกำหนดว่าจะใช้ parser ตัวไหนสำหรับ content แต่ละแบบ
แต่เจ้า sitemesh ให้มาเฉพาะ html parser เท่านั้น
ส่วน decorator-mapper เป็นตัวกำหนด
ว่าจะใช้ decorator อะไรสำหรับเงื่อนไขแบบไหน
ในกรณีนี้เป็นการใช้ ConfigDecoratorMapper
ซึ่งจะอ้างถึง config file ที่ชื่อ decorators.xml อีกที
<sitemesh>
<property name="decorators-file" value="/WEB-INF/decorators.xml"/>
<excludes file="${decorators-file}"/>
<page-parsers>
<parser default="true" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
<parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
<parser content-type="text/html;charset=ISO-8859-1" 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>

ในเนื้อหาของ file decorators.xml
เราสามารถกำหนดได้ว่า จะให้ exclude อะไร
แล้วก็กำหนด pattern ว่า pattern อะไร
ใช้ jsp ตัวไหนเป็น template

<decorators defaultdir="/decorators">
<excludes>
<pattern>/demos/*</pattern>
<pattern>/resources/*</pattern>
</excludes>
<decorator name="default" page="default.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>


สุดท้ายก็เป็นตัวอย่างของ template
ซึ่งใช้ taglib decorator:head กับ decorator:body
เป็นตัวกำหนดจุดที่จะนำเนื้อหามา insert

<%@ taglib
uri="http://www.opensymphony.com/sitemesh/decorator"
prefix="decorator" %>
<html>
<head>
<title>
My Site -
<decorator:title default="Welcome!" />
</title>
<decorator:head />
</head>
<body>
<table>
<tr>
<td>
<H1>
SiteMesh Corporation
<H1>
</td>
</tr>
<tr>
<td><decorator:body /></td>
</tr>
<tr>
<td> SiteMesh copyright</td>
</tr>
</table>
</body>
</html>


ข้อมูลเพิ่มเติม

Related link from Roti

No comments: