1 18 19 package org.apache.struts.webapp.tiles.portal; 20 21 import java.util.List ; 22 23 import javax.servlet.ServletContext ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 28 import org.apache.struts.action.ActionForm; 29 import org.apache.struts.action.ActionForward; 30 import org.apache.struts.action.ActionMapping; 31 import org.apache.struts.tiles.ComponentContext; 32 import org.apache.struts.tiles.actions.TilesAction; 33 34 41 public final class UserPortalAction extends TilesAction { 42 43 46 public static String DEFAULT_USER_SETTINGS_NAME = 47 "tiles.examples.portal.USER_PORTAL_SETTINGS"; 48 49 52 public static String NUMCOLS_ATTRIBUTE = "numCols"; 53 54 57 public static String LIST_ATTRIBUTE = "list"; 58 59 62 public static String LIST_LABELS_ATTRIBUTE = "labels"; 63 64 68 public static String USER_SETTINGS_NAME_ATTRIBUTE = "userSettingsName"; 69 70 73 public static String PORTAL_CATALOG_NAME = "tiles.examples.portal.PortalCatalog"; 74 75 92 public ActionForward execute( 93 ComponentContext context, 94 ActionMapping mapping, 95 ActionForm form, 96 HttpServletRequest request, 97 HttpServletResponse response) 98 throws Exception { 99 100 PortalSettings settings = getSettings(request, context); 102 103 context.putAttribute("numCols", Integer.toString(settings.getNumCols())); 105 106 for (int i = 0; i < settings.getNumCols(); i++) { 107 context.putAttribute("list" + i, settings.getListAt(i)); 108 } 109 110 return null; 111 } 112 113 117 public static PortalSettings getSettings( 118 HttpServletRequest request, 119 ComponentContext context) { 120 121 HttpSession session = request.getSession(); 123 124 String userSettingsId = 126 (String ) context.getAttribute(USER_SETTINGS_NAME_ATTRIBUTE); 127 128 if (userSettingsId == null) { 129 userSettingsId = DEFAULT_USER_SETTINGS_NAME; 130 } 131 132 PortalSettings settings = 134 (PortalSettings) session.getAttribute(userSettingsId); 135 136 if (settings == null) { 137 settings = new PortalSettings(); 139 settings.setNumCols((String ) context.getAttribute(NUMCOLS_ATTRIBUTE)); 140 141 for (int i = 0; i < settings.getNumCols(); i++) { 142 List tiles = 143 (List ) context.getAttribute(((String ) LIST_ATTRIBUTE + i)); 144 145 settings.setListAt(i, tiles); 146 } 147 148 session.setAttribute(userSettingsId, settings); 150 } 151 152 return settings; 153 } 154 155 159 static public PortalCatalog getPortalCatalog( 160 ComponentContext context, 161 ServletContext servletContext) { 162 163 PortalCatalog catalog = 164 (PortalCatalog) servletContext.getAttribute(PORTAL_CATALOG_NAME); 165 166 if (catalog == null) { catalog = new PortalCatalog(); 168 int numCols = 169 Integer.parseInt((String ) context.getAttribute(NUMCOLS_ATTRIBUTE)); 170 171 for (int i = 0; i < numCols; i++) { 172 List tiles = 173 (List ) context.getAttribute(((String ) LIST_ATTRIBUTE + i)); 174 175 List labels = 176 (List ) context.getAttribute( 177 ((String ) LIST_LABELS_ATTRIBUTE + i)); 178 179 catalog.addTiles(tiles, labels); 180 } 181 servletContext.setAttribute(PORTAL_CATALOG_NAME, catalog); 182 } 183 184 return catalog; 185 } 186 187 } 188 | Popular Tags |