web 工程,新建java类:
[java] package leon.webtest.tags;
import java.text.simpledateformat;
import java.util.date;
import javax.servlet..jspexception;
import javax.servlet.jsp.jspwriter;
import javax.servlet.jsp.tagext.tagsupport;
public class datetime extends tagsupport {
private static final long serialversionuid = 1l;
public int dostarttag() throws jspexception {
try {
jspwriter out = this.pagecontext.getout();
simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
out.println("");
out.println(sdf.format(new date()));
out.println("");
} catch (exception e) {
throw new jspexception(e.getmessage());
}
return skip_body;
}
}
package leon.webtest.tags;
import java.text.simpledateformat;
import java.util.date;
import javax.servlet.jsp.jspexception;
import javax.servlet.jsp.jspwriter;
import javax.servlet.jsp.tagext.tagsupport;
public class datetime extends tagsupport {
private static final long serialversionuid = 1l;
public int dostarttag() throws jspexception {
try {
jspwriter out = this.pagecontext.getout();
simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
out.println("");
out.println(sdf.format(new date()));
out.println("");
} catch (exception e) {
throw new jspexception(e.getmessage());
}
return skip_body;
}
}
新建tld文件:web-inf/tld/webtest-tags.tld
[html]
xsi:schemallocation="http://www.51sjk.com/upload/articles/1/0/248/248955_20210624001200531.jpg web-jsptaglibrary_2_0.xsd"
version="2.0">
xsi:schemallocation="http://www.51sjk.com/upload/articles/1/0/248/248955_20210624001200531.jpg web-jsptaglibrary_2_0.xsd"
version="2.0">
web.xml中的配置:
[html]
xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://www.51sjk.com/upload/articles/1/0/248/248955_20210624001200531.jpg http://www.51sjk.com/upload/articles/1/0/248/248955_20210624001200531.jpg/web-app_2_4.xsd">
xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://www.51sjk.com/upload/articles/1/0/248/248955_20210624001200531.jpg http://www.51sjk.com/upload/articles/1/0/248/248955_20210624001200531.jpg/web-app_2_4.xsd">
jsp中的使用:
首先是导入:
<%@ taglib uri="/mytags" prefix="leon" %>
使用标签:
加强版:需要显示出来的时间是动态的,像时钟一样会跳秒的。