如何在JavaWeb程序中使用tld文件
展开全部
tld定义格式
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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版本库-->
<jsp-version>1.2</jsp-version>
<short-name>Cms Tag</short-name>
<description><!--标签描述--->
A simple appbase tag library
</description>
<tag>
<name>page</name><!--tag的名字-->
<tag-class>com.cms.common.tag.PageTag</tag-class><!--tag对应的java类的名字-->
<body-content>empty</body-content>
<!--关于body-content 有三个值可选;empty:标签体必须为空;jsp:标签由其他jsp元素组成比如标签中含有<%=attributeName%>的jsp元素,那么此时body-content的值就是实际attributeName传入的值;tagdependent:有标签解释不带jsp转换(这个深入的含义不太了解)如果想深入交流技术可以加群前面435中间416最后141组合到一起就可以找到了-->
<attribute><!---这里表示的是这个tag的一个参数-->
<name>cmsform</name><!--这个参数的名字-->
<required>true</required><!--是否是必填选项-->
<rtexprvalue>true</rtexprvalue><!--这个参数的值是否可以写入,换句话说 就是这个参数是否可以动态赋值-->
</attribute>
</tag>
</taglib>
2、定义Tag对应类
此类必须重写doStartTag以及doEndTag方法
/**
*
*/
package com.cms.common.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* @author louisliao
*
*/
public class DemoViewTag extends TagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String cmsform = "";
public String getCmsForm() {
return cmsform ;
}
public void setCmsForm(String cmsform ) {
this.cmsform = cmsform ;
}
/**
*
*/
public DemoViewTag() {
// TODO Auto-generated constructor stub
}
public int doStartTag()
{
return super.SKIP_BODY;
}
public int doEndTag() throws JspException
{
JspWriter writer=pageContext.getOut();
try {
writer.print("这是我的标签示例<br/>"+"cmsform :"+this.cmsform);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return super.EVAL_PAGE;
}
}
3、在web.xml中加入taglib对应文件配置 如:
<taglib>
<taglib-uri>http://mytag.sf.net</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>
</taglib>
示例:
定义myTag.tld标签文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>MyJSPTag Library</shortname>
<uri>http://mytag.sf.net</uri>
<info>我的示例标签库</info>
<tag>
<name>demo.Viewport</name>
<tagclass>com.myapp.web.tag.DemoViewTag</tagclass>
<bodycontent>JSP</bodycontent>
<info>demo.Viewport标签</info>
<attribute>
<name>northTitle</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>westTitle</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
定义标签类
/**
*
*/
package com.myapp.web.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* @author louisliao
*
*/
public class DemoViewTag extends TagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String northTitle = "";
private String westTitle = "";
public String getNorthTitle() {
return northTitle;
}
public void setNorthTitle(String northTitle) {
this.northTitle = northTitle;
}
public String getWestTitle() {
return westTitle;
}
public void setWestTitle(String westTitle) {
this.westTitle = westTitle;
}
/**
*
*/
public DemoViewTag() {
// TODO Auto-generated constructor stub
}
public int doStartTag()
{
return super.SKIP_BODY;
}
public int doEndTag() throws JspException
{
JspWriter writer=pageContext.getOut();
try {
writer.print("这是我的标签示例<br/>westTitle:"+this.westTitle+"<br/>northTitle:"+this.northTitle);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return super.EVAL_PAGE;
}
}
web.xml添加配置
<taglib>
<taglib-uri>http://mytag.sf.net</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>
</taglib>
测试页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://mytag.sf.net" prefix="myTag"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'tagtldDemo.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<myTag:demo.Viewport northTitle="南" westTitle="西"></myTag:demo.Viewport>
</body>
</html>
2016-08-08 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
这是自定义JSP标签的配置文件,在JavaWeb程序中使用的方法有2种:
1)在web.xml中统一配置
<taglib>
<taglib-location>标签配置文件的物理地址</taglib-location>
<taglib-uri>标签配置文件的调用别名</taglib-uri>
</taglib>
2)JSP中引入标签库
<%@ taglib uri="标签配置文件的物理地址"
prefix="调用别名"%>
1)在web.xml中统一配置
<taglib>
<taglib-location>标签配置文件的物理地址</taglib-location>
<taglib-uri>标签配置文件的调用别名</taglib-uri>
</taglib>
2)JSP中引入标签库
<%@ taglib uri="标签配置文件的物理地址"
prefix="调用别名"%>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询