267 pdfsam Foundations Of Ajax (2005)

244 CHAPTER 8 ■ PUTTING IT ALL TOGETHER Listing 8-10 shows the contents of the newsItemDetail.jsp file. All this JSP i...

1 downloads 44 Views 26KB Size
244

CHAPTER 8 ■ PUTTING IT ALL TOGETHER

Listing 8-10 shows the contents of the newsItemDetail.jsp file. All this JSP is doing is iterating over all the news items, and for each news item, the JSP creates a link for the news item and places the link within its own div tag. Listing 8-10. newsItemDetail.jsp <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> By now you can see how beneficial it is to keep the code organized into separate JSP files. Each JSP file represents a focused unit of work that you can easily resuse by including it in other JSP files, which eases the maintenance burden when it comes time to make changes. You can reuse the newsItemDetail.jsp file whenever the browser makes an Ajax request to update the news items. You don’t need to redraw the entire component. The only thing that you need to update is the contents of the div element that houses the list of news stories. That’s where the newsItemsAjax.jsp file comes in. The newsItemsAjax.jsp file is the JSP that is used when the browser submits an Ajax request asking for the news items to be updated. This file is interested only in updating the news items instead of redrawing the entire news items component. Like the weather forecast example, it will use the Taconite framework, but this time it will avoid using Taconite’s JSP tag library in order to demonstrate how the Taconite framework can be used with any server-side technology. Listing 8-11 shows the newsItemsAjax.jsp file. Since this example does not use any of Taconite’s custom JSP tags, you must manually set the Content-Type header of the response to text/xml. Note the similarities to Listing 8-8, which is the actual XML generated by Taconite’s JSP tag library. Listing 8-11. newsItemsAjax.jsp <%@page contentType="text/xml"%> <%@include file="newsItemsDetail.jsp"%>