Online Book Reader

Home Category

AJAX In Action [215]

By Root 4099 0
provides a getPortalWindows() Licensed to jonathan zheng

Implementing DHTML windows

443

method c that takes a User object as an argument. We have one of those sitting in the session, so we pull it out now b. The actual JavaScript is written by the JSUtil object again, providing some user interface initialization code d, declaring each of the portal windows that we’ve extracted from the database e and then writing them directly to the servlet output stream f.

Let’s briefly review the helper objects that we’ve used along the way, DBUtil and JSUtil. We used DBUtil to get a list of the portal windows. As we noted, we’d probably automate this in production using Hibernate or something similar; but listing 11.9 provides a method from DBUtil that is a simple home-rolled implementation of accessing the portal_windows table in the database, for teaching purposes. We’re using straightforward SQL directly here, so it should be easy to adapt to the server language of your choice.

Listing 11.9 getPortalWindows() method

public static List getPortalWindows(User user){

List list=new ArrayList();

Connection conn=getConnection();

try{

String sql="SELECT * FROM portal_windows "

+"WHERE user_id="+user.getId(); b

Construct SQL statement

Statement stmt=conn.createStatement();

ResultSet rs=stmt.executeQuery(sql);

PortalWindow win=null;

while (rs.next()){ c

Iterate through results

int id=rs.getInt("id");

int x=rs.getInt("xPos");

int y=rs.getInt("yPos");

int w=rs.getInt("width");

int h=rs.getInt("height");

String url=rs.getString("url");

String title=rs.getString("title");

win=new PortalWindow( d

Add Object

id,user,x,y,w,h,url,title

);

list.add(win);

}

rs.close();

stmt.close();

}catch (SQLException sqlex){

}

return list;

}

Licensed to jonathan zheng

444

CHAPTER 11

The enhanced Ajax web portal

We simply construct the SQL statement b, iterate through the result set that it generates c, and add a PortalWindow object to our list in each case d. Second, we use the JSUtil helper object to generate some initialization code and declare our window objects in JavaScript. The methods are basically exercises in string concatenation, and we won’t show the full class here. The following code gives a flavor of how it works:

public static String initWindow(PortalWindow window) {

StringBuffer jsBuf=new StringBuffer()

.append("CreateWindow(new NewWin('")

.append(window.getId())

.append("',")

.append(window.getXPos())

.append(",")

.append(window.getYPos())

.append(",")

.append(window.getWidth())

.append(",")

.append(window.getHeight())

.append(",'")

.append(window.getUrl())

.append("','")

.append(window.getTitle())

.append("'));\n");

return jsBuf.toString();

}

The initWindow() method generates the JavaScript code for initializing a single portal window. The JavaScript code from a successful request might look like this, with initWindow() being called for each window in turn (the code has been formatted here for improved readability): document.getElementById('login')

.innerHTML='Welcome back!'

document.getElementById('defaultContent')

.style.display='none';

CreateWindow(

new NewWin(

'1',612,115,615,260,

'http://www.javaranch.com','JavaRanch'

)

);

CreateWindow(

new NewWin(

'2',10,115,583,260,

'http://www.google.com','Google'

)

);

Licensed to jonathan zheng

Implementing DHTML windows

445

CreateWindow(

new NewWin(

'3',10,387,1220,300,

'http://radio.javaranch.com/pascarello','Ajax Blog!'

)

);

Since we are now logged in, we can remove the login textboxes and submit button by placing a welcome message in their place. After we put up the welcome message, we need to hide the content that’s on the screen by default. To do this, we set the defaultContent DOM element’s display property to none so it is removed from the user’s view.

The JavaScript statement that instantiates the window involves two parts for each window. The first part is a function call to CreateWindow(), which is part of the JavaScript library that we added. Inside

Return Main Page Previous Page Next Page

®Online Book Reader