简介
-标签在jsp页面中被调用
-标签对应一个java处理类,来处理标签逻辑
-好处是在页面中尽量少写代码
标签的接口和类
-jsp所有标签都实现了javax.servlet.jsp.JspTag接口
这个接口有两个子接口:SimpleTag和Tag
标签的分类
-不带属性和标签体的简单标签
-不带标签体,但是有属性的标签
-带标签体,且有属性的标签
标签实例
首先是要创建一个标签对应的java类,以此来实现标签的输出。
这是我的第一个hitag.java,重写了Tag接口
package beandemo;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
public class hitag implements Tag {
private PageContext pageContext;
private Tag tag;
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
String str="hi JSP tag";
JspWriter out=pageContext.getOut();
try {
out.print(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.SKIP_BODY;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
return 0;
}
@Override
public Tag getParent() {
// TODO Auto-generated method stub
return tag;
}
@Override
public void release() {
// TODO Auto-generated method stub
}
@Override
public void setPageContext(PageContext pageContext) {
// TODO Auto-generated method stub
this.pageContext=pageContext;
}
@Override
public void setParent(Tag tag) {
// TODO Auto-generated method stub
this.tag=tag;
}
}
同时建一个.tld文件,这个文件靠xml文件建立,在WEB_INF下新建xml file然后改文件后缀为.tld,同时选择create xml file from a DTD file,然后Select XML Catalog entry,选择DTD JSP Tag Library 1.2就ok了
这个文件为
<?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>test</short-name>
<uri>http://www.bing.com.cn</uri>
<tag>
<name>hitag</name>
<tag-class>beandemo.hitag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
tag标签里有name和tag-class这两个将java文件与jsp结合
接下来是jsp文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.bing.com.cn" prefix="hi" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<hi:hitag/>
</body>
</html>
其实就是主要是在开头的
<%@ taglib uri="http://www.bing.com.cn" prefix="hi" %>
里面。
这样子jsp文件就可以运行啦。
同时下面有一个不通过实现tag接口的,通过继承TagSupport类实习的,同时给一些接口定义了属性,同时有一些和数据库有关的东西也可以一起结合进去。然后下面是所有的java代码
hitag.java
package beandemo;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
public class hitag implements Tag {
private PageContext pageContext;
private Tag tag;
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
String str="hi JSP tag";
JspWriter out=pageContext.getOut();
try {
out.print(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.SKIP_BODY;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
return 0;
}
@Override
public Tag getParent() {
// TODO Auto-generated method stub
return tag;
}
@Override
public void release() {
// TODO Auto-generated method stub
}
@Override
public void setPageContext(PageContext pageContext) {
// TODO Auto-generated method stub
this.pageContext=pageContext;
}
@Override
public void setParent(Tag tag) {
// TODO Auto-generated method stub
this.tag=tag;
}
}
hellotag.java
package beandemo;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class hellotag extends TagSupport{
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
String str="second tag";
try {
pageContext.getOut().print(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doEndTag();
}
}
selectiontag.java
package beandemo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class selecttag extends TagSupport {
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
String str="<select>";/*
str+="<option value=1>CEO</option>";
str+="<option value=2>CFO</option>";
str+="<option value=3>PM</option>";*/
List list=list();
for(int i=0;i<list.size();++i){
Position p=(Position)list.get(i);
str+="<option value="+p.getId()+">"+p.getName()+"</option>";
}
str+="<select/>";
try {
pageContext.getOut().print(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doEndTag();
}
public static List list(){
Position p1=new Position();
p1.setId(1);
p1.setName("CEO");
Position p2=new Position();
p2.setId(2);
p2.setName("CFO");
Position p3=new Position();
p3.setId(3);
p3.setName("PM");
Position p4=new Position();
p4.setId(4);
p4.setName("PM");
List list =new ArrayList();
list.add(p4);
list.add(p3);
list.add(p2);
list.add(p1);
return list;
}
}
Position.java
package beandemo;
public class Position {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
tagwithattribute.java
package beandemo;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class tagwithattribute extends TagSupport{
private int count;
public void setCount(int count){
this.count=count;
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
String str="hello";
for(int i=0;i<count;++i){
try {
pageContext.getOut().println(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return super.doEndTag();
}
}
然后是tld文件
hitag.tld
<?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>test</short-name>
<uri>http://www.bing.com.cn</uri>
<tag>
<name>hitag</name>
<tag-class>beandemo.hitag</tag-class>
<body-content>empty</body-content>
</tag>
<tag>
<name>hellotag</name>
<tag-class>beandemo.hellotag</tag-class>
<body-content>empty</body-content>
</tag>
<tag>
<name>selecttag</name>
<tag-class>beandemo.selecttag</tag-class>
</tag>
<tag>
<name>attr</name>
<tag-class>beandemo.tagwithattribute</tag-class>
<attribute>
<name>count</name> //需要被赋值的参数
<required>true</required> //是否需要强制被赋值
<rtexprvalue>true</rtexprvalue> //是否可用于表达式
</attribute>
</tag>
</taglib>
接下来就是简单的jsp文件了
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.bing.com.cn" prefix="hi" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<hi:hitag/>
<hi:hellotag/>
<hi:selecttag/>
<hi:attr count="10"/>
</body>
</html>