在现在的开发平台上发现有自己定义的标签,于是在空闲时间学习了一下自定义标签,并且做了一个简单的ajax校验的text标签,废话少说,看下面的步骤和代码。
一. jsp页面包含的内容
<%@taglib uri="http://com.zenglx.dream" prefix="my"%>
<!--由于用到了jquery的ajax方法所以引入jquery包-->
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<!-- validateClazz是校验的自定义类,methdo的是定义的方法 -->
用户名:<my:text id="ddddd" name="aaa" method="validate" validateClazz="com.test.tld.MyValidate"/>
用户名2:<my:text validateClazz="com.test.tld.MyValidate2" name="cccc" method="validate" id="aaa"/>
二.tld文件的编写
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>myself </description>
<display-name>mytest</display-name>
<tlib-version>1.0</tlib-version>
<short-name>my</short-name>
<uri>http://com.zenglx.dream</uri>
<tag>
<name>text</name>
<tag-class>com.test.tld.MyText</tag-class>
<body-content>empty</body-content>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>Custom method</description>
<name>method</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>Custom class</description>
<name>validateClazz</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
三.web.xml的配置
<jsp-config>
<taglib>
<taglib-uri>http://com.zenglx.dream</taglib-uri>
<taglib-location>/WEB-INF/tld/myTest.tld</taglib-location>
</taglib>
</jsp-config>
四.处理标签java类的编写
package com.test.tld;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class MyText extends SimpleTagSupport {
private String id;
private String name;
private String method;
private String validateClazz;
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setMethod(String method) {
this.method = method;
}
public void setValidateClazz(String validateClazz) {
this.validateClazz = validateClazz;
}
@Override
public void doTag() throws JspException, IOException {
JspContext jc = this.getJspContext();
PageContext pc = (PageContext) jc;
//String count = (String) pc.getAttribute("count");
HttpServletRequest request = ((HttpServletRequest) pc.getRequest());
//获得项目路径
String path = request.getContextPath();
//获得项目在机器上的真是路径
//String realPath = pc.getServletContext().getRealPath("/");
JspWriter jw = jc.getOut();
jw.println("<input type='text' id='" + id + "' name='" + name
+ "'/><span id='" + id + "s'></span>");
jw.println("<br/>");
jw.println("<script type='text/javascript'>");
jw.println("$(function(){" + "$('#" + id + "').focus(function(){"
+ "$('#" + id + "s').text('') " + "});" + "$('#" + id
+ "').blur(function(){" + "var value=$('#" + id + "').val();"
+ "$.ajax({" + "cache: false," + "async: true,"
+ "type:'POST'," + "data: {textData: value,validateClazz:'"
+ validateClazz + "',method:'" + method + "'}," + "url:'"
+ path + "/validateData'," + "success: function(info) {"
+ " $('#" + id + "s').css('color','red');" + " $('#" + id
+ "s').text(info);" + "}});" + "})});");
jw.println("</script>");
jw.flush();
}
}
五.校验servlet的编写,用到了反射机制完成方法的调用
package com.test.view;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ValidateDataServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value = request.getParameter("textData");
String validateClazz = request.getParameter("validateClazz");
String vlidateMethod = request.getParameter("method");
try {
//反射得到字节码文件
Class clazz = Class.forName(validateClazz);
//得到自定义类
Object o = clazz.newInstance();
//得到自定义方法
Method m = clazz.getMethod(vlidateMethod,String.class);
//设置私有方法权限
m.setAccessible(true);
//运行方法获取返回值
String info = (String) m.invoke(o, value);
//将返回值写入页面
response.getWriter().write(info);
}catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
六.自己在标签中定义的类和方法的编写
package com.test.tld;
public class MyValidate {
public String validate(String value) {
//自定义的java校验类
String info = null;
if(null!=value&&("1234").equals(value)) {
info = "恭喜你用户名可用!";
} else {
info = "对不起,用户名不可用";
}
return info;
}
}
package com.test.tld;
public class MyValidate2 {
public String validate(String value) {
//自定义的java校验类
String info = null;
if(null!=value&&("1111").equals(value)) {
info = "恭喜你用户名可用!";
} else {
info = "对不起,用户名不可用";
}
return info;
}
}