JavaServer Page

October 24, 2006

616 CHAPTER 15 USING STRUTS, XDOCLET, AND

Filed under: JavaServer Page — webmaster @ 3:26 am

616 CHAPTER 15 USING STRUTS, XDOCLET, AND OTHER TOOLS Tiles Controllers Tiles controllers can be very helpful in improving the architecture of your Tiles-enabled application. They haven t received much press in current publications, but can be a very useful feature. At its core, a Tiles controller is designed to prepare data for presentation on a tile. You might think of it as a mini-action. However, these controllers aren t designed to determine application flow; that s the responsibility of the ActionServlet. If you re developing a portal site or you have tiles that require their own custom data, you should definitely considering using one. Of course, it will be easier to understand if we give you an example. Therefore, we ve created a feature for struts-resumethat counts the current number of active sessions and displays it as Current Users. We did this by first creating a UserCounterListenerthat implements ServletContextListener and HttpSessionListener. This listener increments an applicationscoped variable when new sessions are created and decrements from the same variable when sessions are destroyed. This source file is located in struts-resumeat src/web/org/appfuse/ webapp/listener. We ve used XDoclet s @web:listener tag to create a
entry for this class in web.xml. To implement a Tiles controller, we created a UserCounterController class that implements the Controllerinterface and its method: public final class UserCounterController implements Controller { /** * This method illustrates a simple example of using a Tiles Controller * to get a “current users” counter for this application. * * @param tileContext Current tile context * @param request Current request * @param response Current response * @param servletContext Current Servlet Context */ public void perform(ComponentContext tilesContext, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws ServletException, IOException { // Get the number of current users from the application’s context String userCounter = (String) servletContext.getAttribute(UserCounterListener.COUNT_KEY); // Add this number to the request for display request.setAttribute(UserCounterListener.COUNT_KEY, userCounter); } } You can see that the perform() method s signature is similar to the Actionclass s signature except there s no ActionMapping or ActionForm. The ComponentContext is a scope similar to that of a request or session; however, it s specific to Tiles and is used to store its configuration information.

Note: If you are looking for good and high quality web space to host and run your jsp application check Lunarwebhost jsp web hosting services

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

Powered by Java Web Hosting