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() + " ⇒ " + 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>