从jsp1.1开始,jsp就可以使用自定义标签了。不仅实现了代码的重用,而且代码更加简洁了。
一个tag对应一个java类,该类必须继承TagSupport 或者 BodyTagSurport,这两个类提供了一些方法,可以方便的操作和页面之间的操作。
package Test07.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts2.ServletActionContext;
public class InputTag extends TagSupport{
/**
* serialVersionUID
*/
private static final long serialVersionUID = 4382050238149459293L;
private static final String num = "num";
private static final String str = "str";
private String name;
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
/**
* 当jsp容器遇到自定义标签的结束标志,执行该方法,通过返回值来确定后续流程
*/
@Override
public int doEndTag() throws JspException {
// this.pageContext.setAttribute("input", name);
ServletActionContext.getServletContext().setAttribute("input", name);
//SKIP_PAGE:作为doEndTag()的返回值,表示跳过页面中标签后剩下的jsp程序代码
//EVAL_PAGE:作为doEndTag()的返回值,表示继续执行页面中标签后的jsp程序代码
return EVAL_PAGE;
}
/**
* 当jsp容器遇到自定义标签的起始标志,执行该方法,通过返回值来确定执行流程
*/
@Override
public int doStartTag() throws JspException {
boolean isStop = false;
if(num.equals(type)){
System.out.println("type is number!");
}else if(str.equals(type)){
System.out.println("type is string!");
}else{
System.out.println("type is not right!");
isStop = true;
}
// 判断是否继续
if(isStop){
return SKIP_BODY;//SKIP_BODY:作为doStartTag()返回值,表示忽略标签体,不执行
}
//EVAL_BODY_INCLUDE:作为doStartTag()的返回值,表示正常的执行标签体中的内容
return EVAL_BODY_INCLUDE;
}
}
主要可以通过重写doStartTag() 和 doEndTag() 方法,就可以实现基本的操作了。
tld中的属性,和java类的属性是一一对应的。
编写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>ZeTag</short-name>
<uri>http://www.zen.com.cn/ZeTag</uri> <!-- 这个是 uri 而不是 url -->
<tag>
<name>input</name>
<tag-class>Test07.tag.InputTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>type</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
在web.xml中配置tag:
<jsp-config>
<taglib>
<taglib-uri>http://www.zen.com.cn/ZeTag</taglib-uri>
<taglib-location>/WEB-INF/ZeTag.tld</taglib-location>
</taglib>
</jsp-config>
前台jsp页面:导入taglib
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.zen.com.cn/ZeTag" prefix="z"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<% String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆页面</title>
</head>
<body>
<form action="test07/login" id="form" name="form" method="post">
账号:<input id="userName" name="userName" type="text">
密码:<input name="userPassword" type="password">
<input type="button" οnclick="check()" value="提交">
<!-- <input type="hidden" id="userName" id="userName" value=""> -->
<z:input name="test1" type="str"/>
</form>
</body>
<script type="text/javascript" language="javascript">
function check(){
var name = document.getElementById("userName");
//alert(name.value);
if(name.value == ''){
alert('请输入用户名!');
document.getElementById("userName").focus();
return false;
}
document.form.submit();
}
</script>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.zen.com.cn/ZeTag" prefix="z" %>
<!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=UTF-8">
<title>登陆成功</title>
</head>
<body>
${userName},欢迎登陆!
<hr>
测试tag
<z:out value="input"/><br>
<z:out value="test"/>
</body>
</html>