Refreshing Data

As you have learned from the previous sections, you can use the dataset tag to retrieve XML data. But there is more. As you will witness shortly, dataset can also refresh the data.

Consider the LZX application in Listing 8.9. This application displays the server current time and you can click a button to force it to load new data.

Listing 8.9. Submitting user input
<canvas width="340">
    <simplelayout axis="y"/>
    <dataset name="myDataset"
            request="true" src="http://localhost:8080/lps-
       4.0.x/app08/getTime.jsp"/>
    <text width="340" datapath="myDataset:/serverTime/text()"/>
    <button onclick="myDataset.doRequest()">Update Time</button>
</canvas>

It uses the dataset tag to download data from the server. To be precise, it sends an HTTP request to this JSP page:

http://localhost:8080/lps-4.0.x/test/getTime.jsp

Note

If you’re using a different version of OpenLaszlo, change the URL above accordingly.


To test the application, you must have the getTime.jsp page in the same directory as the application in Listing 8.9. The getTime.jsp page is presented in Listing 8.10.

Listing 8.10. The getTime.jsp page
<serverTime>
<%
    out.println(new java.util.Date());
%>
</serverTime>

The JSP page generates the following XML:

<serverTime>
    currentTime</serverTime>

where currentTime is the current time on the server.

You can use this URL to test the LZX application in Listing 8.9.

http://localhost:8080/lps-4.0.x/app08/submitTest1.lzx

Figure 8.6 shows how the application looks like.

Figure 8.6. Refreshing data


In addition, you can also use the ondata event of the dataset tag to get notification when the data arrives. For example, you can display a splash screen that begs the user’s patience and then hides it when the data arrives.

The LZX application in Listing 8.11 demonstrates the use of ondata.

Listing 8.11. Using the ondata event
<canvas width="340">
    <simplelayout axis="y"/>
    <dataset name="myDataset"
            request="true" src="http://localhost:8080/lps-
       4.0.x/app08/getTime.jsp"
            ondata="splash.setAttribute('visible', false)"
    />
    <text name="myText" width="340"
       datapath="myDataset:/serverTime/text()"/>
    <button onclick="splash.setAttribute('visible',
       true);myDataset.doRequest()">Update Time</button>
    <view width="100" height="20" name="splash" bgcolor="silver">
        <text>Loading</text>
    </view>
</canvas>

When the application loads, it will display the view that acts as a splash screen. When the data arrives, the splash screen disappears.

To test the application in Listing 8.11, use this URL.

http://localhost:8080/lps-4.0.x/app08/ondataTest1.lzx

Figure 8.7 shows the splash screen

Figure 8.7. Splash screen


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset