标签:private att string pos swa new text use catch
第一步:导入jar包 jsp-api-2.2-sources.jar
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency>
第二步,定义一个用来实现标签功能的java类,例如:DateConvert.java
@SuppressWarnings("serial") public class DateConvert extends TagSupport { private String longTime; public String getLongTime() { return longTime; } public void setLongTime(String longTime) { this.longTime = longTime; } @Override public int doStartTag() throws JspException { long l = 0l; if(longTime!=null && !longTime.equals("")){ l = Long.parseLong(longTime); } Date date = new Date(l); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String targetTime = format.format(date); try { super.pageContext.getOut().write(targetTime); super.pageContext.getOut().write("<br/>"); super.pageContext.getOut().write("123123"); } catch (IOException e) { e.printStackTrace(); } return super.doStartTag(); } }
第三步,可在WEB-INF目录下定义一个*.tld文件,例如dateConvert.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>dateConvert</short-name> <uri>/dateConvert</uri> <tag> <name>longStr</name> <tag-class>spittr.tag.DateConvert</tag-class> <body-content>JSP</body-content> <attribute> <name>longTime</name> <required>true</required><!-- 表示该属性,一定要使用 --> <rtexprvalue>true</rtexprvalue><!-- 属性是否能用表达式 --> </attribute> </tag> </taglib>
第四步,在web.xml文件中引用你的*.tld文件
<jsp-config> <taglib> <taglib-uri>/dateConvert</taglib-uri> <taglib-location>./tag/dateConvert.tld</taglib-location> </taglib> </jsp-config>
第五步,在你的页面引入自定义标签库进行使用
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="/dateConvert" prefix="ct" %> <h1>Register</h1> <form method="post" enctype="multipart/form-data"> First Name:<input type="text" name="firstName"/><br/> Last Name:<input type="text" name="lastName"/><br/> Username:<input type="text" name="userName"/><br/> Password:<input type="password" name="password"/><br/> file:<input type="file" name="profilePicture" accept="image/jpeg,image/png,image/gif"><br/> <ct:longStr longTime="1314842011312"></ct:longStr> <input type="submit" value="Register"/> </form>
标签:private att string pos swa new text use catch
原文地址:https://www.cnblogs.com/yifanSJ/p/9217531.html