jsp in action

28
JSP in Action continuation

Upload: winter

Post on 17-Feb-2016

33 views

Category:

Documents


0 download

DESCRIPTION

JSP in Action. continuation. JSP’s Tag Extension Mechanism. You can define your own actions to replace lengthy scriptlets By “hiding” functions behind custom tags, you can increase modularity and maintainability of your pages . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: JSP in Action

JSP in Actioncontinuation

Page 2: JSP in Action

JSP’s Tag Extension Mechanism• You can define your own actions to replace lengthy scriptlets• By “hiding” functions behind custom tags, • you can increase modularity and maintainability of your pages.

Page 3: JSP in Action

1. Define Java classes that provide the functionality of the new actions, including the definition of their attributes (e.g., myAttributeName). These classes are called tag handlers.

2. Provide a formalized description of your action elements, so that Tomcat knows how to handle them. For example, you need to specify which actions can have a body and which attributes can be omitted. Such a description is called a tag library descriptor (TLD).

3. In the JSP pages, tell Tomcat that the pages need your tag library and specify the prefix that you want to identify those custom tags with.

Page 4: JSP in Action

• Bodyless Custom Action• <wow:weekday date=“date”/>

Page 5: JSP in Action

Step 1: Define the Tag Handler• A tag handler for a bodyless custom tag is a class that implements the

interfaces java.io.Serializable and javax.servlet.jsp.tagext.Tag. • Remember that to satisfy an interface, you have to implement all the

methods it defines. • To satisfy Serializable, you only need to define a unique identifier, like

this:

static final long serialVersionUID = 1L;

Page 6: JSP in Action
Page 7: JSP in Action
Page 8: JSP in Action
Page 9: JSP in Action

Step 2: Define the TLD

Page 10: JSP in Action
Page 11: JSP in Action

Step 3: Use the Custom Action

Page 12: JSP in Action

Bodied Custom Actions

Page 13: JSP in Action

Step 1: Define the Tag Handler• Bodied actions need to implement an interface,

javax.servlet.jsp.tagex.BodyTag instead of Tag.• Implement doEndTag method,and doAfterBody

<wow:weekdayBody></wow:weekdayBody>

Page 14: JSP in Action
Page 15: JSP in Action

Step 2: Define the TLD

Page 16: JSP in Action

Step 3: Use the Custom Action

Page 17: JSP in Action

Tag Files• Tag files are special JSP files that replace tag handlers written in Java. • There are three directives: tag, attribute, and variable.

Page 18: JSP in Action

Bodyless Tag

Page 19: JSP in Action

• The tag directive of a tag file replaces the page directive of a JSP page, and the attribute directive lets you define an input parameter. • Another simplification is in sending the result to the output, because

in the tag file the implicit variable out makes it unnecessary to invoke pageContext.getOut().

Page 20: JSP in Action
Page 21: JSP in Action
Page 22: JSP in Action

Bodied Tag

Page 23: JSP in Action

• If you omit the attribute var, doBody sends the body’s result to the output;• If you replace var with varReader, the result is stored as a java.io.Reader

object instead of a java.lang.String; • If you add the attribute scope, you can specify var/varReader to be defined

as a request, session, or application attribute, instead of in the page scope.• Know that jsp:invoke is very similar to jsp:doBody but operates on a JSP

fragment instead of the action body. • For example, by writing the following two lines in a tag file

<%@attribute name="fragName" fragment="true"%><jsp:invoke fragment="fragName"/>

Page 24: JSP in Action
Page 25: JSP in Action

The tag Directive

Page 26: JSP in Action

The attribute Directive

Page 27: JSP in Action

The variable Directive

Page 28: JSP in Action

JSTL and EL Next Meeting