我的Blog我做主^_^

          走向一條通往JAVA的不歸路...

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            64 隨筆 :: 68 文章 :: 77 評論 :: 0 Trackbacks

          基本配置我就不多說了,網(wǎng)上多的很,我這里只說一下具體實現(xiàn),呵呵

           

          采用Struts+Hibernate

          一、新建菜單表:表根據(jù)配置文件自己建吧,我這里就不寫了

          二、建立表對應的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();

                        //創(chuàng)建事務

                        Transaction tx=session.beginTransaction();

                        //創(chuàng)建對話

                        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="網(wǎng)易"/>

                  </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_蟈蟈 閱讀(2420) 評論(1)  編輯  收藏

          評論

          # re: Struts-menu 權限控制 2011-08-23 09:18 jspc
          學習著  回復  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 宜昌市| 安塞县| 休宁县| 顺昌县| 什邡市| 洛浦县| 湖北省| 开封县| 施秉县| 利川市| 荣成市| 寿光市| 广德县| 祁门县| 略阳县| 安宁市| 贵阳市| 枞阳县| 哈巴河县| 平顶山市| 阿鲁科尔沁旗| 奎屯市| 花垣县| 高阳县| 深圳市| 永仁县| 海晏县| 滨海县| 上林县| 隆德县| 寿阳县| 溧水县| 梧州市| 南城县| 南溪县| 保靖县| 柘荣县| 霍城县| 长寿区| 灵宝市| 龙海市|