JSP Tag Libraries Lec 38 Last Lecture Example
JSP Tag Libraries Lec - 38
Last Lecture Example n We incorporated Java. Beans in “Course Outline” Example n But still have to write java code inside java. jsp & web. jsp n As discussed, JSPs are built for presentation purpose
Last Lecture Example n What if we replace all code of scriptlet with one single line (custom tag) <% Course. DAO course. DAO = new Course. DAO(); …… for ( ……. ) { ……. . } …… %> n <mytag: displaycourse page. Name=“java” />
Contents n What is a Custom Tag ? n Why build Custom Tag? n Types of Tags n Developing Custom Tags n Using Custom Tags n Examples
What is Custom Tag ? n A user defined component to perform certain action. n Provides mechanism for encapsulating complex functionality for use in JSPs. n We have already seen & used many builtin tags n n n <jsp: use. Bean/> <jsp: include/> <jsp: forward/> etc
Advantages n More cleaner separation of processing logic and presentation, than Java Beans n Have access to all the JSP page objects n Can be customized using attributes
Types of Tags
Types of Tags 1. Simple Tag 2. Tag with Attributes 3. Tag with Body
Types of tags 1. Simple Tags n Start and End of tag n No body within tag n No Attributes n For Example: < mytag: hello /> Tag library prefix Tag Name
Types of tags 2. Tags with attributes n Start and End of tag n Attributes within tag n No body enclosed n For Example : < mytag: hello attribute=“value” />
Types of tags 3. Tags with body n Start and End of tag n Body enclosed within tag n Attributes (optional) n For Example : <mytag: hello optional_attributes …. . > some body </mytag: hello>
Building Custom Tags
Building Custom Tags n n We can build custom tag using either of the following specs n JSP 1. 2 n JSP 2. 0 We will use JSP 2. 0
Building Custom Tags - Steps 1. Develop the Tag Handler class 2. Write the Tag Library Descriptor (tld) file 3. Deployment
1 - Tag Handler class n Tag Handler is the java class n Implicitly called when the associated tag is encountered in the JSP n Must implement Simple. Tag interface n Usually extend Simple. Tag. Support class. For example public class My. Tag. Handler extends Simple. Tag. Support { …………. . }
1 - Tag Handler class cont. n do. Tag() n n n method By default does nothing Need to implement / override to code tag’s functionality Invoked when the end element of the tag encountered
1 - Tag Handler class cont. n Implicit objects are available to tag handler class through page. Context object n page. Context object can be obtained using get. Jsp. Context() method. n For example, to retrieve out object Page. Context pc = (Page. Context)get. Jsp. Context(); Jsp. Writer out = pc. get. Out();
2 - Tag Library Descriptor (tld) n XML based document n Specifies information required by the JSP container such as: n n n Tag library version JSP version Tag name Tag handler class name Attribute names etc.
3 - Deployment n Place Tag Handler class in myapp/WEBINF/classes folder of web application n Place TLD file in myapp/WEB-INF/tlds folder
Using Custom Tags
Using Custom Tags n Use “taglib” directive in JSP to refer to the tag library <%@ taglib uri = “TLDFile. Name” prefix = “mytag” %> n Call the tag by its name as defined in TLD n If tag name defined in TLD is hello then we write < mytag: hello /> n Behind the Scenes n Container calls the appropriate Tag Handler n Tag Handler will write the appropriate response back to the page
Building Simple Tag
Building Simple Tag n A simple tag that displays “Hello World” n Approach n Extend Tag Handler class from Simple. Tag. Support class • Override do. Tag() method n Build TLD file n Deploy
Example Code Building Simple Tag (displays “Hello World” )
Building Tag with Attributes
Building Tags with Attributes n n E. g. <mytag: hello attribute=“value” /> To handle attributes, need to add n n n Instance variables and Corresponding setter methods Container call these setter methods n Passed the custom tag attribute’s value as arguments
Building Tag with Attribute n (cont. ) Example: n We will modify our Course Outline Example to incorporate tags n Based on attribute value, tag will display the respective course outline
Example Code Building Tag with Attribute (Modifying Course. Outline Example)
- Slides: 28