如何在JavaWeb程序中使用自定义标签
3个回答
2016-11-19 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
tld定义格式
[java] view plain copy print?
<?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转换(这个深入的含义不太了解)-->
<attribute><!---这里表示的是这个tag的一个参数-->
<name>cmsform</name><!--这个参数的名字-->
<required>true</required><!--是否是必填选项-->
<rtexprvalue>true</rtexprvalue><!--这个参数的值是否可以写入,换句话说 就是这个参数是否可以动态赋值-->
</attribute>
</tag>
</taglib>
定义Tag对应类
此类必须重写doStartTag以及doEndTag方法
[java] view plain copy print?
/**
*
*/
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;
}
}
在web.xml中加入taglib对应文件配置
如:
[java] view plain copy print?
<taglib>
<taglib-uri>http://mytag.sf.net</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>
</taglib>
这样就表示了http://mytag.sf.net对应WEB-INF/mytag.tld文件
在Jsp页面中引用
如:
<%@ taglib uri="http://mytag.sf.net" prefix="myTag"%>
在Jsp页面中使用
<myTag:exname1><myTag:exname1>
示例:
定义myTag.tld标签文件
[java] view plain copy print?
<?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>
定义标签类
[java] view plain copy print?
/**
*
*/
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添加配置
[java] view plain copy print?
<taglib>
<taglib-uri>http://mytag.sf.net</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>
</taglib>
测试页面
[java] view plain copy print?
<%@ 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>
[java] view plain copy print?
<?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转换(这个深入的含义不太了解)-->
<attribute><!---这里表示的是这个tag的一个参数-->
<name>cmsform</name><!--这个参数的名字-->
<required>true</required><!--是否是必填选项-->
<rtexprvalue>true</rtexprvalue><!--这个参数的值是否可以写入,换句话说 就是这个参数是否可以动态赋值-->
</attribute>
</tag>
</taglib>
定义Tag对应类
此类必须重写doStartTag以及doEndTag方法
[java] view plain copy print?
/**
*
*/
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;
}
}
在web.xml中加入taglib对应文件配置
如:
[java] view plain copy print?
<taglib>
<taglib-uri>http://mytag.sf.net</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>
</taglib>
这样就表示了http://mytag.sf.net对应WEB-INF/mytag.tld文件
在Jsp页面中引用
如:
<%@ taglib uri="http://mytag.sf.net" prefix="myTag"%>
在Jsp页面中使用
<myTag:exname1><myTag:exname1>
示例:
定义myTag.tld标签文件
[java] view plain copy print?
<?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>
定义标签类
[java] view plain copy print?
/**
*
*/
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添加配置
[java] view plain copy print?
<taglib>
<taglib-uri>http://mytag.sf.net</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>
</taglib>
测试页面
[java] view plain copy print?
<%@ 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>
展开全部
直接用,前提是你已经定义好了标签
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2018-08-01 · 【免费测试,验证码5秒必达】
北京巴卜技术有限公司
北京巴卜技术有限公司(以下简称巴卜)是具备国际水准的移动商务平台技术和应用方案提供商。自成立以来,巴卜始终 致力于为国内外企业提供具备国际技术水准的移动商务平台及运营服务。
向TA提问
关注
展开全部
额,你这不是在JSP页面上执行的啊。如果要用JSP调用自定义的类的话,可以这样的:
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询