SlideShare a Scribd company logo
Custom Tag
ThS Văn Thiên Hoàng
Mô hình công nghệ
Các mã g i tr c ti p Servletọ ự ế
Các mã g i Servlet không tr c ti p (JSP)ọ ự ế
Beans
Servlet/JSP
MVC v i s k t h p JSP, Servlet, Beanớ ự ế ợ
Custom tag
MVC v i Bean, Custom tag, và cácớ
framework h tr Structs ho c JSF.ỗ ợ ặ
ng D ngỨ ụ
đ n gi nơ ả
ng D ngỨ ụ
ph c t pứ ạ
Các d ng Custom Tagạ
Tag Files
Simple Tag handlers
Classic Tag handlers
Simple Tag
Các b c đ t o m t Tagướ ể ạ ộ
B c 1: K th a l p SimpleTagSupport vàướ ế ừ ớ
vi t đè ph ng th c doTag().ế ươ ứ
package my;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class Simple extends SimpleTagSupport {   
public void doTag() throws IOException, JspException
{    
 getJspContext().getOut().append("Hello from a
simple tag handler!!");  
 }
}
Các b c đ t o m t Tag (1)ướ ể ạ ộ
Đ nh nghĩa t p tin (.TLD) miêu t tag trong thị ậ ả ư
vi n. Đ t t p tin này trong th m c WEB-INF.ệ ặ ậ ư ụ
<?xml version=“1.0” encoding=“UTF-8” ?>
<taglib ...>
<uri>nikojava</uri>
<tag>
<name>simple</name>
<tag-class>my.Simple</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
Các b c đ t o m t Tag (2)ướ ể ạ ộ
G i th ra s d ng.ọ ẻ ử ụ
<%@ taglib prefix="yo" uri="nikojava" %>
<html>   
<body>
     <yo:simple />   
</body>
</html>
X lý n i dung th tagử ộ ẻ
S d ng ph ng th c invoke.ử ụ ươ ứ
Đ nh nghĩa thông tin th .TLDị ẻ
public class Simple extends SimpleTagSupport {   
public void doTag() throws IOException, JspException {    
getJspContext().getOut().print("This is my body:<br />");     
getJspBody().invoke(null);   
}
}
<tag>   
<name>simple</name>   
<tag-class>my.Simple</tag-class>   
<body-content>scriptless</body-content>
</tag>
X lý n i dung th tag (1)ử ộ ẻ
G i th ra s d ng.ọ ẻ ử ụ
<%@ taglib prefix="yo" uri="nikojava" %>
<html>   <body>    
 <yo:simple>Some useful content</yo:simple>   
</body> </html>
Đ nh nghĩa thu c tính bên trong thị ộ ẻ
G i th ra s d ng.ọ ẻ ử ụ
public class Simple extends SimpleTagSupport {  
public void doTag() throws IOException, JspException
{   
 getJspContext().setAttribute("friend", "Niko");   
getJspBody().invoke(null);  
}
}
<%@ taglib prefix="yo" uri="nikojava" %>
<html>  <body>    <yo:simple>Hello ${friend}!!</yo:simple>  
</body></html>
Đ nh nghĩa thu c tính bên trong thị ộ ẻ
(1)
public class Simple extends SimpleTagSupport {  
private String friend;  
public void doTag() throws IOException, JspException {    
getJspContext().getOut().append("Hello " + friend + "!!"); 
  }  
public void setFriend(String friend) {    this.friend = friend;  }
}
Đ nh nghĩa thu c tính bên trong thị ộ ẻ
(1)
<tag>  
<name>simple</name>  
<tag-class>my.Simple</tag-class>  
<body-content>empty</body-content>  
<attribute>    
<name>friend</name> 
   <required>true</required>
     <rtexprvalue>true</rtexprvalue>
  </attribute>
</tag>
<%@ taglib prefix="yo" uri="nikojava" %>
<html>  <body>    
<yo:simple friend="Niko" />  
</body></html>
Đ nh nghĩa thu c tính đ ngị ộ ộ
public class Simple extends SimpleTagSupport implements
DynamicAttributes
{  
private Map<String, Object> map = new HashMap<String, Object>();  
public void setDynamicAttribute(String uri, String name, Object value)
{    map.put(name, value);  }  
public void doTag() throws IOException, JspException {
     JspWriter out = getJspContext().getOut();    
out.append("These are the dynamic attributes:");
    out.append("<ul>");    
for (Map.Entry<String, Object> element : map.entrySet())
{      out.append("<li>");      
out.append(element.getKey() + " &rArr; " + element.getValue());
       out.append("</li>");    
}   
 out.append("</ul>");  }}
Đ nh nghĩa thu c tính đ ng (1)ị ộ ộ
<tag>  
<name>simple</name>  
<tag-class>my.Simple</tag-class>  
<body-content>empty</body-content>  
<dynamic-attributes>true</dynamic-attributes>
</tag>
<%@ taglib prefix="yo" uri="nikojava" %>
<html>  <body>    
<yo:simple friend="Niko" casoline="soft" more="lagi" />  
</body></html>
B qua ph n còn l i c a trangỏ ầ ạ ủ
public class Simple extends SimpleTagSupport {  
public void doTag() throws IOException, JspException
{   
 getJspContext().getOut().append("Encapsulation");   
 throw new SkipPageException();  
}
}
<%@ taglib prefix="yo" uri="nikojava" %>
<html>  <body>   
 (Start of page)    
<yo:simple />    
(End of page)  
</body></html>
Câu h iỏ

More Related Content

PPT
Bài 6: Custom Tag - Lập Trình Mạng Nâng Cao
PPT
Bài 7,8: Custom Tag - Lập Trình Mạng Nâng Cao
PPT
Bài 3: Servlet - Lập Trình Mạng Nâng Cao
PPT
Bài 5: Java Bean - Lập Trình Mạng Nâng Cao
PPT
Bài 3: Servlet&Cookie&Session - Lập Trình Mạng Nâng Cao
PPT
Bài 2: J2EE - Lập Trình Mạng Nâng Cao
PPT
Bài 11: JSF-1 - Lập Trình Mạng Nâng Cao
PPT
Bài 4: JSP Cơ Bản - Lập Trình Mạng Nâng Cao
Bài 6: Custom Tag - Lập Trình Mạng Nâng Cao
Bài 7,8: Custom Tag - Lập Trình Mạng Nâng Cao
Bài 3: Servlet - Lập Trình Mạng Nâng Cao
Bài 5: Java Bean - Lập Trình Mạng Nâng Cao
Bài 3: Servlet&Cookie&Session - Lập Trình Mạng Nâng Cao
Bài 2: J2EE - Lập Trình Mạng Nâng Cao
Bài 11: JSF-1 - Lập Trình Mạng Nâng Cao
Bài 4: JSP Cơ Bản - Lập Trình Mạng Nâng Cao

What's hot (19)

PPT
Jsp java bean
PDF
Bai 09 Basic jsp
DOCX
JSP and Database
PDF
DVMS tạo module joomla 2.5
PPTX
Introduction to Hibernate Framework
PDF
Ebook học Javascript cơ bản tới nâng cao
PDF
Bài 7: Thư viện jQuery và thư viện jQuery UI - Giáo trình FPT
PPT
Javascript and dom_html
PDF
BÀI 3 Làm việc với JAVASCRIPT và JQUERY - Giáo trình FPT
DOC
Bai tap java_script-html-2016
PDF
Giao trinh java script
PDF
Giao Trinh Jquery
PDF
Tu hoc javascript
PDF
Tài liệu tìm hiểu jQuery dành cho người mới bắt đầu
DOC
Giao trinh java_script (1)
PDF
Bai4 basic jsp_4474
DOCX
Kiem tra javascript
PDF
Bai1 gioi thieu_servlet_va_jsp_8952
DOCX
Bat dau hoc lap trinh asp
Jsp java bean
Bai 09 Basic jsp
JSP and Database
DVMS tạo module joomla 2.5
Introduction to Hibernate Framework
Ebook học Javascript cơ bản tới nâng cao
Bài 7: Thư viện jQuery và thư viện jQuery UI - Giáo trình FPT
Javascript and dom_html
BÀI 3 Làm việc với JAVASCRIPT và JQUERY - Giáo trình FPT
Bai tap java_script-html-2016
Giao trinh java script
Giao Trinh Jquery
Tu hoc javascript
Tài liệu tìm hiểu jQuery dành cho người mới bắt đầu
Giao trinh java_script (1)
Bai4 basic jsp_4474
Kiem tra javascript
Bai1 gioi thieu_servlet_va_jsp_8952
Bat dau hoc lap trinh asp
Ad

Viewers also liked (12)

PPT
Bài 12: JSF-2 - Lập Trình Mạng Nâng Cao
PDF
Security Bootcamp 2013 - Lap trinh web an toan
PPT
Bài 1: Web Cơ Bản - Lập Trình Mạng Nâng Cao
DOCX
Sức mạnh của jsf 2, phần 3 xử lý sự kiện, java script và ajax
PDF
[Cntt] bài giảng java khtn hcm
PDF
Vận dụng kiến thức lập trình web vào môi trường thực tế
PDF
VCP 21- VMWare VPC 6
PDF
Linux LPI Bacis
PPT
Java beans
PDF
tài liệu Mã nguồn mở Lap trình shells
PPTX
Presentation on leadership
PPTX
Leadership concepts and theories
Bài 12: JSF-2 - Lập Trình Mạng Nâng Cao
Security Bootcamp 2013 - Lap trinh web an toan
Bài 1: Web Cơ Bản - Lập Trình Mạng Nâng Cao
Sức mạnh của jsf 2, phần 3 xử lý sự kiện, java script và ajax
[Cntt] bài giảng java khtn hcm
Vận dụng kiến thức lập trình web vào môi trường thực tế
VCP 21- VMWare VPC 6
Linux LPI Bacis
Java beans
tài liệu Mã nguồn mở Lap trình shells
Presentation on leadership
Leadership concepts and theories
Ad

Similar to Bài 10: Custom Tag - Lập Trình Mạng Nâng Cao (12)

PDF
Javascript tong-hop a-z
PDF
Giao trinh java script
PDF
Zing osapi v1.3.3
PDF
Tự học JavaScript
PPTX
introduction to java script, history, functionalities
PPT
Hoc Jquery Trong 1h
PDF
Java script dh bk share-book.com
PDF
OOP_02_Java can ban.pdf
PPTX
Nhibernate -Co ban
PDF
Slide3 - Co ban HTML5
ODP
Golang web database3
PPTX
Iphone programming
Javascript tong-hop a-z
Giao trinh java script
Zing osapi v1.3.3
Tự học JavaScript
introduction to java script, history, functionalities
Hoc Jquery Trong 1h
Java script dh bk share-book.com
OOP_02_Java can ban.pdf
Nhibernate -Co ban
Slide3 - Co ban HTML5
Golang web database3
Iphone programming

Bài 10: Custom Tag - Lập Trình Mạng Nâng Cao

  • 1. Custom Tag ThS Văn Thiên Hoàng
  • 2. Mô hình công nghệ Các mã g i tr c ti p Servletọ ự ế Các mã g i Servlet không tr c ti p (JSP)ọ ự ế Beans Servlet/JSP MVC v i s k t h p JSP, Servlet, Beanớ ự ế ợ Custom tag MVC v i Bean, Custom tag, và cácớ framework h tr Structs ho c JSF.ỗ ợ ặ ng D ngỨ ụ đ n gi nơ ả ng D ngỨ ụ ph c t pứ ạ
  • 3. Các d ng Custom Tagạ Tag Files Simple Tag handlers Classic Tag handlers
  • 5. Các b c đ t o m t Tagướ ể ạ ộ B c 1: K th a l p SimpleTagSupport vàướ ế ừ ớ vi t đè ph ng th c doTag().ế ươ ứ package my; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; public class Simple extends SimpleTagSupport {    public void doTag() throws IOException, JspException {      getJspContext().getOut().append("Hello from a simple tag handler!!");    } }
  • 6. Các b c đ t o m t Tag (1)ướ ể ạ ộ Đ nh nghĩa t p tin (.TLD) miêu t tag trong thị ậ ả ư vi n. Đ t t p tin này trong th m c WEB-INF.ệ ặ ậ ư ụ <?xml version=“1.0” encoding=“UTF-8” ?> <taglib ...> <uri>nikojava</uri> <tag> <name>simple</name> <tag-class>my.Simple</tag-class> <body-content>empty</body-content> </tag> </taglib>
  • 7. Các b c đ t o m t Tag (2)ướ ể ạ ộ G i th ra s d ng.ọ ẻ ử ụ <%@ taglib prefix="yo" uri="nikojava" %> <html>    <body>      <yo:simple />    </body> </html>
  • 8. X lý n i dung th tagử ộ ẻ S d ng ph ng th c invoke.ử ụ ươ ứ Đ nh nghĩa thông tin th .TLDị ẻ public class Simple extends SimpleTagSupport {    public void doTag() throws IOException, JspException {     getJspContext().getOut().print("This is my body:<br />");      getJspBody().invoke(null);    } } <tag>    <name>simple</name>    <tag-class>my.Simple</tag-class>    <body-content>scriptless</body-content> </tag>
  • 9. X lý n i dung th tag (1)ử ộ ẻ G i th ra s d ng.ọ ẻ ử ụ <%@ taglib prefix="yo" uri="nikojava" %> <html>   <body>      <yo:simple>Some useful content</yo:simple>    </body> </html>
  • 10. Đ nh nghĩa thu c tính bên trong thị ộ ẻ G i th ra s d ng.ọ ẻ ử ụ public class Simple extends SimpleTagSupport {   public void doTag() throws IOException, JspException {     getJspContext().setAttribute("friend", "Niko");    getJspBody().invoke(null);   } } <%@ taglib prefix="yo" uri="nikojava" %> <html>  <body>    <yo:simple>Hello ${friend}!!</yo:simple>   </body></html>
  • 11. Đ nh nghĩa thu c tính bên trong thị ộ ẻ (1) public class Simple extends SimpleTagSupport {   private String friend;   public void doTag() throws IOException, JspException {     getJspContext().getOut().append("Hello " + friend + "!!");    }   public void setFriend(String friend) {    this.friend = friend;  } }
  • 12. Đ nh nghĩa thu c tính bên trong thị ộ ẻ (1) <tag>   <name>simple</name>   <tag-class>my.Simple</tag-class>   <body-content>empty</body-content>   <attribute>     <name>friend</name>     <required>true</required>      <rtexprvalue>true</rtexprvalue>   </attribute> </tag> <%@ taglib prefix="yo" uri="nikojava" %> <html>  <body>     <yo:simple friend="Niko" />   </body></html>
  • 13. Đ nh nghĩa thu c tính đ ngị ộ ộ public class Simple extends SimpleTagSupport implements DynamicAttributes {   private Map<String, Object> map = new HashMap<String, Object>();   public void setDynamicAttribute(String uri, String name, Object value) {    map.put(name, value);  }   public void doTag() throws IOException, JspException {      JspWriter out = getJspContext().getOut();     out.append("These are the dynamic attributes:");     out.append("<ul>");     for (Map.Entry<String, Object> element : map.entrySet()) {      out.append("<li>");       out.append(element.getKey() + " &rArr; " + element.getValue());        out.append("</li>");     }     out.append("</ul>");  }}
  • 14. Đ nh nghĩa thu c tính đ ng (1)ị ộ ộ <tag>   <name>simple</name>   <tag-class>my.Simple</tag-class>   <body-content>empty</body-content>   <dynamic-attributes>true</dynamic-attributes> </tag> <%@ taglib prefix="yo" uri="nikojava" %> <html>  <body>     <yo:simple friend="Niko" casoline="soft" more="lagi" />   </body></html>
  • 15. B qua ph n còn l i c a trangỏ ầ ạ ủ public class Simple extends SimpleTagSupport {   public void doTag() throws IOException, JspException {     getJspContext().getOut().append("Encapsulation");     throw new SkipPageException();   } } <%@ taglib prefix="yo" uri="nikojava" %> <html>  <body>     (Start of page)     <yo:simple />     (End of page)   </body></html>