JavaServer Page

March 11, 2007

Iterating with tags 101 To further illustrate how

Filed under: JSP Tag Libraries — webmaster @ 6:14 pm

Iterating with tags 101 To further illustrate how iteration is accomplished with tags, we ve translated the flow chart in figure 10.1 into (roughly) its Java equivalent. t.doInitBody(); do { // The JSP runtime execute // the tag s body … } while(t.doAfterBody() == BodyTag.EVAL_BODY_TAG); As figure 10.1 and the code fragment show, two methods (table 10.1) take part in implementing the iteration: Table 10.1 Iteration methods JSP method Description doBodyInit() Used to initialize preiteration JSP scripting variables and the tags internal values. For example, if your tag exposes some iterator object as a JSP scripting variable, it will probably use doBodyInit() to export its initial value. doAfterBody() Controls the iteration with its return codes: To continue the iteration, doAfterBody() returns a value of BodyTag.EVAL_BODY_TAG (or IterationTag.EVAL_BODY_AGAIN in JSP1.2).To break the iteration , it returns a value BodyTag.SKIP_BODY. This method is also where we re-export the iterator value (the current value of the property on which we are iterating), and where we write the result of the current iteration into the response. NOTE You can skip the implementation of doBodyInit() and perform its work in doStartTag(). This will not have any effect on performance and may even simplify your tags. Better yet, since doStartTag() is not available in IterationTag, code that does not use it will be easier to port to this new tag. In any case, it is a good idea to separate the iteration handling from doStartTag() so that doStartTag() will only deal with service initialization (e.g., obtaining the object set that we are going to iterate) and doBodyInit() will deal with the initialization of the loop. 10.1.1 Iteration example: SimpleForeachTag Now that you know how to implement iteration in your tags, we will take a look at a sample iterative tag and the code that performs iteration. Our first iteration tag, SimpleForeachTag, will take a tag attribute that specifies a list of strings, walk over the string list, and, one by one, export an iterator object that contains the current string value for that iteration round. The following JSP fragment shows a sample usage of this tag:

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

Iterating With Tags This is obviously quite an

Filed under: JSP Tag Libraries — webmaster @ 11:29 am

Iterating With Tags ” property=”header” index=”< %= name %>“/>

This is obviously quite an improvement. Why should we bother creating special iteration tags when a two-line scriptlet hardly seems demanding for a Java developer? Again, we can t forget that the goal of building custom tag libraries is to make it possible for non-Java developers (presentation/HTML developers) to build complex sites. Though iteration using scriptlets may not be complex for the Java programmer, it does require the JSP developer to: Know how to iterate on different Java types Enumerations, Iterators, arrays, and so forth. To further complicate the situation, iteration methods usually return an Object that the JSP developer will have to cast. Position the curly brackets in the correct location. If the JSP developer forgets a curly bracket, the JSP compilation will fail, usually with a relatively obscure error message. Maintain and debug yet another portion of Java code. As a result, iteration tags are necessary to enhance the effectiveness of our JavaBean tags and to keep our JSPs scriptlet-free. This chapter explores iteration with tags and shows how to build JSP custom tags that perform iteration for us. We ll start with a brief introduction to iterating with custom JSP tags and discuss their design principles; later, we will develop iteration tags to handle cases in which we wish to iterate over Java s common object containers. NOTE In this chapter, you will see the word iterator used in two distinct ways. When we use the generic term, we mean any multivalued object (be it an Array, an implementation of the java.util.Enumeration interface or an implementation of the java.util.Iterator interface). When we mention Iterator we are speaking strictly about the Java interface.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

Iterating With Tags This is obviously quite an

Filed under: JSP Tag Libraries — webmaster @ 11:29 am

CHAPTER 10 Iterating with tags 10.1 Iterating with tags 101 Developing custom JSP tags that iterate over some set of values requires us to work, once again, with the familiar BodyTag interface. The BodyTag interface provides a method call protocol to control the execution of the tag s body which we ll need in order to repeat the tag s body for every value in the JavaBean s indexed property. NOTE In JSP1.2 a new IterationTag interface was defined and we can also create tags using this interface. You can find information regarding the IterationTag later in this chapter. Figure 10.1 shows how a tag can implement iteration using the BodyTag method call protocol. Iteration Initalization t.doInitBody() Reiterating or Termination t.doAfterBody() doAfterBody() returns EVAL_BODY_TAG doAfterBody() returns SKIP_BODY Iteration is done Body Processing The JSP environment Process the body Figure 10.1 Implementing iteration using the BodyTag interface

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

10 Iterating with tags In this chapter

Filed under: JSP Tag Libraries — webmaster @ 5:32 am

CHAPTER 10 Iterating with tags At the end of chapter 8, we used our newly created JavaBean tags to export an Enumeration which was then iterated over with a scriptlet. Let s take another look at this JSP fragment.

” property=”header” index=”< %= name %>“/>

< % } %>

Header Value

As you can see (note the highlighted code), although our JavaBean tags greatly reduce the need for scriptlets, we are still unable to avoid them when working with indexed JavaBean properties that have more than one value. In cases of multivalued properties (Enumerations, arrays, etc.) we typically want to loop through (iterate) and perform a function with each value in the property. Without a tag to handle this iteration, we re left using a scriptlet like the one here. This is unfortunate since we want to be able to provide our JSP authors with the ability to perform common functions on JavaBeans without prior knowledge of Java. Ideally, we d like to offer them a very user-friendly JSP custom tag that would work for iteration. Iteration, especially enumerating some value, can be very declarative, and, as we ve seen, declarative tasks are easily performed with tags. For example, by using iteration tags we can modify the previous JSP fragment:

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

Header Value

10 Iterating with tags In this chapter

Filed under: JSP Tag Libraries — webmaster @ 5:32 am

10 Iterating with tags In this chapter Iterating with tags 101 Universal iteration with tags (iterate anything!) Tag-only presentation of a shopping cart The JSP1.2 IterationTag 302

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

March 10, 2007

300 9.4.1 CHAPTER 9 Posing conditions with tags

Filed under: JSP Tag Libraries — webmaster @ 11:40 pm

Summary author or content developer from concerns over conditional logic so they may focus on presentation. A solution that bases itself on beans should look something like: ” property=”myQuery”> < %-- We are here if the query is true --%> < %--We are here if the query is false --%> This approach keeps the JSP syntax clean and straightforward, while enforcing a tidy separation of the business logic and presentation. The advantages of using Java- Beans to evaluate your conditions make it vastly superior to the previous approach, while satisfying all the requirements of supporting complex, real-world use. 9.5 Summary While it is feasible to implement an easy-to-use conditional tag, representing a condition (even a simple one) in a declarative manner is impossible. Therefore, unless you are going to implement your own condition specification language, you will be better off implementing your condition evaluation in a JavaBean, in which you can leverage all the power of the Java language to pre-process and evaluate your conditions. This approach leaves your tags with the sole task of checking the JavaBean s resulting values and including or excluding content based on the bean s exported values. Once you resolve to never attempt implementing a full-fledged condition specification language, implementing custom conditional tags becomes relatively easy. JSP is well adapted for conditional inclusion (as well as exclusion) of content using the Tag method call protocol and JSP will evaluate the conditioned content with no additional effort on your part. In the next chapter, we will look at how to build tags that support iteration, such as iterating over a list of JavaBean property values. We saw scriptlet code in chapter 8 that performed iteration; now we ll see how to eliminate these scriptlets and replace them with simple to use, flexible custom tags that perform the same functions.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

300 9.4.1 CHAPTER 9 Posing conditions with tags

Filed under: JSP Tag Libraries — webmaster @ 11:40 pm

300 9.4.1 CHAPTER 9 Posing conditions with tags Supporting complex conditions with a condition language Inventing a condition language is a reasonable approach to supporting conditions; in fact, we had a very simple type of condition language in our TestTag. Why not improve on it? For example, assume that our condition can look like this: Some JSP code … One resounding benefit to creating a new condition language is that you can make it as complex and as powerful as you desire. Since you ll have total control over the language, you can support as many operands, functions, or comparisons as you desire by extending it. At this level, defining our own conditional language seems like a good approach. There are, however, several notable drawbacks. Drawbacks of complex condition languages By designing your own condition language you can easily support complicated queries; however, to do so you need first to develop a complex parsing mechanism and then implement all the functionality the language supports, including utility functions (such as Trim() in the previous example). This can prove to be a substantial undertaking. Furthermore, once you are finished with new language implementation, you will have to teach it to whomever will be using it. And if that were not enough, you will probably need to provide mechanisms for JSP authors to debug their conditions. Will you provide an IDE to let developers step into your proprietary implementation? After this analysis, it becomes clear that in order to build a debuggable, feature-rich condition language (and the code to support it) you end up building a system similar to commercial products like ColdFusion. Unless you are planning on selling this tag library commercially and for a nifty price, the effort required to support complex conditions this way is not worth it. There must be a better way to solve the problem with a more standard condition language and less effort. One alternative, it turns out, is to use Java. Supporting complex conditions with JavaBeans JSP already supports Java. To take advantage of this, let s place all our conditions in a JavaBean and query the values of that bean s properties. The JavaBean now contains the complicated Java code that performs the condition, and can leverage all the natural features of the Java language. This approach also makes it very easy to debug the code (since you can simply use your favorite Java IDE), and prevents us from supporting any complex parsing or training JSP authors in our own proprietary language. Though this method requires you to write the beans that contain the conditional logic, this will simply be handled by the business logic developer, freeing the JSP

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

CHAPTER 9 Posing conditions with tags in testing

Filed under: JSP Tag Libraries — webmaster @ 4:06 pm

Improving our advanced condition tags B Points to the User-Agent header. C Informs the wrapper tag that we are going to allow the execution of multiple successful tests By setting the multichoice attribute to true, we indicate that all tests wrapped in this WithTag should be evaluated. For example, if you try to access this JSP fragment from Internet Explorer running on Win98, both the MSIE and 98 containment conditions will come up true and you will see the body of these tests in the response. If you wonder why someone would want to have several successful tests in the same JSP fragment, just picture some JSP code in which you want to adapt yourself to the browser as much as possible. In this case you will need to test the browser s capabilities over and over again. Using the multichoice option can give you this flexibility. D Execution of an if-else logic first test if the header contains MSIE and then check that it does not. E Provides an Integer as a second operand. F Checks if the User-Agent header does not start with a Mozilla/ prefix (most browsers do). Note from these two samples that using our tags is fairly straightforward; the tag attributes make sense and we ve kept the syntax to a minimum. It is also easy, using this library, to test the reference object and specify conditions; but aren t these conditions too elementary for many real-world uses? The next section will discuss this concern. 9.4 Improving our advanced condition tags The tags we ve built do a fair job of handling condition evaluation, so what s missing? The main shortcoming with our tag library is that many real applications have conditions that use more than two operands, which is the maximum our library can handle. In Java we can have conditions that look like: if(s.trim().equals( somevalue ) && someBoolean && otherBoolean) { // do something } Such a condition could not be represented using our current library. Does it mean that we cannot use condition tags in a real-world case? There are a number of possible ways to support these complex conditions within tags. Here are two of them: Invent a complex condition language to be used within the tags and extend the tags we developed to use this language. Have the user of the tags put all of the complex conditions inside a bean and use the bean s attributes to grab the condition s result inside the tags.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

CHAPTER 9 Posing conditions with tags in testing

Filed under: JSP Tag Libraries — webmaster @ 4:06 pm

CHAPTER 9 Posing conditions with tags in testing a boolean value; the conditions we use are eq true and eq false, meaning equals true and equals false. We are not doing a simple if-else (we actually can t), but instead are asking about two negatives. Listing 9.8 Using the conditional tags with a boolean value < % Boolean b = new Boolean(true); %> TRUE FALSE You may wonder about the fact that in listing 9.8 we generated our object in a scriptlet (about the easiest way to pass a value to the tag), but don t let it concern you. We could get this value from anywhere in the JSP environment (e.g., we could get this value from a bean) and our tag would work equally well. Another sample JSP fragment shows a more complex scenario in which the developer grabs the User-Agent header and checks to see what type of client is being served. Listing 9.9 A complex usage of the conditional tags d Yes No
e You are probably using some Windows98 variation.
f What’s that? No Mozilla?

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

CHAPTER 9 Posing conditions with tags is needed

Filed under: JSP Tag Libraries — webmaster @ 10:18 am

The advanced condition tag family false scope false false index false true property false false test book.conditions.TestTag book.conditions.TestTagExtraInfo JSP Pose a condition on the reference object. condition true false As seen in the TLD, both tags are marked as having JSP contents, which tells the JSP environment to evaluate the body. As for tag attributes, the WithTag entry inherits most of its attributes from ReflectionTag and adds just a single new multichoice attribute, instructing the tag as to the condition evaluation policy that is desired. TestTag, on the other hand, is less complex with only a single tag attribute that specifies the condition string. 9.3.4 Our tag library in action We can now take a look at a few JSP fragments that use the tag. You ll recognize the following JSP fragment from earlier in this chapter; and it will prove even more useful based on what we ve just learned. This fragment shows how the tag is employed

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

« Previous PageNext Page »

Powered by Java Web Hosting