您的位置:首页 > Web前端 > JavaScript

Jsp:taglib实现

2014-05-04 11:08 253 查看
web应用的结构:

          web.xml

          classes diegoyun OutputTag.class

    WEB-INF  src    diegoyun OutputTag.java

mytag       tlds   diego.tld

    tag.jsp

细节:

web.xml:

<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
</web-app>


OutputTag.java:

package diegoyun;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class OutputTag extends TagSupport
{
private String name=null;
public void setName(String name)
{
this.name = name;
}

public int doStartTag() throws JspException{
try
{
JspWriter out = pageContext.getOut();
out.print("Hello! " + name);
}
catch (Exception e)
{
throw new JspException(e);
}
return EVAL_PAGE;
}
}


diego.tld:

<?xml version="1.0" encoding="GBK" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>diego</short-name>
<!--OutputTag-->
<tag>
<name>out</name>
<tag-class>diegoyun.OutputTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>


tag.jsp:

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>

<%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
Test Tag:
<diego:out name="diegoyun"/>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: