userpreferencetag.java

来自「Java的框架」· Java 代码 · 共 135 行

JAVA
135
字号
package mcaps.core.preference.webapp.taglib;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import mcap.core.preference.model.Preference;
import mcap.core.preference.model.PreferenceSet;
import mcap.core.preference.service.PreferenceManager;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * This tag store the preference for current user in the required scope.
 * @author Tan Beng Suang
 * @date 02-Sep-2005
 * @version 1.0.1.0
 */
public class UserPreferenceTag extends TagSupport {

	private static final long serialVersionUID = -7046175544669125502L;

	private String scope;
	private String key;
	private String name;

	/**
	 * Returns the scope.
	 * @return String
	 */
	public String getScope () {
		return scope;
	}

	/**
	 * Sets the scope.
	 * @param scope The scope to set.
	 */
	public void setScope (String scope) {
		this.scope = scope;
	}

	/**
	 * Returns the key.
	 * @return String
	 */
	public String getKey () {
		return key;
	}

	/**
	 * Sets the key.
	 * @param key The key to set.
	 */
	public void setKey (String key) {
		this.key = key;
	}

	/**
	 * Returns the name.
	 * @return String
	 */
	public String getName () {
		return name;
	}

	/**
	 * Sets the name.
	 * @param name The name to set.
	 */
	public void setName (String name) {
		this.name = name;
	}

	/**
	 * Process the start of this tag.
	 * @return
	 * @exception JspException if a JSP exception has occurred
	 * @see javax.servlet.jsp.tagext.Tag#doStartTag()
	 */
	public int doStartTag () throws JspException {

		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext (pageContext.getServletContext ());

		PreferenceManager mgr = (PreferenceManager) ctx
				.getBean ("preferenceManager");

		PreferenceSet set = mgr
				.getPreferenceSet ( ((HttpServletRequest) pageContext.getRequest ())
						.getRemoteUser ());

		Preference pref = set.get (key);

		if (pref != null) {
			if (scope != null) {
				if (name == null)
					throw new JspException (
							"Attribute 'name' must be specified when attribute 'scope' is specified.");

				if (scope.equals ("page")) {
					pageContext.setAttribute (name, pref.getValue ());
				}
				else if (scope.equals ("request")) {
					pageContext.getRequest ().setAttribute (name, pref.getValue ());
				}
				else if (scope.equals ("session")) {
					pageContext.getSession ().setAttribute (name, pref.getValue ());
				}
				else if (scope.equals ("application")) {
					pageContext.getServletContext ()
							.setAttribute (name, pref.getValue ());
				}
				else {
					throw new JspException (
							"Attribute 'scope' must be: page, request, session or application");
				}
			}
			else {
				try {
					pageContext.getOut ().write (pref.getValue ());
				}
				catch (IOException io) {
					throw new JspException (io);
				}
			}
		}
		return super.doStartTag ();
	}

}

⌨️ 快捷键说明

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