<!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>
tld是taglib description 的缩写定制标签拼图中的最后一块是TLD(标签库描述符)文件。创建的每个标签都必须在一个TLD文件中声巧毕隐明,而且这一文件还必须连同标签的Java处理器类一起被部署。正如在前一节中所看到的数歼,servlet容器知道在何处放置了TLD文件,因为已经在web.xml标准servlet配置文件中声明了路径。
定制标签孝厅拼图中的最后一块是TLD(标签库描述符)文件。创建的每个标签都必须在一个TLD文件中声明,而且这一文件还必须连同标签的Java处理器类一起被部署。正如在前一节中所看到的,servlet容器知道在何处放置了TLD文件,因为已经在web.xml标准servlet配置文件中声明了路径。
简单的就是在使用标签时能不能使用表达式来动态指定数据。true是可以,false是不可以。做个简单例子:
定义TLD文件如下:
Java代码
<tag>
<name>selectDetail</name>
<tag-class>com.nms.taglib.SelectDetail_Tag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>id</name>帆咐
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>selectVal</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>collection</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
页面可以这样设置需要的值:
Java代码态物纯
<%
User user = new User()
user.setId(1)
request.setAttribute("user",user)
%>
<html:selectDetail id="" collection="<%="SEX" %>" selectVal="${user.id}"><蚂衫/html:selectDetail>
使用脚本和EL表达式来获取动态的值
如果你将rtexprvalue设置为flase,那么上面的使用会直接报错
必须这样使用:
Java代码
<html:selectDetail id="" collection="SEX" selectVal="男"></html:selectDetail>
我看了一下别人自定义标签,其实吧你全设置为true就可以了,方便使用也省事。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)