,ch14.9433 Page 482 Monday, February 14, 2005 12:55 PM
Example 14 33. Action that prepares an XML document
package com.oreilly.strutsckbk.ch14;
import java.util.Iterator;
import java.util.List;
import com.oroad.stxx.action.Action;
import javax.servlet.http.*;
import org.jdom.*;
import org.apache.struts.action.*;
public class UserListAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
List usersList = (List) getServlet().getServletContext( ).getAttribute("users");
//create a new XML document for this Action with the root
//element of "userList"
Document document =
new Document(new Element("userList"));
//add some data to the XML document so that the Action
//will produce XML in the form
Element users = new Element("users");
for (Iterator k=usersList.iterator(); k.hasNext( ); ) {
Element user = new Element("user");
Element name = new Element("name");
User u = (User) k.next( );
name.addContent(new Element("firstname").setText(u.getFirstName( )));
name.addContent(new Element("lastname").setText(u.getLastName( )));
user.addContent(name);
user.addContent(new Element("email").setText(u.getEmail( )));
// add the user
users.addContent(user);
}
// add to the root element and save the document
document.getRootElement( ).addContent(users);
saveDocument(request, document);
return mapping.findForward("success");
}
}
482 | Chapter 14: Tiles and Other Presentation Approaches
This is the Title of the Book, eMatter Edition
Copyright 2005 O'Reilly & Associates, Inc. All rights reserved.
footer
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved