基本配置我就不多说了,网上多的很,我这里只说一下具体实现,呵呵
采用
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
=
&quo