How to create Macro / Snippet / HTML Fragment / Named snippet in JSP without using tags / includes / JSPFragments?

This may be a very newbie question, but I did not find anything satisfying

I want to do something like this in JSP (preferably out of the box):

eg. in a file called products.jsp an imaginary implementation that explains what I want

 <x:named-segment name="product"> Product: <strong>${product.name}</strong> <br/> price: ${product.price} </x:named-segment> 

and then use it in different places in the same JSP, it is defined

 <table> <c:forEach var="product" items="${products}"> <tr> <td><x:use-segment name="product"/></td> </tr> </c:forEach> </table> 

I looked at JSP tags and JSP Fragements , but the fragment fragment is just passed from the JSP receiver to the JSP tag, and I want it in one place

The only solution is to skip the JSP tag for this small snippet (or include?)

Did I miss something very basic?

+4
source share
2 answers

If the small piece of text you want in many places is static, I would recommend enabling JSP. However, if the text from the database / Flat file / XML, I would recommend using a special tag. In the example you provided, it seems that you are trying to list the products and their price. This can easily be done in a custom tag.

In your tag class, read the data, create a method that will create HTML tags for the data and return as a string, print a string. Now, a special tag is called in your JSP wherever you need text. Of course, you need to parameterize the tag to determine what to select / display in which place.

NTN

IN

+1
source

I feel your pain @EranMedan, still can't believe this is not a JSP feature. After several years when I wanted it, I wrote my own simple solution to do what you (and I) want: fooobar.com/questions/1300605 / ...

0
source

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


All Articles