1. ในเทมเพลนมีส่วนที่ซ้ำกันทุกหน้าคือ header และ footer ให้ทำการแยกโค้ดในส่วนของ header และ footer เก็บไว้ในเพจ .ftl เช่น header.ftl และ footer.ftl
2. โค้ดในส่วนที่เหลือจาก header.ftl และ footer.ftl ให้เซฟเก็บไว้เป็นไฟล์ index.ftl
3. เปิดไฟล์ CommonScreens.xml ซึ่งจะอยู่ในโฟลเดอร์ hot-deploy/culinary/widget/CommonScreens.xml
4. ทำการสร้าง Screen สำหรับที่จะใช้เป็น template ภายใต้แท็ก <screens></screens> ดังโค้ดต่อไปนี้
<screen name="CommonDecorator">
<section>
<widgets>
<!-- header section -->
<platform-specific>
<html>
<html-template location="component://culinary/webapp/culinary/includes/header.ftl"/>
</html>
</platform-specific>
<decorator-section-include name="body"/>
<!-- right -->
<platform-specific>
<html>
<html-template location="component://culinary/webapp/culinary/includes/right.ftl"/>
</html>
</platform-specific>
<!-- footer section -->
<platform-specific>
<html>
<html-template location="component://culinary/webapp/culinary/includes/footer.ftl"/>
</html>
</platform-specific>
</widgets>
</section>
</screen>
อธิบายโค้ด
<screen name="CommonDecorator"> ตั้งชื่อ screen นี้ว่า CommonDecorator
<platform-specific>
<html>
<html-template location="component://culinary/webapp/culinary/includes/header.ftl"/>
</html>
</platform-specific>
ดึงไฟล์ header.ftl มาเป็นส่วนหนึ่งของเทมเพลต<decorator-section-include name="body"/>เตรียมพื้นที่ไว้รอแสดงส่วนเนื้อหาจากหน้าอื่น
<platform-specific>
<html>
<html-template location="component://culinary/webapp/culinary/includes/footer.ftl"/>
</html>
</platform-specific>
ดึงไฟล์ header.ftl มาเป็นส่วนหนึ่งของเทมเพลตเมื่อเสร็จขั้นตอนนี้ จะได้ เทมเพลตที่ประกอบไปด้วยส่วน header และ footer ที่ไม่มีการเปลี่ยนแปลงของ screen และมีส่วน body ที่ define ไว้รอสำหรับการนำเอาเนื้อหาที่มีการเปลี่ยนแปลงในแต่ละหน้ามาแสดงผล
5. ในโฟลเดอร์ widget จะมีไฟล์ xml ที่ใช้จัดการกับ screen อีก 3 ไฟล์ ซึ่งจะมี 1 ไฟล์ที่ชื่อเดียวกับชื่อ component เช่น ถ้า component ชื่อ culinary ไฟล์นี้จะชื่อ CulinaryScreens.xml ทำการเปิดไฟล์นี้และเพิ่มโค้ดดังนี้
<screen name="home">
<section>
<widgets>
<decorator-screen name="CommonDecorator" location="component://culinary/widget/CommonScreens.xml">
<decorator-section name="body">
<platform-specific>
<html>
<html-template location="component://culinary/webapp/culinary/home.ftl"/>
</html>
</platform-specific>
</decorator-section>
</decorator-screen>
</widgets>
</section>
</screen>
อธิบายโค้ด
<decorator-screen location="component://culinary/widget/CommonScreens.xml" name="CommonDecorator">ตั้งชื่อ screen ว่า CommonDecorator และ url ของ screen นี้ถูกกำหนดลงใน location
<decorator-section name="body">
<platform-specific>
<html>
<html-template location="component://culinary/webapp/culinary/home.ftl"/>
</html>
</platform-specific>
</decorator-section>
อธิบายโค้ดในส่วน decorator-section ชื่อ body ให้เรียกไฟล์ home.ftl มาแสดงผล
6. ทำการกำหนด controller และ view ในไฟล์ webapp/culinary/WEB-INF/controller.xml ดังนี้
<request-map uri="home">
<security https="false" auth="true"/>
<response name="success" type="view" value="home"/></request-map> <view-map name="home" type="screen" page="component://culinary/widget/CulinaryScreens.xml#home"/>
7. ทดสอบเรียกหน้า home ขึ้นมาแสดงผล
ไม่มีความคิดเห็น:
แสดงความคิดเห็น