Java通过读取XML配置用户功能

1、定制XML功能menu.xml
<root>
	<menus id="1" display="产品导航" priority="1" icon="menu-gps">
		<menu id="11" pid="1" display="产品之家" url="/service/products" priority="1" icon="menu-location"></menu>
		<menu id="12" pid="1" display="企业市场" url="/service/market" priority="2" icon="menu-track"></menu>
		<menu id="11" pid="1" display="在线服务" url="/service/online" priority="1" icon="menu-location"></menu>
		<menu id="12" pid="1" display="联系购买" url="/service/contactbuy" priority="2" icon="menu-track"></menu>
	</menus>
	<!--  其他服务  -->
</root>

2、编写菜单对象
public class Menu 
{
	private int id;
	private String text;
	private int pid;
	private String icon;
	private String url;
	private int priority;
	private List<Menu> children;
	//getter,setter...........
}

3、编写解析文件的工具
package com.cmboonya.configuration.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.boonya.code4j.orm.hibernate.BaseDao;
import com.boonya.code4j.orm.hibernate.BaseService;
import com.cmboonya.configuration.dao.AccountMemberGroupDao;
import com.cmboonya.configuration.dao.AccountRoleDao;
import com.cmboonya.configuration.entity.AccountMember;
import com.cmboonya.configuration.entity.AccountMemberGroup;
import com.cmboonya.configuration.entity.Menu;

@Service
@Transactional
public class MenuService extends BaseService<Menu>
{

	@Resource
	private IAccountMemberService accountMemberService;

	@Resource
	private IAccountRoleFunctionService accountRoleFunctionService;

	public Document getDocument(HttpServletRequest req)
	{
		SAXReader reader = new SAXReader();
		Document document = null;
		try
		{
			document=reader.read((MenuService.class.getClassLoader().getResourceAsStream("menu.xml")));
		} catch (DocumentException e)
		{
			e.printStackTrace();
		}
		return document;
	}

	public List<Menu> getMenuListByUserId(int userId, HttpServletRequest req)
	{
		AccountMember user = accountMemberService.getUserById(userId);
		List<Menu> menuList = new ArrayList<Menu>();
		if (user.getType() == 0)
		{
			menuList = getAll(req);
		} else
		{
			menuList=getAll(req, userId);
		}

		return menuList;
	}

        @SuppressWarnings("rawtypes")
	public List<Menu> getAll(HttpServletRequest req)
	{
		List<Menu> menus = new ArrayList<Menu>();
		Document document = getDocument(req);
		Element rootElm = document.getRootElement();
		List nodes = rootElm.elements("menus");
		for (Iterator it = nodes.iterator(); it.hasNext();)
		{
			Element elm = (Element) it.next();
			Menu menu = new Menu();

			menu.setId(Integer.parseInt(elm.attributeValue("id")));
			menu.setText(elm.attributeValue("display"));
			menu.setPid(elm.attributeValue("pid") == null ? 0 : Integer.parseInt(elm.attributeValue("pid")));
			menu.setPriority(Integer.parseInt(elm.attributeValue("priority")));
			menu.setIcon(elm.attributeValue("icon"));
			menu.setUrl(elm.attributeValue("url"));
			Logger.getLogger(MenuService.class).info("Menu url="+menu.getUrl());
			menus.add(menu);
			List children = elm.elements("menu");
			if (children.size() > 0)
			{
				List<Menu> childrenMenus = new ArrayList<Menu>();
				for (Iterator ch = children.iterator(); ch.hasNext();)
				{
					Element element = (Element) ch.next();
					Menu childrenMenu = new Menu();
					childrenMenu.setId(Integer.parseInt(element.attributeValue("id")));
					childrenMenu.setText(element.attributeValue("display"));
					childrenMenu.setPid(element.attributeValue("pid") == null ? 0 : Integer.parseInt(element.attributeValue("pid")));
					childrenMenu.setPriority(Integer.parseInt(element.attributeValue("priority")));
					childrenMenu.setIcon(element.attributeValue("icon"));
					childrenMenu.setUrl(element.attributeValue("url"));
					Logger.getLogger(MenuService.class).info("childrenMenu url="+childrenMenu.getUrl());
					childrenMenus.add(childrenMenu);
				}
				menu.setChildren(childrenMenus);
			}
		}

		return menus;
	}

	// 根据登录账号获取菜单
	@SuppressWarnings("rawtypes")
	public List<Menu> getAll(HttpServletRequest req, int userId)
	{
		AccountMember user = accountMemberService.getUserById(userId);
		List<AccountRoleFunction> funList = accountRoleFunctionService.getBindByRoleId(user.getRoleId());
		List<Integer> funIds = new ArrayList<Integer>();
		for (AccountRoleFunction item : funList)
		{
			int id=item.getFunctionId();
			int temp=id/10;
			if (!funIds.contains(temp)){
				funIds.add(temp);
			}
			funIds.add(id);
		}

		List<Menu> menus = new ArrayList<Menu>();
		Document document = getDocument(req);
		Element rootElm = document.getRootElement();
		List nodes = rootElm.elements("menus");
		for (Iterator it = nodes.iterator(); it.hasNext();)
		{
			Element elm = (Element) it.next();
			int menuId = Integer.parseInt(elm.attributeValue("id"));
			if (funIds.contains(menuId))
			{
				Menu menu = new Menu();
				menu.setId(Integer.parseInt(elm.attributeValue("id")));
				menu.setText(elm.attributeValue("display"));
				menu.setPid(elm.attributeValue("pid") == null ? 0 : Integer.parseInt(elm.attributeValue("pid")));
				menu.setPriority(Integer.parseInt(elm.attributeValue("priority")));
				menu.setIcon(elm.attributeValue("icon"));
				menu.setUrl(elm.attributeValue("url"));
				menus.add(menu);
				List children = elm.elements("menu");
				if (children.size() > 0)
				{
					List<Menu> childrenMenus = new ArrayList<Menu>();
					for (Iterator ch = children.iterator(); ch.hasNext();)
					{
						Element element = (Element) ch.next();
						int childrenId = Integer.parseInt(element.attributeValue("id"));
						if (funIds.contains(childrenId))
						{
							Menu childrenMenu = new Menu();
							childrenMenu.setId(Integer.parseInt(element.attributeValue("id")));
							childrenMenu.setText(element.attributeValue("display"));
							childrenMenu.setPid(element.attributeValue("pid") == null ? 0 : Integer.parseInt(element.attributeValue("pid")));
							childrenMenu.setPriority(Integer.parseInt(element.attributeValue("priority")));
							childrenMenu.setIcon(element.attributeValue("icon"));
							childrenMenu.setUrl(element.attributeValue("url"));
							childrenMenus.add(childrenMenu);
						}
					}
					menu.setChildren(childrenMenus);
				}
			}
		}

		return menus;
	}
}

此方式是一个做UI设计同事写的,拿来给大家分享一下。

你可能感兴趣的:(xml,UI,function,Security)