郁闷了很长一段时间,终于把用Strus Menu做的菜单弄出来了,现把有关东东记录下来,以方便各位有需要的同仁借鉴,也是为了以后不至于找不到,呵呵!
我这里用的是Strus Menu2.4.3,可以直接在
http://struts-menu.sourceforge.net/上下载。
我的例子主要是从数据库中取的数据,并利用Strus Menu进行显示出来(用SSH架构,Sping3.0,Hibernate3.2,Struts1.3.8)。所以我们先要建表。
-- ----------------------------
-- Table structure for menu_item
-- ----------------------------
CREATE TABLE `menu_item` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(60) default NULL,
`parent_name` varchar(60) default NULL,
`title` varchar(60) default NULL,
`description` varchar(60) default NULL,
`location` varchar(255) default NULL,
`target` varchar(60) default NULL,
`onclick` varchar(100) default NULL,
`onmouseover` varchar(100) default NULL,
`onmouseout` varchar(100) default NULL,
`image` varchar(60) default NULL,
`altimage` varchar(60) default NULL,
`tooltip` varchar(160) default NULL,
`roles` varchar(160) default NULL,
`page` varchar(255) default NULL,
`width` varchar(10) default NULL,
`height` varchar(10) default NULL,
`forward` varchar(255) default NULL,
`action` varchar(255) default NULL,
`showSub` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `menu_item` VALUES ('3', 'systemManager', null, 'menu.systemManager', null, null, null, null, null, null, null, null, null, null, null, '140', null, null, null, null);
INSERT INTO `menu_item` VALUES ('4', 'statistiscReport', null, 'menu.statistics.report', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'reportIndex', null, null);
INSERT INTO `menu_item` VALUES ('5', 'musersSetting', 'systemManager', 'menu.admin.usersSetting', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'viewUsers', null, null);
INSERT INTO `menu_item` VALUES ('6', 'personInfo', 'systemManager', 'menu.personInfo', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'personal', null, null);
INSERT INTO `menu_item` VALUES ('7', 'rolesManager', 'systemManager', 'menu.admin.rolesManager', null, null, null, null, null, null, null, null, null, null, null, '140', null, null, null, null);
INSERT INTO `menu_item` VALUES ('8', 'logManager', 'systemManager', 'menu.admin.logManager', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'viewLog', null, null);
INSERT INTO `menu_item` VALUES ('9', 'basicSetting', 'systemManager', 'menu.admin.basicSetting', null, null, null, null, null, null, null, null, null, null, null, '140', null, null, null, null);
INSERT INTO `menu_item` VALUES ('10', 'flushCache', 'systemManager', 'menu.flushCache', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'flush', null, null);
INSERT INTO `menu_item` VALUES ('11', 'clickstream', 'systemManager', 'menu.clickstream', null, null, null, null, null, null, null, null, null, null, null, '140', null, null, null, null);
INSERT INTO `menu_item` VALUES ('12', 'departmentsManager', 'systemManager', 'menu.admin.departmentsManager', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'viewDepartments', null, null);
INSERT INTO `menu_item` VALUES ('13', 'districtAdminIndex', 'systemManager', 'menu.districtmanager', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'districtAdminIndex', null, null);
INSERT INTO `menu_item` VALUES ('14', 'technologySetting', 'systemManager', 'menu.setting.technology', 'TechnologySetting', null, null, null, null, null, null, null, null, null, null, '140', null, 'technologySetting', null, null);
INSERT INTO `menu_item` VALUES ('1', 'AdminMenu', null, 'menu.admin', 'Admin Menu', null, null, null, null, null, null, null, null, null, null, '140', null, null, null, null);
INSERT INTO `menu_item` VALUES ('2', 'BaseInfoManager', null, 'menu.baseInfoManager', 'Base Infomation Manager', null, null, null, null, null, null, null, null, null, null, '140', null, null, null, null);
INSERT INTO `menu_item` VALUES ('15', 'addressManager', 'BaseInfoManager', 'menu.address', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'addressManager', null, null);
INSERT INTO `menu_item` VALUES ('16', 'scheduleManager', 'BaseInfoManager', 'menu.schedule', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'scheduleManager', null, null);
INSERT INTO `menu_item` VALUES ('17', 'worklogManager', 'BaseInfoManager', 'menu.worklog', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'worklogManager', null, null);
INSERT INTO `menu_item` VALUES ('18', 'smsManager', 'BaseInfoManager', 'menu.sms', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'smsManager', null, null);
INSERT INTO `menu_item` VALUES ('19', 'noticeManager', 'BaseInfoManager', 'menu.notice', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'noticeManager', null, null);
INSERT INTO `menu_item` VALUES ('20', 'meetingManager', 'BaseInfoManager', 'menu.meeting', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'meetingManager', null, null);
INSERT INTO `menu_item` VALUES ('21', 'staffManager', 'BaseInfoManager', 'menu.staff', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'staffManager', null, null);
INSERT INTO `menu_item` VALUES ('22', 'fundsManager', 'BaseInfoManager', 'menu.funds', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'fundsManager', null, null);
INSERT INTO `menu_item` VALUES ('23', 'leaveManager', 'BaseInfoManager', 'menu.leave', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'leaveManager', null, null);
INSERT INTO `menu_item` VALUES ('24', 'customerManager', 'BaseInfoManager', 'menu.customer', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'customerManager', null, null);
INSERT INTO `menu_item` VALUES ('25', 'loadManager', 'BaseInfoManager', 'menu.load', null, null, null, null, null, null, null, null, null, null, null, '140', null, 'loadManager', null, null);
建好表,我们用MyEclipse工具加入Spring,Struts,Hibernate的能力,这一步省略。
下面看一下基本的类与配置。
首先是一个Model:MenuItem.java
/*
* @(#)MenuAction.java 1.0 2010-03-05
* Copyright 2010
*/
package com.mis.model;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
* The MenuItem class is responsible for the display of menus.It defines many
* properties for itself.
*
* @author fangyh
* @version 1.0
* @since 2010-03-05
* @see java.lang.Comparable
*
*/
public class MenuItem extends Serializable implements Comparable<Object> {
/**
*
*/
private static final long serialVersionUID = 6679713981266219271L;
/** The composite primary key value. */
private Long id;
/** The value of the simple action property. */
private String action;
/** The value of the simple altimage property. */
private String altimage;
/** The value of the simple description property. */
private String description;
/** The value of the simple forward property. */
private String forward;
/** The value of the simple height property. */
private String height;
/** The value of the simple image property. */
private String image;
/** The value of the simple location property. */
private String location;
/** The value of the simple name property. */
private String name;
/** The value of the simple onclick property. */
private String onclick;
/** The value of the simple onmouseout property. */
private String onmouseout;
/** The value of the simple onmouseover property. */
private String onmouseover;
/** The value of the simple page property. */
private String page;
/** The value of the simple parentName property. */
private String parentName;
/** The value of the simple roles property. */
private String roles;
/** The value of the simple target property. */
private String target;
/** The value of the simple title property. */
private String title;
/** The value of the simple tooltip property. */
private String tooltip;
/** The value of the simple width property. */
private java.lang.String width;
private String showSub;
public String getShowSub() {
return showSub;
}
public void setShowSub(String showSub) {
this.showSub = showSub;
}
/**
* @see com.mis.model.BaseObject#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof MenuItem)) {
return false;
}
MenuItem rhs = (MenuItem) obj;
return new EqualsBuilder().append(this.action, rhs.action).append(
this.width, rhs.width).append(this.title, rhs.title).append(
this.target, rhs.target).append(this.tooltip, rhs.tooltip)
.append(this.onclick, rhs.onclick).append(this.altimage,
rhs.altimage).append(this.roles, rhs.roles).append(
this.onmouseout, rhs.onmouseout)
.append(this.id, rhs.id).append(this.forward, rhs.forward)
.append(this.height, rhs.height).append(this.page, rhs.page)
.append(this.description, rhs.description).append(
this.parentName, rhs.parentName).append(this.image,
rhs.image).append(this.location, rhs.location).append(
this.name, rhs.name).append(this.onmouseover,
rhs.onmouseover).isEquals();
}
/**
* @see com.mis.model.BaseObject#hashCode()
*/
@Override
public int hashCode() {
return new HashCodeBuilder(-1103911661, 1780210383).append(this.action)
.append(this.width).append(this.title).append(this.target)
.append(this.tooltip).append(this.onclick)
.append(this.altimage).append(this.roles).append(
this.onmouseout).append(this.id).append(this.forward)
.append(this.height).append(this.page).append(this.description)
.append(this.parentName).append(this.image).append(
this.location).append(this.name).append(
this.onmouseover).toHashCode();
}
/**
* @see com.mis.model.BaseObject#toString()
*/
@Override
public String toString() {
return new ToStringBuilder(this).append("altimage", this.altimage)
.append("onclick", this.onclick)
.append("forward", this.forward).append("page", this.page)
.append("action", this.action).append("id", this.id).append(
"roles", this.roles).append("height", this.height)
.append("location", this.location).append("tooltip",
this.tooltip).append("name", this.name).append(
"onmouseover", this.onmouseover).append("description",
this.description).append("image", this.image).append(
"parentName", this.parentName).append("width",
this.width).append("title", this.title).append(
"onmouseout", this.onmouseout).append("target",
this.target).toString();
}
public int compareTo(Object obj) {
MenuItem myClass = (MenuItem) obj;
return new CompareToBuilder().append(this.action, myClass.action)
.append(this.width, myClass.width).append(this.title,
myClass.title).append(this.target, myClass.target)
.append(this.tooltip, myClass.tooltip).append(this.onclick,
myClass.onclick)
.append(this.altimage, myClass.altimage).append(this.roles,
myClass.roles).append(this.onmouseout,
myClass.onmouseout).append(this.id, myClass.id).append(
this.forward, myClass.forward).append(this.height,
myClass.height).append(this.page, myClass.page).append(
this.description, myClass.description).append(
this.parentName, myClass.parentName).append(this.image,
myClass.image).append(this.location, myClass.location)
.append(this.name, myClass.name).append(this.onmouseover,
myClass.onmouseover).toComparison();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAction() {
return action;
}
public String getRoles() {
return roles;
}
public void setRoles(String roles) {
this.roles = roles;
}
public void setAction(String action) {
this.action = action;
}
public String getAltimage() {
return altimage;
}
public void setAltimage(String altimage) {
this.altimage = altimage;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getForward() {
return forward;
}
public void setForward(String forward) {
this.forward = forward;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOnclick() {
return onclick;
}
public void setOnclick(String onclick) {
this.onclick = onclick;
}
public String getOnmouseout() {
return onmouseout;
}
public void setOnmouseout(String onmouseout) {
this.onmouseout = onmouseout;
}
public String getOnmouseover() {
return onmouseover;
}
public void setOnmouseover(String onmouseover) {
this.onmouseover = onmouseover;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTooltip() {
return tooltip;
}
public void setTooltip(String tooltip) {
this.tooltip = tooltip;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
}
其实是DAO:IMenuItemDao接口与实现类MenuItemDaoImpl
package com.mis.dao.hibernate;
import java.util.List;
import com.mis.dao.DAO;
import com.mis.model.MenuItem;
public interface IMenuItemDao extends DAO{
public List<MenuItem> getMenuItems();
}
package com.mis.dao.hibernate.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.mis.dao.hibernate.IMenuItemDao;
import com.mis.model.MenuItem;
public class MenuItemDaoImpl extends HibernateDaoSupportimplements IMenuItemDao {
@SuppressWarnings("unchecked")
public List<MenuItem> getMenuItems() {
String hql = "from MenuItem";
return getHibernateTemplate().find(hql);
}
}
再者是Hibernate的配置文件MenuItem.hbm.xml
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Tue Aug 28 18:29:28 CST 2007 -->
<hibernate-mapping package="com.mis.model">
<class name="MenuItem" table="menu_item">
<id name="id" column="id" type="java.lang.Long">
<generator class="native" />
</id>
<property name="action" column="action" type="java.lang.String" />
<property name="altimage" column="altimage" type="java.lang.String" />
<property name="description" column="description" type="java.lang.String" />
<property name="forward" column="forward" type="java.lang.String" />
<property name="height" column="height" type="java.lang.String" />
<property name="image" column="image" type="java.lang.String" />
<property name="location" column="location" type="java.lang.String" />
<property name="name" column="name" type="java.lang.String" />
<property name="onclick" column="onclick" type="java.lang.String" />
<property name="onmouseout" column="onmouseout" type="java.lang.String" />
<property name="onmouseover" column="onmouseover" type="java.lang.String" />
<property name="page" column="page" type="java.lang.String" />
<property name="parentName" column="parent_name" type="java.lang.String" />
<property name="target" column="target" type="java.lang.String" />
<property name="title" column="title" type="java.lang.String" />
<property name="tooltip" column="tooltip" type="java.lang.String" />
<property name="width" column="width" type="java.lang.String" />
<property name="showSub" column="showSub" type="java.lang.String" />
<property name="roles" column="roles" type="java.lang.String" />
</class>
</hibernate-mapping>
好了,DAO层结束了,接下来就是业务层(Service)。接口IMenuItemManager.java和实现类MenuItemManagerImpl.java
/*
* @(#)IMenuItemManager.java 1.0 2010-03-05
* Copyright 2010
*/
package com.mis.service;
import java.util.List;
import com.mis.model.MenuItem;
/**
* @author fangyh
* @version 1.0
* @since 2010-03-05 17:12:22
*
*/
public interface IMenuItemManager{
/**
*
* @return
*/
List<MenuItem> getMenuItems();
/**
*
* @param userName
* @return
*/
List<MenuItem> getMenuItemsByUser(String userName);
}
package com.mis.service.impl;
import java.util.List;
import com.mis.dao.hibernate.IMenuItemDao;
import com.mis.model.MenuItem;
import com.mis.service.IMenuItemManager;
public class MenuItemManagerImpl implements IMenuItemManager {
private IMenuItemDao menuItemDao;
public MenuItemManagerImpl() {
}
public IMenuItemDao getMenuItemDao() {
return menuItemDao;
}
public void setMenuItemDao(IMenuItemDao menuItemDao) {
this.menuItemDao = menuItemDao;
}
public List<MenuItem> getMenuItems() {
return menuItemDao.getMenuItems();
}
public List<MenuItem> getMenuItemsByUser(String userName) {
return null;
}
}
这个没什么好说的,就是调用DAO。
下面就是WEB层,Struts. MenuItemForm MenuAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.mis.action.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 03-09-2010
*
* XDoclet definition:
* @struts.form name="menuItemForm"
*/
public class MenuItemForm extends ActionForm {
/*
* Generated fields
*/
/**
*
*/
private static final long serialVersionUID = 2104278779659656910L;
/** showSub property */
private Boolean showSub;
/** location property */
private String location;
/** onmouseout property */
private String onmouseout;
/** width property */
private String width;
/** image property */
private String image;
/** altimage property */
private String altimage;
/** onmouseover property */
private String onmouseover;
/** id property */
private Long id;
/** title property */
private String title;
/** height property */
private String height;
/** forward property */
private String forward;
/** page property */
private String page;
/** description property */
private String description;
/** roles property */
private String roles;
/** name property */
private String name;
/** target property */
private String target;
/** action property */
private String action;
/** onclick property */
private String onclick;
/** tooltip property */
private String tooltip;
/** parentName property */
private String parentName;
/*
* Generated Methods
*/
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
}
/**
* Returns the showSub.
* @return Boolean
*/
public Boolean getShowSub() {
return showSub;
}
/**
* Set the showSub.
* @param showSub The showSub to set
*/
public void setShowSub(Boolean showSub) {
this.showSub = showSub;
}
/**
* Returns the location.
* @return String
*/
public String getLocation() {
return location;
}
/**
* Set the location.
* @param location The location to set
*/
public void setLocation(String location) {
this.location = location;
}
/**
* Returns the onmouseout.
* @return String
*/
public String getOnmouseout() {
return onmouseout;
}
/**
* Set the onmouseout.
* @param onmouseout The onmouseout to set
*/
public void setOnmouseout(String onmouseout) {
this.onmouseout = onmouseout;
}
/**
* Returns the width.
* @return String
*/
public String getWidth() {
return width;
}
/**
* Set the width.
* @param width The width to set
*/
public void setWidth(String width) {
this.width = width;
}
/**
* Returns the image.
* @return String
*/
public String getImage() {
return image;
}
/**
* Set the image.
* @param image The image to set
*/
public void setImage(String image) {
this.image = image;
}
/**
* Returns the altimage.
* @return String
*/
public String getAltimage() {
return altimage;
}
/**
* Set the altimage.
* @param altimage The altimage to set
*/
public void setAltimage(String altimage) {
this.altimage = altimage;
}
/**
* Returns the onmouseover.
* @return String
*/
public String getOnmouseover() {
return onmouseover;
}
/**
* Set the onmouseover.
* @param onmouseover The onmouseover to set
*/
public void setOnmouseover(String onmouseover) {
this.onmouseover = onmouseover;
}
/**
* Returns the id.
* @return Long
*/
public Long getId() {
return id;
}
/**
* Set the id.
* @param id The id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* Returns the title.
* @return String
*/
public String getTitle() {
return title;
}
/**
* Set the title.
* @param title The title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* Returns the height.
* @return String
*/
public String getHeight() {
return height;
}
/**
* Set the height.
* @param height The height to set
*/
public void setHeight(String height) {
this.height = height;
}
/**
* Returns the forward.
* @return String
*/
public String getForward() {
return forward;
}
/**
* Set the forward.
* @param forward The forward to set
*/
public void setForward(String forward) {
this.forward = forward;
}
/**
* Returns the page.
* @return String
*/
// public String getPage() {
// return page;
// }
/**
* Set the page.
* @param page The page to set
*/
public void setPage(String page) {
this.page = page;
}
/**
* Returns the description.
* @return String
*/
public String getDescription() {
return description;
}
/**
* Set the description.
* @param description The description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* Returns the roles.
* @return String
*/
public String getRoles() {
return roles;
}
/**
* Set the roles.
* @param roles The roles to set
*/
public void setRoles(String roles) {
this.roles = roles;
}
/**
* Returns the name.
* @return String
*/
public String getName() {
return name;
}
/**
* Set the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns the target.
* @return String
*/
public String getTarget() {
return target;
}
/**
* Set the target.
* @param target The target to set
*/
public void setTarget(String target) {
this.target = target;
}
/**
* Returns the action.
* @return String
*/
public String getAction() {
return action;
}
/**
* Set the action.
* @param action The action to set
*/
public void setAction(String action) {
this.action = action;
}
/**
* Returns the onclick.
* @return String
*/
public String getOnclick() {
return onclick;
}
/**
* Set the onclick.
* @param onclick The onclick to set
*/
public void setOnclick(String onclick) {
this.onclick = onclick;
}
/**
* Returns the tooltip.
* @return String
*/
public String getTooltip() {
return tooltip;
}
/**
* Set the tooltip.
* @param tooltip The tooltip to set
*/
public void setTooltip(String tooltip) {
this.tooltip = tooltip;
}
/**
* Returns the parentName.
* @return String
*/
public String getParentName() {
return parentName;
}
/**
* Set the parentName.
* @param parentName The parentName to set
*/
public void setParentName(String parentName) {
this.parentName = parentName;
}
}
/*
* @(#)MenuAction.java 1.0 2010-03-05
* Copyright 2010
*/
package com.mis.action.struts.action;
import com.mis.action.struts.BaseAction;
import com.mis.model.MenuItem;
import com.mis.service.IMenuItemManager;
import com.mis.util.constants.Constants;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.navigator.menu.MenuComponent;
import net.sf.navigator.menu.MenuRepository;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* The MenuAction class is responsible for obtaining a menu and creating a new
* menu.
*
* @struts.action path="/menulist" type="com.mis.action.struts.action.MenuAction"
* name="menuItemForm" scope="request" parameter="method"
* unknown="false" validate="false"
*
* @struts.action-forward name="menuok" path="/" redirect="false"
*/
public class MenuAction extends Action {
/**
*
*/
private static final long serialVersionUID = -7414899074527890160L;
private IMenuItemManager menuItemManager;
public IMenuItemManager getMenuItemManager() {
return menuItemManager;
}
public void setMenuItemManager(IMenuItemManager menuItemManager) {
this.menuItemManager = menuItemManager;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
log.info("enter MenuAction---->execute.......");
List<MenuItem> list = menuItemManager.getMenuItems();
this.getMenuRepository(list, request);
if (log.isDebugEnabled()) {
log.debug("Menu init complete [OK]");
}
return mapping.findForward("menuok");
}
/**
* get menu repository in order to support conveniences for that will invoke
* them.
*
* @param list
* the menus list
* @param request
* @return the menu repository
*/
private MenuRepository getMenuRepository(List<?> list,
HttpServletRequest request) {
MenuRepository repository = new MenuRepository();
repository.removeAllMenus();
HttpSession httpsession = (HttpSession) request.getSession();
ServletContext application = (ServletContext) httpsession
.getServletContext();
MenuRepository defaultRepository = null;
try {
defaultRepository = (MenuRepository) application
.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);
repository.setDisplayers(defaultRepository.getDisplayers());
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < list.size(); i++) {
MenuComponent mc = new MenuComponent();
MenuItem mi = (MenuItem) list.get(i);
String name = mi.getName();
mc.setName(name);
String parent = (String) mi.getParentName();
System.out.println(name + ", parent is: " + parent);
// Creates the parent menu.If the parent menu is null,we can create
// one.Otherwise we can set it.
if (parent != null) {
MenuComponent parentMenu = repository.getMenu(parent);
if (parentMenu == null) {
System.out.println("parentMenu '" + parent
+ "' doesn't exist!");
// create a temporary parentMenu
parentMenu = new MenuComponent();
parentMenu.setName(parent);
repository.addMenu(parentMenu);
}
mc.setParent(parentMenu);
}
mc.setTitle(mi.getTitle());
mc.setRoles(mi.getRoles());
mc.setAction(mi.getAction());
mc.setForward(mi.getForward());
mc.setLocation(mi.getLocation());
mc.setDescription(mi.getDescription());
mc.setAltImage(mi.getAltimage());
mc.setHeight(mi.getHeight());
mc.setImage(mi.getImage());
mc.setOnclick(mi.getOnclick());
mc.setOnmouseout(mi.getOnmouseout());
mc.setOnmouseover(mi.getOnmouseover());
mc.setTarget(mi.getTarget());
mc.setToolTip(mi.getTooltip());
mc.setPage(mi.getPage());
mc.setWidth(mi.getWidth());
mc.setHeight(mi.getHeight());
repository.addMenu(mc);
}
application.setAttribute(Constants.SESSION_STRUCT_MENU, repository);
return repository;
}
}
struts的原理在这里就不说了,现在需要说明的是,getMenuRepository方法,就是获取MenuRepository,
接下来就是struts的配置文件struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="menuItemForm" type="com.mis.action.struts.form.MenuItemForm" />
</form-beans>
<global-exceptions />
<global-forwards>
<forward name="reportIndex" path="/reportTreeIndex.do?method=reportIndex"
redirect="true" />
<forward name="userManager" path="/reportTreeIndex.do?method=userManager"
redirect="true" />
<forward name="taskManager" path="/reportTreeIndex.do?method=taskManager"
redirect="true" />
<forward name="logManager" path="/reportTreeIndex.do?method=logManager"
redirect="true" />
<forward name="departmentManager" path="/reportTreeIndex.do?method=logManager"
redirect="true" />
<forward name="roleManager" path="/reportTreeIndex.do?method=roleManager"
redirect="true" />
<forward name="viewUsers" path="/reportTreeIndex.do?method=roleManager"
redirect="true" />
<forward name="viewDepartments" path="/reportTreeIndex.do?method=roleManager"
redirect="true" />
<forward name="personal" path="/reportTreeIndex.do?method=roleManager"
redirect="true" />
<forward name="viewsysparam" path="/reportTreeIndex.do?method=roleManager"
redirect="true" />
<forward name="viewLog" path="/reportTreeIndex.do?method=roleManager"
redirect="true" />
<forward name="flush" path="/reportTreeIndex.do?method=roleManager"
redirect="true" />
<forward name="technologySetting" path="/reportTreeIndex.do?method=roleManager"
redirect="true" />
<forward name="viewRoles" path="/reportTreeIndex.do?method=roleManager"
redirect="true" />
<forward name="districtAdminIndex" path="/reportTreeIndex.do?method=roleManager"
redirect="true" />
<forward name="addressManager" path="/addressAction.do?method=refresh"
redirect="true" />
<forward name="scheduleManager" path="/scheduleAction.do?method=refresh"
redirect="true" />
<forward name="worklogManager" path="/worklogAction.do?method=refresh"
redirect="true" />
<forward name="smsManager" path="/smsAction.do?method=refresh"
redirect="true" />
<forward name="noticeManager" path="/noticeAction.do?method=refresh"
redirect="true" />
<forward name="meetingManager" path="/meetingAction.do?method=refresh"
redirect="true" />
<forward name="staffManager" path="/staffAction.do?method=refresh"
redirect="true" />
<forward name="fundsManager" path="/fundsAction.do?method=refresh"
redirect="true" />
<forward name="leaveManager" path="/leaveAction.do?method=refresh"
redirect="true" />
<forward name="customerManager" path="/customerAction.do?method=refresh"
redirect="true" />
<forward name="loadManager" path="/loadAction.do?method=refresh"
redirect="true" />
</global-forwards>
<action-mappings>
<action path="/menuAction" attribute="menuItemForm" name="menuItemForm"
scope="request" parameter="method" unknown="false" validate="false"
type="org.springframework.web.struts.DelegatingActionProxy"
cancellable="true">
<forward name="menuok" path="/WEB-INF/pages/menu/menu.jsp"
redirect="false" />
</action>
</action-mappings>
<message-resources parameter="com.mis.util.resources.ApplicationResources" />
<!--
The menuConfig property is an optional attribute. It is set to
/WEB-INF/menu-config.xml by default.
-->
<plug-in className="net.sf.navigator.menu.MenuPlugIn"> <set-property
property="menuConfig" value="/WEB-INF/menu-config.xml" /> </plug-in>
</struts-config>
其中<plug-in className="net.sf.navigator.menu.MenuPlugIn"> <set-property
property="menuConfig" value="/WEB-INF/menu-config.xml" /> </plug-in>就是加载menu-config.xml
这个有三种加载方式:
以下为一种方式。
第二种方式:在web.xml中配置
<listener>
<listener-class>net.sf.navigator.menu.MenuContextListener</listener-class>
</listener>
第三种方式:在applicationContext.xml中配置
<bean id="menu" class="net.sf.navigator.menu.MenuLoader">
<property name="menuConfig" value="/WEB-INF/menu-config.xml"/>
</bean>
接下来看menu-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<MenuConfig>
<Displayers>
<Displayer name="DropDown"
type="net.sf.navigator.displayer.DropDownMenuDisplayer" />
<Displayer name="Simple"
type="net.sf.navigator.displayer.SimpleMenuDisplayer" />
<Displayer name="CoolMenu"
type="net.sf.navigator.displayer.CoolMenuDisplayer" />
<Displayer name="CoolMenu4"
type="net.sf.navigator.displayer.CoolMenuDisplayer4" />
<Displayer name="ListMenu"
type="net.sf.navigator.displayer.ListMenuDisplayer" />
<Displayer name="TabbedMenu"
type="net.sf.navigator.displayer.TabbedMenuDisplayer" />
<Displayer name="Velocity"
type="net.sf.navigator.displayer.VelocityMenuDisplayer" />
<Displayer name="XtreeMenu"
type="net.sf.navigator.displayer.XtreeMenuDisplayer" />
<Displayer name="CSSListMenu"
type="net.sf.navigator.displayer.CSSListMenuDisplayer" />
</Displayers>
<Menus>
<Menu></Menu>
</Menus>
</MenuConfig>
applicationContext.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- set the configuration file -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}">
</property>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/mis/dao/hibernate/sql/MenuItem.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<!--
<prop key="hibernate.cglib.use_reflection_optimizer"> true </prop>
-->
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.HashtableCacheProvider
</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean
class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
</entry>
</map>
</property>
</bean>
<!-- Hibernate Template Defintion -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"
lazy-init="true">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="jdbcExceptionTranslator">
<ref bean="jdbcExceptionTranslator" />
</property>
<property name="cacheQueries">
<value>true</value>
</property>
</bean>
<bean id="menuItemDao" class="com.mis.dao.hibernate.impl.MenuItemDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="menuItemManager" class="com.mis.service.impl.MenuItemManagerImpl">
<property name="menuItemDao" ref="menuItemDao" />
</bean>
<bean name="/menuAction" class="com.mis.action.struts.action.MenuAction">
<property name="menuItemManager" ref="menuItemManager" />
</bean>
</beans>
最后就是显示页面:index.jsp,menu.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/commons/taglibs.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<br />
<br>
<br>
<form action="menuAction.do" method="post">
<input type="submit" value="submit" />
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/commons/taglibs.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>ok</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" media="screen"
href="styles/global.css" />
<link rel="stylesheet" type="text/css" media="screen"
href="styles/coolmenu.css" />
<script type="text/javascript" src="scripts/coolmenus4.js"></script>
<script type="text/javascript" src="scripts/cm_addins.js"></script>
</head>
<body>
<br />
<table>
<div>
<!-- Custom Configuration for this example, must come after body to work in IE -->
<script type="text/javascript" src="scripts/coolmenu4-config.js"></script>
<menu:useMenuDisplayer name="CoolMenu4" repository="repository"
config="/templates/coolmenus.html">
<c:forEach var="menu" items="${repository.topMenus}">
<menu-el:displayMenu name="${menu.name}" />
</c:forEach>
</menu:useMenuDisplayer>
</div>
</table>
</body>
</html>
至此,整个用SSH实现的Struts menu算做完了。有点多哈。下面把源代码传上,没JAR包哈,自己下吧,太大了。