buildmenurepositorytag.java
来自「Java的框架」· Java 代码 · 共 182 行
JAVA
182 行
package mcaps.core.menu.webapp.taglib;
import java.util.ArrayList;
import java.util.Collections;
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 mcap.core.menu.util.MenuOrderComparator;
import net.sf.navigator.menu.MenuComponent;
import net.sf.navigator.menu.MenuRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* Tag for building menu repository.
* @author Tan Beng Suang
* @date 06-Sep-2005
* @version 1.0.1.0
*/
public class BuildMenuRepositoryTag extends TagSupport {
private static MenuOrderComparator comparator = new MenuOrderComparator ();
private static final long serialVersionUID = 1255667227103428688L;
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 {
MenuRepository repository = this.buildRepository ();
if (scope != null) {
if (scope.equals ("page")) {
pageContext.setAttribute (name, repository);
}
else if (scope.equals ("request")) {
pageContext.getRequest ().setAttribute (name, repository);
}
else if (scope.equals ("session")) {
pageContext.getSession ().setAttribute (name, repository);
}
else if (scope.equals ("application")) {
pageContext.getServletContext ().setAttribute (name, repository);
}
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 MenuRepository buildRepository () {
MenuRepository repository = new MenuRepository ();
// Get the repository from the application scope - and copy the
// DisplayerMappings from it.
MenuRepository defaultRepository = (MenuRepository) pageContext
.getServletContext ().getAttribute (MenuRepository.MENU_REPOSITORY_KEY);
repository.setDisplayers (defaultRepository.getDisplayers ());
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);
MenuComponent mc = buildMenuComponent (menu, "");
repository.addMenu (mc);
}
}
else {
for (int i = 0; i < list.size (); i++) {
menu = (Menu) list.get (i);
if (strList.contains (menu.getName ())) {
MenuComponent mc = buildMenuComponent (menu, "");
repository.addMenu (mc);
}
}
}
}
return repository;
}
private List parseMenusString () {
StringTokenizer st = new StringTokenizer (menus, ",");
List list = new ArrayList ();
while (st.hasMoreTokens ()) {
list.add (st.nextToken ().trim ());
}
return list;
}
private MenuComponent buildMenuComponent (Menu menu, String parentRoles) {
MenuComponent mc = new MenuComponent ();
mc.setName ("menu_" + menu.getId());
mc.setTitle (menu.getTitle ());
mc.setDescription (menu.getDescription ());
mc.setTarget (menu.getTarget ());
String page = menu.getPage();
if (page != null && page.length () > 0 && !page.equals("null"))
mc.setPage (page);
mc.setRoles (menu.getRolesAsString ());
//if current menu roles is not set, use parent.
if (mc.getRoles() == null || mc.getRoles().length()== 0)
mc.setRoles(parentRoles);
List childList = menu.getMenuItems ();
Collections.sort(childList, comparator);
for (int i = 0; i < childList.size (); i++) {
Menu child = (Menu) childList.get (i);
mc.addMenuComponent (buildMenuComponent (child, mc.getRoles()));
}
return mc;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?