Check if JSP fragment is installed

Have I stumbled upon the next tutorial of JSP tricks to simplify template creation? to use JSP to create page templates (how have I missed this for so long?!?), however after some searches I can’t figure out how (or if possible) to check if the JSP fragment is installed.

Here is an example of what I'm trying to do:

I have a template called default.tag . It has 2 JSP attributes defined as follows:

 <%@attribute name="title" fragment="true" required="true" %> <%@attribute name="heading" fragment="true" %> 

Then in the page code, I have a page <title> element set to <jsp:invoke fragment="title" /> . Then, later on the page, I have the following:

 <c:choose> <c:when test="${true}"> <jsp:invoke fragment="heading" /> </c:when> <c:otherwise> <jsp:invoke fragment="title" /> </c:otherwise> </c:choose> 

Where I have <c:when test="${true}"> , I want to check if the heading fragment was set to display it, but if not, the title fragment is used by default.

+8
jsp
source share
1 answer

After I joked a bit, I'm going to answer my question. It turns out that the name assigned to attribute actually becomes a variable. Therefore, I can do the following:

 <c:choose> <c:when test="${not empty heading}"> <jsp:invoke fragment="heading" /> </c:when> <c:otherwise> <jsp:invoke fragment="title" /> </c:otherwise> </c:choose> 

That was all I needed to do. Looks like I was just trying to make it harder than necessary!

+11
source share

Source: https://habr.com/ru/post/650381/


All Articles