Iterating with tags 101 To further illustrate how
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