buildtopmenuslisttag.java

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

JAVA
139
字号
package mcaps.core.menu.webapp.taglib;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

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

import mcap.core.menu.model.Menu;
import mcap.core.menu.service.MenuManager;

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


/**
 * Tag for building a list of menu id ordered according to ordering.
 * 
 * @author bstan
 * @date 13-Dec-2005
 * @version 1.0.1.0
 */
public class BuildTopMenusListTag extends TagSupport {

	private static final long serialVersionUID = -8034511464312435726L;
	private String name;
	private String scope;
	private String menus;
	
	/**
	 * Sets the menus.
	 * @param menus The menus to set.
	 */
	public void setMenus (String menus) {
		this.menus = menus;
	}

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

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

	/**
	 * 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 {

		List list = buildList ();
		
		if (scope != null) {
			if (scope.equals ("page")) {
				pageContext.setAttribute (name, list);
			}
			else if (scope.equals ("request")) {
				pageContext.getRequest ().setAttribute (name, list);
			}
			else if (scope.equals ("session")) {
				pageContext.getSession ().setAttribute (name, list);
			}
			else if (scope.equals ("application")) {
				pageContext.getServletContext ().setAttribute (name, list);
			}
			else {
				throw new JspException (
						"Attribute 'scope' must be: page, request, session or application");
			}
		}

		return super.doStartTag ();
	}
	
	/**
	 * Release aquired resources to enable tag reusage.
	 * @see javax.servlet.jsp.tagext.Tag#release()
	 */
	public void release () {
		super.release ();
	}

	private List buildList () {

		List menuList = new ArrayList ();
		
		if (menus != null && menus.trim ().length () > 0) {
			List strList = parseMenusString ();

			ApplicationContext ctx = WebApplicationContextUtils
					.getRequiredWebApplicationContext (pageContext.getServletContext ());
			MenuManager mgr = (MenuManager) ctx.getBean ("menuManager");
			Menu menu = new Menu ();
			menu.setParentId ("-1");

			List list = mgr.getMenus (menu);

			// add all
			if (strList.contains ("All")) {
				for (int i = 0; i < list.size (); i++) {
					menu = (Menu) list.get (i);
					menuList.add ("menu_" + menu.getId());
				}
			}
			else {
				for (int i = 0; i < list.size (); i++) {
					menu = (Menu) list.get (i);
					if (strList.contains ("menu_" + menu.getId ())) {
						menuList.add ("menu_" + menu.getId());
					}
				}
			}
		}

		return menuList;
	}
	
	private List parseMenusString () {
		StringTokenizer st = new StringTokenizer (menus, ",");
		List list = new ArrayList ();
		while (st.hasMoreTokens ()) {
			list.add (st.nextToken ().trim ());
		}
		return list;
	}
}

⌨️ 快捷键说明

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