Upload
tuan-nguyen
View
238
Download
0
Embed Size (px)
DESCRIPTION
Lập Trình Mạng Nâng Cao
Citation preview
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
Các bước sử dụng Tag File
Bước 1: viết tập tin simple.tag trong thư mục WEB-INF/tags.
<%-- simple.tag --%>
<p>Greetings from a JSP Tag File!!</p>
Các bước sử dụng Tag File (1)
Bước 2:.
<%-- output.jsp --%>
<%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
<html>
<body>
<my:simple />
</body>
</html>
Truyền thuộc tính
Khai báo dẫn hướng cho phép truyền giá trị thuộc tính vào trong thẻ.
<%-- simple.tag --%>
<%@ attribute name="friend" %><p>Hello ${friend}, greetings from a JSP Tag File!!</p>
<%-- output.jsp --%><%@ taglib prefix="my" tagdir="/WEB-INF/tags" %><html> <body> <my:simple friend="Niko" /> </body></html>
Truyền thuộc tính (1)
Đánh dấu thuộc tính bắt buộc.
Chỉ định thuộc tính được tính toán trong thời gian thực thi.
<%@ attribute name="friend" required="true" %>
<%@ attribute name="friend" rtexprvalue="true" %>
Xử lý nội dung bên trong thẻ
Truyền nội dung giữa cặp thẻ vào trong thẻ.
<%-- simple.tag --%><%@ tag body-content="scriptless" %><p>This is my body: <jsp:doBody /></p><%-- output.jsp --%><%@ taglib prefix="my" tagdir="/WEB-INF/tags" %><html> <body> <my:simple>Some useful content</my:simple> </body></html>
Xử lý truyền tham số động
<%-- simple.tag --%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ tag dynamic-attributes="map" %><p>These are the dynamic attributes:</p><ol> <c:forEach var="item" items="${map}">
<li>${item.key}</li> </c:forEach></ol><%-- output.jsp --%><%@ taglib prefix="my" tagdir="/WEB-INF/tags" %><html> <body> <my:simple name="Niko" java="100%" /> </body></html>
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() + " ⇒ " + 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