我的Blog我做主^_^

走向一条通往JAVA的不归路...

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  64 随笔 :: 68 文章 :: 77 评论 :: 0 Trackbacks

基本配置我就不多说了,网上多的很,我这里只说一下具体实现,呵呵

 

采用Struts+Hibernate

一、新建菜单表:表根据配置文件自己建吧,我这里就不写了

二、建立表对应的Hibernate的配置文件及JAVABEAN

<?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">

<!--

    Mapping file autogenerated by MyEclipse - Hibernate Tools

-->

<hibernate-mapping>

    <class name="com.wz.hibernate.Menu" table="menu" catalog="gknews">

        <id name="id" type="java.lang.Integer">

            <column name="id" />

            <generator class="native" />

        </id>

        <property name="parent" type="java.lang.String">

            <column name="parent" not-null="true" />

        </property>

        <property name="name" type="java.lang.String">

            <column name="name" length="45" not-null="true" />

        </property>

        <property name="target" type="java.lang.String">

            <column name="target" length="45" not-null="true" />

        </property>

    </class>

</hibernate-mapping>

package com.wz.hibernate;

 

public class Menu  implements java.io.Serializable {

 

     private Integer id;

     private String parent;

     private String name;

     private String target;

 

    /** default constructor */

    public Menu() {

    }

    /** full constructor */

    public Menu(String parent, String name, String target) {

        this.parent = parent;

        this.name = name;

        this.target = target;

    }  

    // Property accessors

    public Integer getId() {

        return this.id;

    }   

    public void setId(Integer id) {

        this.id = id;

    }

    public String getParent() {

        return this.parent;

    }   

    public void setParentId(String parent) {

        this.parent = parent;

    }

    public String getName() {

        return this.name;

    }   

    public void setName(String name) {

        this.name = name;

    }

    public String getTarget() {

        return this.target;

    }   

    public void setTarget(String target) {

        this.target = target;

}

}

三、ActionForm

/*

 * Generated by MyEclipse Struts

 * Template path: templates/java/JavaClass.vtl

 */

package com.wz.struts_menu.struts.form;

 

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

public class TestMenuForm extends ActionForm {

      

       private String parent;

       private String title;

       private String target;

       private Integer id;

       public ActionErrors validate(ActionMapping mapping,

                     HttpServletRequest request) {

              // TODO Auto-generated method stub

              return null;

       }

       public void reset(ActionMapping mapping, HttpServletRequest request) {

              // TODO Auto-generated method stub

       }

       public String getParent() {

              return parent;

       }

       public void setParentId(String parent) {

              this.parent = parent;

       }

       public String getTitle() {

              return title;

       }

       public void setTitle(String title) {

              this.title = title;

       }

       public String getTarget() {

              return target;

       }

       public void setTarget(String target) {

              this.target = target;

       }

       public Integer getId() {

              return id;

       }

       public void setId(Integer id) {

              this.id = id;

       }

}

四、Action

/*

 * Generated by MyEclipse Struts

 * Template path: templates/java/JavaClass.vtl

 */

package com.wz.struts_menu.struts.action;

 

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.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.Transaction;

 

import com.wz.hibernate.Menu;

import com.wz.hibernate.MenuItem;

import com.wz.hibernate.SessionFactory;

import com.wz.struts_menu.struts.form.TestMenuForm;

public class TestMenuAction extends Action {

      

       public ActionForward execute(ActionMapping mapping, ActionForm form,

                     HttpServletRequest request, HttpServletResponse response) {

              TestMenuForm testMenuForm = (TestMenuForm) form;// TODO Auto-generated method stub

             

              Session session=SessionFactory.getSession();

              //创建事务

              Transaction tx=session.beginTransaction();

              //创建对话

              Query query=session.createQuery("FROM Menu m order by id");

              List list=query.list();

              //事务提交

              tx.commit();

             

              if(list.size()<0)

                     return mapping.getInputForward();

             

              MenuRepository repository = new MenuRepository();

             

              HttpSession httpsession=(HttpSession)request.getSession();

             

              ServletContext application=(ServletContext)httpsession.getServletContext();

             

              MenuRepository defaultRepository = (MenuRepository)application.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);

             

              repository.setDisplayers(defaultRepository.getDisplayers());

             

             for (int i=0; i < list.size(); i++) {

                   

                    MenuComponent mc = new MenuComponent();

                   

                    Menu mi=(Menu) list.get(i);

                   

                    String name = mi.getName();       

            mc.setName(name);

           

            String parent = (String) mi.getParentId();

           

            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);

            }

            String title = (String)mi.getName();

            mc.setTitle(title);

            String location = (String) mi.getTarget();

            mc.setLocation(location);

            repository.addMenu(mc);

              }

             request.setAttribute("repository", repository);

              return mapping.findForward("okGo");

       }    

}

 

五、JSP

<%@ page contentType="text/html;charset=UTF-8" language="java"%>

<%@ taglib uri="/WEB-INF/struts-menu.tld" prefix="menu" %>

<%@ taglib uri="/WEB-INF/struts-menu-el.tld" prefix="menu-el" %>

<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

    <title>Dynamic, Database-driven Menu</title>

    <link rel="stylesheet" type="text/css" media="all"

        href="<c:url value="/styles/menuExpandable.css"/>" />

    <script type="text/javascript"

        src="<c:url value="/scripts/menuExpandable.js"/>"></script>

    <link rel="stylesheet" type="text/css" media="all" href="<c:url value="/styles/xtree.css"/>" />

    <script type="text/javascript" src="<c:url alue="/scripts/xtree.js"/>"></script>

    <link rel="stylesheet" type="text/css" media="all" href="<c:url value="/styles/global.css"/>" />

    <script type="text/javascript">

        /* Function for showing and hiding elements that use 'display:none' to hide */

        function toggleDisplay(targetId) {

            if (document.getElementById) {

                target = document.getElementById(targetId);

                if (target.style.display == "none"){

                    target.style.display = "";

                } else {

                    target.style.display = "none";

                }

            }

        }

    </script>

</head>

<body>

    <div class="dynamicMenu">

        <menu:useMenuDisplayer name="ListMenu" repository="repository">

            <menu:displayMenu name="新浪"/>

            <menu:displayMenu name="网易"/>

        </menu:useMenuDisplayer>

    </div>

</body>

</html>

 

六、Struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

 

<struts-config>

  <data-sources />

  <form-beans />

  <global-exceptions />

  <global-forwards >   

    <forward name="okGo" path="/ok.jsp" /> 

  </global-forwards>

  <action-mappings >

    <action path="/testAction" type="com.wz.struts_menu.struts.action.TestMenuAction" />

  </action-mappings>

  <message-resources parameter="com.wz.struts_menu.struts.ApplicationResources" />

 

  <plug-in className="net.sf.navigator.menu.MenuPlugIn">

    <set-property property="menuConfig" value="/WEB-INF/menu-config.xml" />

  </plug-in>

</struts-config>

 

这样就算完成了,呵呵;学习中……

 



posted on 2007-05-17 18:54 java_蝈蝈 阅读(2414) 评论(1)  编辑  收藏

评论

# re: Struts-menu 权限控制 2011-08-23 09:18 jspc
学习着  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: