cedric 02/02/18 06:57:59 Added: contrib/tiles/src/tutorial/org/apache/struts/example/tiles/skin DefinitionCatalog.java LayoutSettingsAction.java LayoutSettingsForm.java LayoutSwitchAction.java SimpleSwitchLayoutAction.java Log: New skin example sources Revision Changes Path 1.1 jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/skin/DefinitionCatalog.java Index: DefinitionCatalog.java =================================================================== //Source file: H:\\TEMP\\generated\\org\\apache\\struts\\example\\tiles\\skin\\DefinitionCatalog.java package org.apache.struts.example.tiles.skin; import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; import java.util.Iterator; import org.apache.struts.tiles.ComponentDefinition; import org.apache.struts.tiles.DefinitionsUtil; import org.apache.struts.tiles.FactoryNotFoundException; import org.apache.struts.tiles.DefinitionsFactoryException; import org.apache.struts.tiles.NoSuchDefinitionException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletContext; /** * A catalog of available definitions. */ public class DefinitionCatalog { /** debug flag */ public static boolean debug = true; /** Attribute carrying definition readable name */ public static final String LABEL_NAME_ATTRIBUTE = "skin.label"; /** Attribute carrying the list of definition names */ public static final String DEFINITION_LIST_ATTRIBUTE = "skin.list"; /** * Map of skins, by their keys */ private Map definitions = new HashMap(); /** * Map of skins, by their keys */ private ComponentDefinition defaultDefinition; /** * List of names */ private List names = new ArrayList(); /** * List of keys */ private List keys = new ArrayList(); /** * Constructor. * Initialize catalog from definitions factory. * @param HttpRequest request * @param ServletContext context * @throws FactoryNotFoundException, DefinitionsFactoryException */ public DefinitionCatalog( String catalogName, HttpServletRequest request, ServletContext context) throws FactoryNotFoundException, DefinitionsFactoryException { // Get definition containing list of definitions ComponentDefinition catalogDef = DefinitionsUtil.getDefinition( catalogName, request, context); if(debug) System.out.println( "Got definition " + catalogDef ); // Get list of definition names List list = (List)catalogDef.getAttribute( DEFINITION_LIST_ATTRIBUTE ); Iterator i = list.iterator(); while(i.hasNext() ) { String name = (String)i.next(); System.out.println( "add " + name ); ComponentDefinition def = DefinitionsUtil.getDefinition(name, request, context); if(def==null) throw new NoSuchDefinitionException("Can't find definition '" + name + "'" ); add( name, def ); } // end loop if(debug) System.out.println( "Catalog initialized" ); } /** * Get definition identified by key. * @param key * @return Definition associated to key */ public ComponentDefinition get(Object key) { if(key==null) return getDefault(); return (ComponentDefinition)definitions.get(key); } /** * Get definition identified by key. * @param key * @return Definition associated to key */ public ComponentDefinition getDefault() { return defaultDefinition; } /** * Return List of names of definitions presents in catalog. * Names are user readable names. Returned list has the same order as list * returned by getKeys. * @return List */ public List getNames() { return names; } /** * Get list of keys of definitions present in catalog. * A key is used to retrieve a skin from catalog. * @return List */ public List getKeys() { return keys; } /** * Check if requested key is valid in catalog. * Return null otherwise * @return valid key or null */ public String getKey( String key ) { if( definitions.get(key) != null) return key; return null; } /** * Add a skin definition * @param definition */ public void add(String key, ComponentDefinition definition) { // Intitialize default definition with first definition encountered if( defaultDefinition == null ) { defaultDefinition = definition; } // store definition definitions.put( key , definition); Object name = definition.getAttribute(LABEL_NAME_ATTRIBUTE); if( name == null ) name = key; names.add( name ); keys.add(key); } } 1.1 jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/skin/LayoutSettingsAction.java Index: LayoutSettingsAction.java =================================================================== //Source file: H:\\TEMP\\generated\\org\\apache\\struts\\example\\tiles\\skin\\LayoutSettingAction.java package org.apache.struts.example.tiles.skin; import org.apache.struts.tiles.actions.TilesAction; import org.apache.struts.action.ActionForward; import org.apache.struts.tiles.ComponentContext; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import javax.servlet.ServletException; /** * Action used to set user skin. */ public class LayoutSettingsAction extends TilesAction { /** debug flag */ public static boolean debug = true; /** * Process the specified HTTP request, and create the corresponding HTTP * response (or forward to another web component that will create it). * Return an <code>ActionForward</code> instance describing where and how * control should be forwarded, or <code>null</code> if the response has * already been completed. * This method should be implemented by subclasses. * * @param context The current Tile context, containing Tile attributes * @param mapping The ActionMapping used to select this instance * @param actionForm The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet exception occurs */ public ActionForward perform( ComponentContext context, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if(debug) System.out.println("Enter action LayoutSettingAction"); LayoutSettingsForm actionForm = (LayoutSettingsForm)form; // Load user menu settings and available list of choices String selected = LayoutSwitchAction.getUserSetting( context, request ); if(selected==null) selected = "default"; System.out.println("user setting retrieved"); DefinitionCatalog catalog = LayoutSwitchAction.getCatalog( context, request, getServlet().getServletContext() ); System.out.println("catalog retrieved"); // Check if form is submitted // If true, read, check and store new values submitted by user. if( actionForm.isSubmitted() ) { // read arrays if(debug) System.out.println("form submitted"); selected = catalog.getKey(actionForm.getSelected()); if(debug) System.out.println( "key : " + selected ); LayoutSwitchAction.setUserSetting(context, request, selected ); if(debug) System.out.println( "settings : " + selected ); actionForm.reset(); } // end if // Prepare data for view tile context.putAttribute( "selected", selected ); context.putAttribute( "catalog", catalog ); if(debug) System.out.println("Exit action LayoutSettingAction"); return null; } } 1.1 jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/skin/LayoutSettingsForm.java Index: LayoutSettingsForm.java =================================================================== //Source file: H:\\TEMP\\generated\\org\\apache\\struts\\example\\tiles\\skin\\LayoutSettingForm.java package org.apache.struts.example.tiles.skin; import org.apache.struts.action.ActionForm; /** * Struts form */ public class LayoutSettingsForm extends ActionForm { /** Validate value */ protected String validate; /** * User selected key value */ private String selected; /** * Access method for the selectedKey property. * * @return the current value of the selectedKey property */ public String getSelected() { return selected; } /** * Sets the value of the selectedKey property. * * @param aSelectedKey the new value of the selectedKey property */ public void setSelected(String aSelectedKey) { selected = aSelectedKey; } /** * Is this form submitted ? */ public boolean isSubmitted() { return validate != null; } /** * Is this form submitted ? */ public void setValidate( String value) { this.validate = value; } /** * Reset properties */ public void reset() { selected = null; validate = null; } } 1.1 jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/skin/LayoutSwitchAction.java Index: LayoutSwitchAction.java =================================================================== package org.apache.struts.example.tiles.skin; import org.apache.struts.tiles.DefinitionsUtil; import org.apache.struts.tiles.DefinitionsFactoryException; import org.apache.struts.tiles.definition.ReloadableDefinitionsFactory; import org.apache.struts.tiles.ComponentContext; import org.apache.struts.tiles.actions.TilesAction; import org.apache.struts.tiles.ComponentDefinition; import org.apache.struts.tiles.DefinitionsFactoryException; import org.apache.struts.tiles.FactoryNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.Map; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.RequestDispatcher; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * Customize layouts according to predefined "skin" * A "skin" is a set of layouts used together to provide a consistent * Look & Feel. * This action is called when inserting a definition's layout. It replaces * definition's layout by the one set in selected skin. * the appropriate * Available skins are stored in application context. They are initialized from * a Tile definition. * Currently selected skin is stored under user session, if any. Otherwise, * the default skin is used. * This action act for all layouts. A Tile's attribute (skinLayout) is used as a key to * know which layouts is concerned. */ public class LayoutSwitchAction extends TilesAction { /** debug flag */ public static boolean debug = true; /** Tile's attribute containing layout key */ public static final String LAYOUT_ATTRIBUTE = "layout.attribute"; /** Tile attribute containing name used to store user settings in session context */ public static String USER_SETTINGS_NAME_ATTRIBUTE = "userSettingsName"; /** Default name used to store settings in session context */ public static String DEFAULT_USER_SETTINGS_NAME = "examples.tiles.skin.SELECTED_DEFINITION"; /** Name of catalog in application context */ public static final String CATALOG_NAME = "examples.tiles.skin.CATALOG_NAME"; /** Default name used to store menu catalog in application scope */ public static String DEFAULT_CATALOG_NAME = "tiles.examples.skin.layoutCatalog"; /** Tile attribute containing name used to store menu catalog in application scope */ public static String CATALOG_NAME_ATTRIBUTE = "catalogName"; /** Tile attribute containing name of the settings definition used to initialize catalog */ public static final String CATALOG_SETTING_ATTRIBUTE = "catalogSettings"; /** * Process the specified HTTP request, and create the corresponding HTTP * response (or forward to another web component that will create it). * Return an <code>ActionForward</code> instance describing where and how * control should be forwarded, or <code>null</code> if the response has * already been completed. * This method should be implemented by subclasses. * * @param context The current Tile context, containing Tile attributes * @param mapping The ActionMapping used to select this instance * @param actionForm The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet exception occurs */ public ActionForward perform( ComponentContext context, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if(debug) System.out.println( "EnterLayoutSwitchAction" ); // Get attribute value indicating which layout we want String layoutKey = (String)context.getAttribute( LAYOUT_ATTRIBUTE ); if(layoutKey==null) throw new ServletException( "Error - CustomSkinAction : attribute '" + LAYOUT_ATTRIBUTE + "' not found in Tile's attributes. Need it to select appropriate layout" ); // Get user current skin ComponentDefinition definition = getCurrentDefinition( context, request, getServlet().getServletContext() ); // get requested layout from definition String layout = (String)definition.getAttribute(layoutKey); if(layout==null) throw new ServletException( "Error - CustomSkinAction : no layout defined for key '" + layoutKey + "' in currently selected skin '" + getUserSetting(context, request ) + "'." ); // set path to forward to // Not very nice solution, need to improve it /* ComponentDefinition forwarDefinition = new ComponentDefinition( "", layout, new HashMap() ); DefinitionsUtil.setActionDefinition( request, forwarDefinition ); */ if(debug) System.out.println( "Switch to : " + layout ); RequestDispatcher rd = getServlet().getServletContext().getRequestDispatcher( layout ); if(rd==null) throw new ServletException( "LayoutSwitch error : Can't find layout '" + layout + "'." ); rd.include(request, response); if(debug) System.out.println( "Exit LayoutSwitchAction" ); return null; } /** * Retrieve key associated to user. * This key denote a definition in catalog. * Return user selected key, or "default" if none is set. */ public static String getUserSetting( ComponentContext context, HttpServletRequest request ) { HttpSession session = request.getSession( false ); if( session == null ) return null; // Retrieve attribute name used to store settings. String userSettingsName = (String)context.getAttribute( USER_SETTINGS_NAME_ATTRIBUTE ); if( userSettingsName == null ) userSettingsName = DEFAULT_USER_SETTINGS_NAME; return (String)session.getAttribute(userSettingsName); } /** * Set user setting value. * This key denote a definition in catalog. * Return user selected key, or "default" if none is set. */ public static void setUserSetting( ComponentContext context, HttpServletRequest request, String setting ) { HttpSession session = request.getSession(); // Retrieve attribute name used to store settings. String userSettingsName = (String)context.getAttribute( USER_SETTINGS_NAME_ATTRIBUTE ); if( userSettingsName == null ) userSettingsName = DEFAULT_USER_SETTINGS_NAME; session.setAttribute(userSettingsName, setting); } /** * Get currently selected skin definition. */ public static ComponentDefinition getCurrentDefinition( ComponentContext context, HttpServletRequest request, ServletContext servletContext ) throws ServletException { // Get selected key String selected = getUserSetting(context, request); DefinitionCatalog catalog = getCatalog( context, request, servletContext); ComponentDefinition definition = (ComponentDefinition)catalog.get( selected ); if( definition == null ) definition = (ComponentDefinition)catalog.getDefault(); return definition; } /** * Get catalog of available skins. */ public static DefinitionCatalog getCatalog( ComponentContext context, HttpServletRequest request, ServletContext servletContext ) throws ServletException { // Retrieve name used to store catalog in application context. // If not found, use default name String catalogName = (String)context.getAttribute( CATALOG_NAME_ATTRIBUTE ); if(catalogName == null) catalogName = DEFAULT_CATALOG_NAME; if(debug) System.out.println( "Catalog name=" + catalogName ); try { DefinitionCatalog catalog = (DefinitionCatalog)servletContext.getAttribute( catalogName ); if(catalog == null) { // create catalog if(debug) System.out.println( "Create catalog" ); String catalogSettings = (String)context.getAttribute( CATALOG_SETTING_ATTRIBUTE ); if(catalogSettings == null) throw new ServletException( "Error - CustomSkinAction : attribute '" + CATALOG_SETTING_ATTRIBUTE + "' not found in Tile's attributes. Need it to initialize catalog" ); catalog = new DefinitionCatalog( catalogSettings, request, servletContext ); if(debug) System.out.println( "Catalog created" ); servletContext.setAttribute( catalogName, catalog ); } // end if return catalog; } catch(DefinitionsFactoryException ex ) { if(debug) System.out.println( "Exception : " + ex.getMessage() ); throw new ServletException( ex.getMessage() ); } } } 1.1 jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/skin/SimpleSwitchLayoutAction.java Index: SimpleSwitchLayoutAction.java =================================================================== package org.apache.struts.example.tiles.skin; import org.apache.struts.tiles.DefinitionsUtil; import org.apache.struts.tiles.DefinitionsFactoryException; import org.apache.struts.tiles.definition.ReloadableDefinitionsFactory; import org.apache.struts.tiles.ComponentContext; import org.apache.struts.tiles.actions.TilesAction; import org.apache.struts.tiles.ComponentDefinition; import org.apache.struts.tiles.DefinitionsFactoryException; import org.apache.struts.tiles.FactoryNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.Map; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.RequestDispatcher; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * Simple Switch Layout */ public class SimpleSwitchLayoutAction extends TilesAction { /** debug flag */ public static boolean debug = true; /** Tile's attribute containing layout key */ public static final String LAYOUT_ATTRIBUTE = "layout.attribute"; /** Tile attribute containing name used to store user settings in session context */ public static String USER_SETTINGS_NAME_ATTRIBUTE = "userSettingsName"; /** Default name used to store settings in session context */ public static String DEFAULT_USER_SETTINGS_NAME = "examples.tiles.skin.SELECTED_DEFINITION"; /** Name of catalog in application context */ public static final String CATALOG_NAME = "examples.tiles.skin.CATALOG_NAME"; /** Default name used to store menu catalog in application scope */ public static String DEFAULT_CATALOG_NAME = "tiles.examples.skin.layoutCatalog"; /** Tile attribute containing name used to store menu catalog in application scope */ public static String CATALOG_NAME_ATTRIBUTE = "catalogName"; /** Tile attribute containing name of the settings definition used to initialize catalog */ public static final String CATALOG_SETTING_ATTRIBUTE = "catalogSettings"; /** * Process the specified HTTP request, and create the corresponding HTTP * response (or forward to another web component that will create it). * Return an <code>ActionForward</code> instance describing where and how * control should be forwarded, or <code>null</code> if the response has * already been completed. * This method should be implemented by subclasses. * * @param context The current Tile context, containing Tile attributes * @param mapping The ActionMapping used to select this instance * @param actionForm The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet exception occurs */ public ActionForward perform( ComponentContext context, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if(debug) System.out.println( "Enter SimpleSwitchLayoutAction" ); String layoutDir = "/layouts/"; String userSelection = getUserSetting( context, request ); //String layout = "classicLayout.jsp"; String layout = (String)context.getAttribute( LAYOUT_ATTRIBUTE ); if(layout==null) throw new ServletException( "Attribute '" + LAYOUT_ATTRIBUTE + "' is required." ); String layoutPath = layoutDir+userSelection+ "/" + layout; RequestDispatcher rd = getServlet().getServletContext().getRequestDispatcher( layoutPath ); if(rd==null) { layoutPath = layoutDir + layout; rd = getServlet().getServletContext().getRequestDispatcher( layoutPath ); if(rd==null) throw new ServletException( "SwitchLayout error : Can't find layout '" + layoutPath + "'." ); } rd.include(request, response); if(debug) System.out.println( "Exit SimpleSwitchLayoutAction" ); return null; } /** * Retrieve key associated to user. * This key denote a definition in catalog. * Return user selected key, or "default" if none is set. */ public static String getUserSetting( ComponentContext context, HttpServletRequest request ) { HttpSession session = request.getSession( false ); if( session == null ) return null; // Retrieve attribute name used to store settings. String userSettingsName = (String)context.getAttribute( USER_SETTINGS_NAME_ATTRIBUTE ); if( userSettingsName == null ) userSettingsName = DEFAULT_USER_SETTINGS_NAME; return (String)session.getAttribute(userSettingsName); } /** * Set user setting value. * This key denote a definition in catalog. * Return user selected key, or "default" if none is set. */ public static void setUserSetting( ComponentContext context, HttpServletRequest request, String setting ) { HttpSession session = request.getSession(); // Retrieve attribute name used to store settings. String userSettingsName = (String)context.getAttribute( USER_SETTINGS_NAME_ATTRIBUTE ); if( userSettingsName == null ) userSettingsName = DEFAULT_USER_SETTINGS_NAME; session.setAttribute(userSettingsName, setting); } /** * Get catalog of available skins. */ public static DefinitionCatalog getCatalog( ComponentContext context, HttpServletRequest request, ServletContext servletContext ) throws ServletException { // Retrieve name used to store catalog in application context. // If not found, use default name String catalogName = (String)context.getAttribute( CATALOG_NAME_ATTRIBUTE ); if(catalogName == null) catalogName = DEFAULT_CATALOG_NAME; if(debug) System.out.println( "Catalog name=" + catalogName ); try { DefinitionCatalog catalog = (DefinitionCatalog)servletContext.getAttribute( catalogName ); if(catalog == null) { // create catalog if(debug) System.out.println( "Create catalog" ); String catalogSettings = (String)context.getAttribute( CATALOG_SETTING_ATTRIBUTE ); if(catalogSettings == null) throw new ServletException( "Error - CustomSkinAction : attribute '" + CATALOG_SETTING_ATTRIBUTE + "' not found in Tile's attributes. Need it to initialize catalog" ); catalog = new DefinitionCatalog( catalogSettings, request, servletContext ); if(debug) System.out.println( "Catalog created" ); servletContext.setAttribute( catalogName, catalog ); } // end if return catalog; } catch(DefinitionsFactoryException ex ) { if(debug) System.out.println( "Exception : " + ex.getMessage() ); throw new ServletException( ex.getMessage() ); } } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>