⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 portletutils.java

📁 spring api 源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * needs to have a public no-arg constructor.
	 * Useful for on-demand state objects in a web tier, like shopping carts.
	 * @param session current portlet session
	 * @param name the name of the session attribute
	 * @param clazz the class to instantiate for a new attribute
	 * @param scope the session scope of this attribute
	 * @return the value of the session attribute, newly created if not found
	 * @throws IllegalArgumentException if the session attribute could not be instantiated; or if the supplied <code>session</code> argument is <code>null</code>
	 */
	public static Object getOrCreateSessionAttribute(PortletSession session, String name, Class clazz, int scope)
			throws IllegalArgumentException {
		Assert.notNull(session, "Session must not be null");
		Object sessionObject = session.getAttribute(name, scope);
		if (sessionObject == null) {
			Assert.notNull(clazz, "Class must not be null if attribute value is to be instantiated");
			try {
				sessionObject = clazz.newInstance();
			}
			catch (InstantiationException ex) {
				throw new IllegalArgumentException(
						"Could not instantiate class [" + clazz.getName() +
						"] for session attribute '" + name + "': " + ex.getMessage());
			}
			catch (IllegalAccessException ex) {
				throw new IllegalArgumentException(
						"Could not access default constructor of class [" + clazz.getName() +
						"] for session attribute '" + name + "': " + ex.getMessage());
			}
			session.setAttribute(name, sessionObject, scope);
		}
		return sessionObject;
	}

	/**
	 * Return the best available mutex for the given session:
	 * that is, an object to synchronize on for the given session.
	 * <p>Returns the session mutex attribute if available; usually,
	 * this means that the
	 * {@link org.springframework.web.util.HttpSessionMutexListener}
	 * needs to be defined in <code>web.xml</code>. Falls back to the
	 * {@link javax.portlet.PortletSession} itself if no mutex attribute found.
	 * <p>The session mutex is guaranteed to be the same object during
	 * the entire lifetime of the session, available under the key defined
	 * by the {@link org.springframework.web.util.WebUtils#SESSION_MUTEX_ATTRIBUTE}
	 * constant. It serves as a safe reference to synchronize on for locking
	 * on the current session.
	 * <p>In many cases, the {@link javax.portlet.PortletSession} reference
	 * itself is a safe mutex as well, since it will always be the same
	 * object reference for the same active logical session. However, this is
	 * not guaranteed across different servlet containers; the only 100% safe
	 * way is a session mutex.
	 * @param session the HttpSession to find a mutex for
	 * @return the mutex object (never <code>null</code>)
	 * @see org.springframework.web.util.WebUtils#SESSION_MUTEX_ATTRIBUTE
	 * @see org.springframework.web.util.HttpSessionMutexListener
	 */
	public static Object getSessionMutex(PortletSession session) {
		Assert.notNull(session, "Session must not be null");
		Object mutex = session.getAttribute(WebUtils.SESSION_MUTEX_ATTRIBUTE);
		if (mutex == null) {
			mutex = session;
		}
		return mutex;
	}

	/**
	 * Expose the given Map as request attributes, using the keys as attribute names
	 * and the values as corresponding attribute values. Keys must be Strings.
	 * @param request current portlet request
	 * @param attributes the attributes Map
	 * @throws IllegalArgumentException if an invalid key is found in the Map (i.e. the key is not a String); or if
	 * either of the supplied arguments is <code>null</code>
	 */
	public static void exposeRequestAttributes(PortletRequest request, Map attributes)
			throws IllegalArgumentException {
		Assert.notNull(request, "Request must not be null");
		Assert.notNull(attributes, "attributes Map must not be null");
		Iterator it = attributes.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			if (!(entry.getKey() instanceof String)) {
				throw new IllegalArgumentException(
						"Invalid key [" + entry.getKey() + "] in attributes Map - only Strings allowed as attribute keys");
			}
			request.setAttribute((String) entry.getKey(), entry.getValue());
		}
	}

	/**
	 * Check if a specific input type="submit" parameter was sent in the request,
	 * either via a button (directly with name) or via an image (name + ".x" or
	 * name + ".y").
	 * @param request current portlet request
	 * @param name name of the parameter
	 * @return if the parameter was sent
	 * @see org.springframework.web.util.WebUtils#SUBMIT_IMAGE_SUFFIXES
	 */
	public static boolean hasSubmitParameter(PortletRequest request, String name) {
		return getSubmitParameter(request, name) != null;
	}

	/**
	 * Return the full name of a specific input type="submit" parameter 
	 * if it was sent in the request, either via a button (directly with name)
	 * or via an image (name + ".x" or name + ".y").
	 * @param request current portlet request
	 * @param name name of the parameter
	 * @return the actual parameter name with suffix if needed - null if not present
	 * @see org.springframework.web.util.WebUtils#SUBMIT_IMAGE_SUFFIXES
	 */
	public static String getSubmitParameter(PortletRequest request, String name) {
		Assert.notNull(request, "Request must not be null");
		if (request.getParameter(name) != null) {
			return name;
		}
		for (int i = 0; i < WebUtils.SUBMIT_IMAGE_SUFFIXES.length; i++) {
			String suffix = WebUtils.SUBMIT_IMAGE_SUFFIXES[i];
			String parameter = name + suffix;
			if (request.getParameter(parameter) != null) {
				return parameter;
			}
		}
		return null;
	}

	/**
	 * Return a map containing all parameters with the given prefix.
	 * Maps single values to String and multiple values to String array.
	 * <p>For example, with a prefix of "spring_", "spring_param1" and
	 * "spring_param2" result in a Map with "param1" and "param2" as keys.
	 * <p>Similar to portlet
	 * {@link javax.portlet.PortletRequest#getParameterMap()},
	 * but more flexible.
	 * @param request portlet request in which to look for parameters
	 * @param prefix the beginning of parameter names
	 * (if this is <code>null</code> or the empty string, all parameters will match)
	 * @return map containing request parameters <b>without the prefix</b>,
	 * containing either a String or a String array as values
	 * @see javax.portlet.PortletRequest#getParameterNames
	 * @see javax.portlet.PortletRequest#getParameterValues
	 * @see javax.portlet.PortletRequest#getParameterMap
	 */
	public static Map getParametersStartingWith(PortletRequest request, String prefix) {
		Assert.notNull(request, "Request must not be null");
		Enumeration paramNames = request.getParameterNames();
		Map params = new TreeMap();
		if (prefix == null) {
			prefix = "";
		}
		while (paramNames != null && paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			if ("".equals(prefix) || paramName.startsWith(prefix)) {
				String unprefixed = paramName.substring(prefix.length());
				String[] values = request.getParameterValues(paramName);
				if (values == null || values.length == 0) {
					// Do nothing, no values found at all.
				}
				else if (values.length > 1) {
					params.put(unprefixed, values);
				}
				else {
					params.put(unprefixed, values[0]);
				}
			}
		}
		return params;
	}


	/**
	 * Pass all the action request parameters to the render phase by putting them into
	 * the action response object. This may not be called when the action will call
	 * {@link javax.portlet.ActionResponse#sendRedirect sendRedirect}.
	 * @param request the current action request
	 * @param response the current action response
	 * @see javax.portlet.ActionResponse#setRenderParameter
	 */
	public static void passAllParametersToRenderPhase(ActionRequest request, ActionResponse response) {
		try {
			Enumeration en = request.getParameterNames();
			while (en.hasMoreElements()) {
				String param = (String) en.nextElement();
				String values[] = request.getParameterValues(param);
				response.setRenderParameter(param, values);
			}
		}
		catch (IllegalStateException ex) {
			// Ignore in case sendRedirect was already set.
		}
	}

	/**
	 * Clear all the render parameters from the {@link javax.portlet.ActionResponse}.
	 * This may not be called when the action will call
	 * {@link ActionResponse#sendRedirect sendRedirect}.
	 * @param response the current action response
	 * @see ActionResponse#setRenderParameters
	 */
	public static void clearAllRenderParameters(ActionResponse response) {
		try {
			response.setRenderParameters(new HashMap());
		}
		catch (IllegalStateException ex) {
			// Ignore in case sendRedirect was already set.
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -