File tree Expand file tree Collapse file tree 8 files changed +201
-0
lines changed
java/com/itbulls/learnit/onlinestore/web/tags Expand file tree Collapse file tree 8 files changed +201
-0
lines changed Original file line number Diff line number Diff line change 3232 <artifactId >online-store.core</artifactId >
3333 <version >${project.version} </version >
3434 </dependency >
35+
36+ <dependency >
37+ <groupId >org.apache.tomcat</groupId >
38+ <artifactId >tomcat-jsp-api</artifactId >
39+ <version >10.0.0-M10</version >
40+ <scope >provided</scope >
41+ </dependency >
42+
3543 </dependencies >
3644
3745 <build >
Original file line number Diff line number Diff line change 1+ package com .itbulls .learnit .onlinestore .web .tags ;
2+
3+ import java .io .IOException ;
4+ import java .util .LinkedHashMap ;
5+ import java .util .Map ;
6+
7+ import jakarta .servlet .jsp .JspException ;
8+ import jakarta .servlet .jsp .JspWriter ;
9+ import jakarta .servlet .jsp .tagext .DynamicAttributes ;
10+ import jakarta .servlet .jsp .tagext .SimpleTagSupport ;
11+
12+ public class DynamicAttributesTagHandler extends SimpleTagSupport implements DynamicAttributes {
13+
14+ private Map <String , Object > dynamicAttributes = new LinkedHashMap <>();
15+
16+ @ Override
17+ public void setDynamicAttribute (String uri , String localName , Object value ) throws JspException {
18+ dynamicAttributes .put (localName , value );
19+ }
20+
21+ @ Override
22+ public void doTag () throws JspException , IOException {
23+ JspWriter out = getJspContext ().getOut ();
24+ out .println ("<ul>" );
25+ for (Map .Entry <String , Object > entry : dynamicAttributes .entrySet ()) {
26+ out .println ("<li style=\" color:" + entry .getValue () + "\" >"
27+ + entry .getKey () + "</li>" );
28+ }
29+ out .println ("</ul>" );
30+ }
31+
32+
33+
34+ }
Original file line number Diff line number Diff line change 1+ package com .itbulls .learnit .onlinestore .web .tags ;
2+
3+ import java .io .IOException ;
4+ import java .util .Collection ;
5+
6+ import jakarta .servlet .jsp .JspException ;
7+ import jakarta .servlet .jsp .JspWriter ;
8+ import jakarta .servlet .jsp .tagext .SimpleTagSupport ;
9+
10+ public class HelloTagHandler extends SimpleTagSupport {
11+ private String message ;
12+ private Collection <Object > coll ;
13+
14+ public void setMessage (String msg ) {
15+ this .message = msg ;
16+ }
17+
18+ public void setColl (Collection <Object > coll ) {
19+ this .coll = coll ;
20+ }
21+
22+ @ Override
23+ public void doTag () throws JspException , IOException {
24+ JspWriter out = getJspContext ().getOut ();
25+ if (coll != null ) {
26+ out .println ("<p>Hello from Custom Tag!" + " Value of 'message' attribute: \" "
27+ + message + "\" and size of \" coll\" attribue is " + coll .size () + "</p>" );
28+ } else {
29+ out .println ("<p>Hello from Custom Tag!" + " Value of 'message' attribute: \" "
30+ + message + "\" </p>" );
31+ }
32+
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ <taglib>
2+ <tlib-version>1.0</tlib-version>
3+ <jsp-version>3.0</jsp-version>
4+ <short-name>Example TLD</short-name>
5+
6+ <tag>
7+ <name>helloTag</name>
8+ <tag-class>com.itbulls.learnit.onlinestore.web.tags.HelloTagHandler
9+ </tag-class>
10+ <body-content>empty</body-content>
11+ <attribute>
12+ <name>message</name>
13+ </attribute>
14+ <attribute>
15+ <name>coll</name>
16+ <type>java.util.Collection</type>
17+ <rtexprvalue>true</rtexprvalue>
18+ </attribute>
19+ </tag>
20+
21+ <tag>
22+ <name>coloredListItems</name>
23+ <tag-class>com.itbulls.learnit.onlinestore.web.tags.DynamicAttributesTagHandler
24+ </tag-class>
25+ <body-content>empty</body-content>
26+ <dynamic-attributes>true</dynamic-attributes>
27+ </tag>
28+
29+ </taglib>
Original file line number Diff line number Diff line change 1+ <div style="width:100%;height:50px;background:grey;text-align:center">
2+ My Custom Footer
3+ </div>
Original file line number Diff line number Diff line change 1+ <%@ tag language =" java" pageEncoding =" UTF-8" %>
2+ <%@ taglib prefix =" c" uri =" http://java.sun.com/jsp/jstl/core" %>
3+
4+ <%@ attribute name =" footerStyle" required =" true" %>
5+ <%@ attribute name =" footerRegular" fragment =" true" %>
6+ <%@ attribute name =" footerContactForm" fragment =" true" %>
7+
8+ <div >
9+ <div >
10+ <jsp:doBody />
11+ </div >
12+ <c:if test =" ${ footerStyle eq ' regular' } " >
13+ <div >
14+ <jsp:invoke fragment =" footerRegular" />
15+ </div >
16+ </c:if >
17+
18+ <c:if test =" ${ footerStyle eq ' contact-form' } " >
19+ <div >
20+ <jsp:invoke fragment =" footerContactForm" />
21+ </div >
22+ </c:if >
23+ </div >
Original file line number Diff line number Diff line change 1+ <%@ taglib uri =" http://java.sun.com/jsp/jstl/core" prefix =" c" %>
2+ <%@ attribute name =" coll" type =" java.util.Collection" required =" true" %>
3+
4+ <table border =" 1" >
5+ <c:forEach items =" ${ coll } " var =" item" >
6+ <tr >
7+ <td >
8+ ${ item }
9+ </td >
10+ </tr >
11+ </c:forEach >
12+ </table >
Original file line number Diff line number Diff line change 1+ <%@ taglib prefix =" g" tagdir =" /WEB-INF/tags/general" %>
2+ <%@ taglib prefix =" c" uri =" http://java.sun.com/jsp/jstl/core" %>
3+ <%@ taglib prefix =" custom" uri =" /WEB-INF/custom.tld" %>
4+ <!DOCTYPE html>
5+ <html >
6+ <head >
7+ <meta charset =" UTF-8" >
8+ <title >JSP Tags Demo</title >
9+ </head >
10+ <body >
11+
12+ <g:footer />
13+
14+ <p >==========</p >
15+
16+ <c:set var =" arr" value =" ${ [' Apple' , ' Banana' , ' Orange' ] } " />
17+ <g:printArray coll =" ${ arr } " />
18+
19+ <p >==========</p >
20+
21+ <custom:helloTag />
22+ <custom:helloTag message =" test message" />
23+ <custom:helloTag message =" test message" coll =" ${ arr } " />
24+
25+ <p >==========</p >
26+
27+ <g:fragment-demo footerStyle =" regular" >
28+ <jsp:attribute name =" footerRegular" >
29+ This is regular footer fragment
30+ </jsp:attribute >
31+ <jsp:attribute name =" footerContactForm" >
32+ This is footer with contact form fragment
33+ </jsp:attribute >
34+ <jsp:body >
35+ This is body of JSP Custom Tag
36+ </jsp:body >
37+ </g:fragment-demo >
38+
39+ <p >==========</p >
40+
41+ <g:fragment-demo footerStyle =" contact-form" >
42+ <jsp:attribute name =" footerRegular" >
43+ This is regular footer fragment
44+ </jsp:attribute >
45+ <jsp:attribute name =" footerContactForm" >
46+ This is footer with contact form fragment
47+ </jsp:attribute >
48+ <jsp:body >
49+ This is body of JSP Custom Tag
50+ </jsp:body >
51+ </g:fragment-demo >
52+
53+ <p >==========</p >
54+
55+ <custom:coloredListItems item1 =" red" item2 =" green" item3 =" orange" />
56+
57+ </body >
58+ </html >
You can’t perform that action at this time.
0 commit comments