Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม

แอตทริบิวต์มาตรฐานที่ควรส่งให้ custom tag ใน JSP

เมื่อสร้าง custom tag ควรกำหนดพร็อพเพอร์ตี้ต่อไปนี้สำหรับแต่ละ attribute ในไฟล์ TLD (Tag Library Descriptor)

ลำดับพร็อพเพอร์ตี้วัตถุประสงค์
1nameชื่อของ attribute (ต้องไม่ซ้ำใน tag เดียวกัน)
2requiredtrue/false - บังคับหรือไม่บังคับ
3rtexprvaluetrue/false - รองรับ runtime expression (เช่น ${...}) ได้หรือไม่
4typeคลาส Java ของ attribute (ค่าเริ่มต้น String)
5descriptionคำอธิบายเพื่อเอกสาร
6fragmenttrue/false - ค่าควรถือเป็น JspFragment หรือไม่

ตัวอย่างใน TLD

<attribute>
    <name>attribute_name</name>
    <required>false</required>
    <type>java.util.Date</type>
    <fragment>false</fragment>
</attribute>

ตัวอย่าง 2 attributes

<attribute>
    <name>attribute_name1</name>
    <required>false</required>
    <type>java.lang.Boolean</type>
    <fragment>false</fragment>
</attribute>
<attribute>
    <name>attribute_name2</name>
    <required>true</required>
    <type>java.util.Date</type>
</attribute>

คำแนะนำ

  • ตั้งค่า required="true" สำหรับ attribute ที่จำเป็น
  • ใช้ rtexprvalue="true" เพื่อรองรับ EL expressions
  • ระบุ type ให้ชัดเจนเพื่อ type safety
  • ใช้ fragment="true" สำหรับ attribute ที่รับ body content แบบ dynamic