Adding a custom JSP tag to your JSP page
I need to create a reusable "chunk" of JSP code
and then add that code to any page where I need it
I am going to create a converter from a java.time.LocalDate
to java.util.Date, so the JSTL
tag will work
steps:
1) I need to create a new tag that looks like:
<%@ tag body-content="empty" pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="date" required="true" type="java.time.LocalDate" %>
<%@ attribute name="pattern" required="false" type="java.lang.String" %>
notice: the attributes, the first of type java.time.LocalDate, the
second of type String.
then my check it see if the pattern string is empty, and apply
a default if is is
finally I will convert the LocalDate to a String, then convert
that String to a java.util.Date for the JSTL library can handle it
2) I will need to save this new tag as a .tag file, and I want to
put it somewhere the Browser cannot see it, but somewhere the JSP
processor has access to, so I create a new folder under
src/main/webapp/WEB-INF and name that folders tags
my file is saved in this folder as localDate.tag
3) Now I need to put this new tag into my JSP code, there are 2 steps
to to this
3.1) add the tag definition to the top of the page:
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
Note the tagdir= points to where I put the tag file
3.2) use this new tag prefix where I have a LocalDate that
I want formatted by the JSTL