jsp自定义标签

1 jsp自定义标签,大致分三个步骤。
第一个步骤:创建自定义标签处理类。
第二个步骤:生成自定义标签库及加载到web.xml中。
第三个步骤:在jsp页面中引入并使用。


2 ,创建自定义标签处理类,要继承TagSupport
如:
public class MyTag extends TagSupport{


private String parameter="date";
private String neirong="";


public String getParameter() {
return parameter;
}


public void setParameter(String parameter) {
this.parameter = parameter;
}



public String getNeirong() {
return neirong;
}


public void setNeirong(String neirong) {
this.neirong = neirong;
}


@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
JspWriter out=pageContext.getOut();
try {
if(parameter.equalsIgnoreCase("filename")){

out.print(request.getServletPath());

}else{
out.print(new Date());
}

if(neirong.equals("ceshi")){
out.print("this is a ceshi neirong");
}else if(neirong.equals("shice")){
out.print("neirong ceshi a is this");
}else{
out.print("");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//return super.doStartTag();
return SKIP_BODY;
}


}
java类中的属性,要有set和get,然后在这些属性会在jsp标签内部作为标签的属性,
doStartTag为自定义标签开始处理类。返回的是SKIP_BODY为0。


3 创建自定义标签库及加载到web.xml中
如:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
 <tlib-version>1.0</tlib-version>
 <jsp-version>1.2</jsp-version>
 <short-name>demo</short-name>
 <tag>
  <name>demo</name>
  <tag-class>com.tag.MyTag</tag-class>
  <body-content>empty</body-content>
  <attribute>
  <name>parameter</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
  <name>neirong</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
 <tag>
  <name>demo3</name>
  <tag-class>com.tag.MyTag3</tag-class>
  <body-content>jsp</body-content>
 </tag>
</taglib>
short-name为默认的prefix值。
tag内部的name为prefix后冒号的值。
tag-class为自定义标签处理类。
body-content的值为empty表示内部不增加jsp代码。为jsp时可以增加jsp代码
attribute内部的name为属性值。
required为是否必须
rtexprvalue为是否支持jsp中的输出语句:<%=......%>


最后加载到web.xml中
如:<jsp-config>
<taglib>
<taglib-uri>http://displaytag.sourceforge.net/</taglib-uri>
<taglib-location>
/WEB-INF/displaytag-11.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/demo</taglib-uri>
<taglib-location>/WEB-INF/demo.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/demo1</taglib-uri>
<taglib-location>/WEB-INF/demo1.tld</taglib-location>
</taglib>
</jsp-config>


4 在jsp页面中需要引入,然后使用
引入:<%@taglib prefix="ceshi" uri="/WEB-INF/demo"%>
使用: <ceshi:demo parameter="filename" neirong="ceshi"/>
    --------------------------------<br/><br/>
    <ceshi:demo parameter="data" />


这样简单的就创建了自定义标签。


5还有一种是自定义内容标签,处理的是标签之间的内容。继承的不是TagSupport而是BodyTagSupport
如:
public class MyTag3 extends BodyTagSupport{


@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
//return super.doEndTag();
String body=this.getBodyContent().getString();
HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
JspWriter out=pageContext.getOut();
System.out.println("之前的body:"+body);
body=body.replace("$date",new Date().toString());
System.out.println("之后的body:"+body);
try {
out.print(body);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SKIP_BODY;
}




}


然后创建自定义标签库和之前的一样。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
 <tlib-version>1.0</tlib-version>
 <jsp-version>1.2</jsp-version>
 <short-name>mytag</short-name>
 <tag>
  <name>tag</name>
  <tag-class>com.tag.MyTag1</tag-class>
  <body-content>jsp</body-content>
  <attribute>
  <name>parameter</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
</taglib>
加载到web.xml中
<taglib>
<taglib-uri>/WEB-INF/demo1</taglib-uri>
<taglib-location>/WEB-INF/demo1.tld</taglib-location>
</taglib>


最后在jsp页面上引入并使用:
引入:<%@ taglib prefix="mytag" uri="/WEB-INF/demo1"%>
使用:<ceshi:demo3>date:$date</ceshi:demo3><br/><br/>
    <ceshi:demo3>file:$filename</ceshi:demo3>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值