SlideShare uma empresa Scribd logo
1 de 16
Baixar para ler offline
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ỏ

Mais conteúdo relacionado

Mais procurados

DVMS tạo module joomla 2.5
DVMS tạo module joomla 2.5DVMS tạo module joomla 2.5
DVMS tạo module joomla 2.5dvms
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate FrameworkPhuoc Nguyen
 
Ebook học Javascript cơ bản tới nâng cao
Ebook học Javascript cơ bản tới nâng caoEbook học Javascript cơ bản tới nâng cao
Ebook học Javascript cơ bản tới nâng caoTrung Thanh Nguyen
 
Bài 7: Thư viện jQuery và thư viện jQuery UI - Giáo trình FPT
Bài 7: Thư viện jQuery và thư viện jQuery UI - Giáo trình FPTBài 7: Thư viện jQuery và thư viện jQuery UI - Giáo trình FPT
Bài 7: Thư viện jQuery và thư viện jQuery UI - Giáo trình FPTMasterCode.vn
 
BÀI 3 Làm việc với JAVASCRIPT và JQUERY - Giáo trình FPT
BÀI 3 Làm việc với JAVASCRIPT và JQUERY - Giáo trình FPTBÀI 3 Làm việc với JAVASCRIPT và JQUERY - Giáo trình FPT
BÀI 3 Làm việc với JAVASCRIPT và JQUERY - Giáo trình FPTMasterCode.vn
 
Bai tap java_script-html-2016
Bai tap java_script-html-2016Bai tap java_script-html-2016
Bai tap java_script-html-2016viethoang89
 
Giao trinh java script
Giao trinh java scriptGiao trinh java script
Giao trinh java scripthieusy
 
Tu hoc javascript
Tu hoc javascriptTu hoc javascript
Tu hoc javascriptzingoncmu2
 
Tài liệu tìm hiểu jQuery dành cho người mới bắt đầu
Tài liệu tìm hiểu jQuery dành cho người mới bắt đầuTài liệu tìm hiểu jQuery dành cho người mới bắt đầu
Tài liệu tìm hiểu jQuery dành cho người mới bắt đầuLel Đặng Văn
 
Giao trinh java_script (1)
Giao trinh java_script (1)Giao trinh java_script (1)
Giao trinh java_script (1)duynamit
 
Bai4 basic jsp_4474
Bai4 basic jsp_4474Bai4 basic jsp_4474
Bai4 basic jsp_4474Ham Chơi
 
Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952Ham Chơi
 
Bat dau hoc lap trinh asp
Bat dau hoc lap trinh aspBat dau hoc lap trinh asp
Bat dau hoc lap trinh aspLam To
 

Mais procurados (19)

Jsp java bean
Jsp   java beanJsp   java bean
Jsp java bean
 
Bai 09 Basic jsp
Bai 09 Basic jspBai 09 Basic jsp
Bai 09 Basic jsp
 
JSP and Database
JSP and DatabaseJSP and Database
JSP and Database
 
DVMS tạo module joomla 2.5
DVMS tạo module joomla 2.5DVMS tạo module joomla 2.5
DVMS tạo module joomla 2.5
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
 
Ebook học Javascript cơ bản tới nâng cao
Ebook học Javascript cơ bản tới nâng caoEbook học Javascript cơ bản tới nâng cao
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
Bài 7: Thư viện jQuery và thư viện jQuery UI - Giáo trình FPTBài 7: Thư viện jQuery và thư viện jQuery UI - Giáo trình FPT
Bài 7: Thư viện jQuery và thư viện jQuery UI - Giáo trình FPT
 
Javascript and dom_html
Javascript and dom_htmlJavascript and dom_html
Javascript and dom_html
 
BÀI 3 Làm việc với JAVASCRIPT và JQUERY - Giáo trình FPT
BÀI 3 Làm việc với JAVASCRIPT và JQUERY - Giáo trình FPTBÀI 3 Làm việc với JAVASCRIPT và JQUERY - Giáo trình FPT
BÀI 3 Làm việc với JAVASCRIPT và JQUERY - Giáo trình FPT
 
Bai tap java_script-html-2016
Bai tap java_script-html-2016Bai tap java_script-html-2016
Bai tap java_script-html-2016
 
Giao trinh java script
Giao trinh java scriptGiao trinh java script
Giao trinh java script
 
Giao Trinh Jquery
Giao Trinh JqueryGiao Trinh Jquery
Giao Trinh Jquery
 
Tu hoc javascript
Tu hoc javascriptTu hoc javascript
Tu hoc javascript
 
Tài liệu tìm hiểu jQuery dành cho người mới bắt đầu
Tài liệu tìm hiểu jQuery dành cho người mới bắt đầuTài liệu tìm hiểu jQuery dành cho người mới bắt đầu
Tài liệu tìm hiểu jQuery dành cho người mới bắt đầu
 
Giao trinh java_script (1)
Giao trinh java_script (1)Giao trinh java_script (1)
Giao trinh java_script (1)
 
Bai4 basic jsp_4474
Bai4 basic jsp_4474Bai4 basic jsp_4474
Bai4 basic jsp_4474
 
Kiem tra javascript
Kiem tra javascriptKiem tra javascript
Kiem tra javascript
 
Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952
 
Bat dau hoc lap trinh asp
Bat dau hoc lap trinh aspBat dau hoc lap trinh asp
Bat dau hoc lap trinh asp
 

Destaque

Bài 12: JSF-2 - Lập Trình Mạng Nâng Cao
Bài 12:  JSF-2 - Lập Trình Mạng Nâng CaoBài 12:  JSF-2 - Lập Trình Mạng Nâng Cao
Bài 12: JSF-2 - Lập Trình Mạng Nâng CaoTuan Nguyen
 
Security Bootcamp 2013 - Lap trinh web an toan
Security Bootcamp 2013 - Lap trinh web an toanSecurity Bootcamp 2013 - Lap trinh web an toan
Security Bootcamp 2013 - Lap trinh web an toanSecurity Bootcamp
 
Bài 1: Web Cơ Bản - Lập Trình Mạng Nâng Cao
Bài 1: Web Cơ Bản - Lập Trình Mạng Nâng CaoBài 1: Web Cơ Bản - Lập Trình Mạng Nâng Cao
Bài 1: Web Cơ Bản - Lập Trình Mạng Nâng CaoTuan Nguyen
 
Sức mạnh của jsf 2, phần 3 xử lý sự kiện, java script và ajax
Sức mạnh của jsf 2, phần 3 xử lý sự kiện, java script và ajaxSức mạnh của jsf 2, phần 3 xử lý sự kiện, java script và ajax
Sức mạnh của jsf 2, phần 3 xử lý sự kiện, java script và ajaxTuyet Tam
 
[Cntt] bài giảng java khtn hcm
[Cntt] bài giảng java   khtn hcm[Cntt] bài giảng java   khtn hcm
[Cntt] bài giảng java khtn hcmHong Phuoc Nguyen
 
Vận dụng kiến thức lập trình web vào môi trường thực tế
Vận dụng kiến thức lập trình web vào môi trường thực tếVận dụng kiến thức lập trình web vào môi trường thực tế
Vận dụng kiến thức lập trình web vào môi trường thực tếVKhang Yang
 
VCP 21- VMWare VPC 6
VCP 21- VMWare VPC 6VCP 21- VMWare VPC 6
VCP 21- VMWare VPC 6Tuan Nguyen
 
tài liệu Mã nguồn mở Lap trình shells
tài liệu Mã nguồn mở  Lap trình shellstài liệu Mã nguồn mở  Lap trình shells
tài liệu Mã nguồn mở Lap trình shellsThuyet Nguyen
 
Presentation on leadership
Presentation on leadershipPresentation on leadership
Presentation on leadershipsd college
 
Leadership concepts and theories
Leadership concepts and theoriesLeadership concepts and theories
Leadership concepts and theoriesalaguraja76
 

Destaque (12)

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

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

Javascript tong-hop a-z
Javascript tong-hop a-zJavascript tong-hop a-z
Javascript tong-hop a-zManhh Nguyễn
 
Tự học JavaScript
Tự học JavaScriptTự học JavaScript
Tự học JavaScriptÁnh Nguyễn
 
Hoc Jquery Trong 1h
Hoc Jquery Trong 1hHoc Jquery Trong 1h
Hoc Jquery Trong 1hDang Tuan
 
Java script dh bk share-book.com
Java script dh bk   share-book.comJava script dh bk   share-book.com
Java script dh bk share-book.comphongbk1609
 
OOP_02_Java can ban.pdf
OOP_02_Java can ban.pdfOOP_02_Java can ban.pdf
OOP_02_Java can ban.pdfssuserd01a5c
 
Nhibernate -Co ban
Nhibernate -Co banNhibernate -Co ban
Nhibernate -Co ban07k1034
 
Slide3 - Co ban HTML5
Slide3 - Co ban HTML5Slide3 - Co ban HTML5
Slide3 - Co ban HTML5Đặng Til
 
Golang web database3
Golang web database3Golang web database3
Golang web database3NISCI
 
Iphone programming
Iphone programmingIphone programming
Iphone programmingCoderseta Jp
 

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

Javascript tong-hop a-z
Javascript tong-hop a-zJavascript tong-hop a-z
Javascript tong-hop a-z
 
Giao trinh java script
Giao trinh java scriptGiao trinh java script
Giao trinh java script
 
Zing osapi v1.3.3
Zing osapi v1.3.3Zing osapi v1.3.3
Zing osapi v1.3.3
 
Tự học JavaScript
Tự học JavaScriptTự học JavaScript
Tự học JavaScript
 
Hoc Jquery Trong 1h
Hoc Jquery Trong 1hHoc Jquery Trong 1h
Hoc Jquery Trong 1h
 
Java script dh bk share-book.com
Java script dh bk   share-book.comJava script dh bk   share-book.com
Java script dh bk share-book.com
 
OOP_02_Java can ban.pdf
OOP_02_Java can ban.pdfOOP_02_Java can ban.pdf
OOP_02_Java can ban.pdf
 
Nhibernate -Co ban
Nhibernate -Co banNhibernate -Co ban
Nhibernate -Co ban
 
Slide3 - Co ban HTML5
Slide3 - Co ban HTML5Slide3 - Co ban HTML5
Slide3 - Co ban HTML5
 
Golang web database3
Golang web database3Golang web database3
Golang web database3
 
Iphone programming
Iphone programmingIphone programming
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>