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