10.8. Refreshing Application Data

Problem

You want the ability to refresh application-scoped objects cached in the servlet context.

Solution

Provide an interface, such as the one shown in Example 10-24, which indicates if an object can be refreshed.

Example 10-24. Interface used to refresh cached objects

package com.oreilly.strutsckbk.ch10;

/**
 * An object that can be refreshed from its original source.
 */
public interface Refreshable {

    public void refresh( ) throws CacheException;
    
}

Create an Action, such as the one shown in Example 10-25, which refreshes objects in the servlet context. Alternatively, you could create a base Action that provides a protected method for refreshing a named object. This may be necessary to avoid chaining to the ContextRefreshAction whenever you need to refresh data.

Example 10-25. Context refresh action

package com.oreilly.strutsckbk.ch10; import java.util.Enumeration; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class ContextRefreshAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String name = request.getParameter("name"); ServletContext ctx = servlet.getServletContext( ); ...

Get Jakarta Struts Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.