Formatting dynamic content on a JSP
The sample below shows you how to create a loop and then loop over the results of a rule firing in a content spot.
- This sample file, named example.jsp, starts with typical beginning html tags
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <TITLE>example.jsp</TITLE> </HEAD> <BODY>- Now tell the JSP that you will be using a content spot, which is also called a java bean. The class parameter is the fully qualified class name, and id is the name of your content spot. It helps to give a name to the id that is similar to the class name for easy identification.
<jsp:useBean class="sample10.contentspots.GetLatestNewsSpot" id="getLatestNewsSpot"/>- Next pass the request object to the Resource Engine. To do so, call the following method on the content spot that has been instantiated.
<%getLatestNewsSpot.setRequest(request);%>- The content spot is now ready to be used. Place the following line of code somewhere in the HTML of the page close to the content that's being returned. This next line is what displays the "blue spot" whenever the JSP is previewed from the authoring environment. When the JSP is not being previewed, the following line does nothing. The user can modify the rule that creates the content during preview by clicking on the "blue spot."
<%=getLatestNewsSpot.getPreviewAnchorTag()%>- In the lines below, a try block is included to catch any exceptions and an array, _a0 of type sample10.News is created. sample10.News is the class name of the resource that is returned by the content spot that has already been defined.
The getRuleContent() method will return an array of Resource Objects. The array is determined by the rule mapped to the content spot. When creating a content spot, specify a Resource Type.
The array,_a0, is populated with the results of the call to the getRuleContent() method on your content spot. Then another variable, _p0, is created to represent a single instance of your resource that is returned by the content spot. Finally, the first instance in the _a0array is assigned to _p0. If there are no instances in _a0, then the assignment will throw an exception, which is caught and described below in step 9.
<% try { sample10.News[] _a0 = getLatestNewsSpot.getRuleContent(); sample10.News _p0 = _a0[0]; // throws an exception if empty. %>- At this point, your content spot has returned at least one result. Any HTML between this step and the next one will be displayed exactly once, before you begin to loop over your returned content. In this case, a table has been created and its body begun. A table header could be created here too.
<TABLE> <THEAD> <TR> <TD>Story ID</TD> <TD>Headline</TD> <TD>Author</TD> </TR> <THEAD> <TBODY>- With any header/initialization complete, you begin by looping over your result set. The for loop below begins that looping.
<% for (int _i0 = 0; ; ) { %>- Any content between this step and the next will be displayed during each iteration of the loop, once for each piece of content that is returned from the content spot. In this example, a table row with three cells is output, pulling out one field from the resource in each cell. These methods must be defined on the Resource Class. To find out the appropriate methods, you may view the Resource Class created by the Wizards and look at the outline view. Any public methods can be accessed.
<TR> <TD><%=_p0.getId()%></TD> <TD><%= _p0.getHeadline()%></TD> <TD><%= _p0.getAuthor()%></TD> </TR>- In the section below, the loop counter increments as pieces of content are returned from the content spot and the try statement attempts to grab another result from the original result set stored in the _a0 array. If there are no more results in the array, then a java.lang.ArrayIndexOutOfBoundsException is thrown but is caught in the following catch block. A break terminates the loop.
<% _i0++; try { _p0 = _a0[_i0]; } catch (java.lang.ArrayIndexOutOfBoundsException _e0) { break; } } %>- This section is executed when the for loop ends. This is analogous to the above header section after all the results have been displayed from the result set that the content spot returned.
</TBODY> </TABLE>- The following line catches any java.lang.ArrayIndexOutOfBoundsException that may have been thrown in thetry statement found in the above code before the header. When an exception is caught, it should indicate that no results were returned from the content spot.
<% } catch (java.lang.ArrayIndexOutOfBoundsException _e0) { %>- Now give some indication that no results were returned. Any content here is displayed only if no results came back from the content spot.
Sorry, there are no News items to display.- To complete your personalized JSP, add one final } to close out the catch block and close the remaining HTML tags.
<% } %> </BODY> </HTML>
Back