Home index.jsp The upper level category shows the included menu.jsp, and the first level category on the page is displayed when you enter the page, so you want to query the first level of classification information in Indexaction and then return to the logical view.
1. Create a table of first class classification
CREATE TABLE ' category ' ( ' cid ' int (one) not null auto_increment, ' cname ' varchar (255) DEFAULT NULL, PRIMARY K EY (' CID ')) Engine=innodb auto_increment=8 DEFAULT Charset=utf8;
2. Building packages and corresponding classes
Cn.xdy.shop.category
* Action
* Service
* Categoryservice: Business Layer Object
* DAO
* CategoryDao: Persistent Layer Object
* VO
* Category: entity Object
* Category.hbm.xml: Map File
Categoryservice.java
Package Cn.xdy.shop.category.service;import Java.util.list;import Cn.xdy.shop.category.dao.categorydao;import cn.xdy.shop.category.vo.category;/** * Business tier for Class I * @author asus * */public class Categoryservice {private CategoryDao Categ orydao;public void Setcategorydao (CategoryDao categorydao) {This.categorydao = CategoryDao;} /** * Query All Categories * @return */public list<category> findAll () {return Categorydao.findall ();}}
Categorydao.java
Package Cn.xdy.shop.category.dao;import Java.util.list;import Org.springframework.orm.hibernate3.support.hibernatedaosupport;import Cn.xdy.shop.category.vo.category;public Class CategoryDao extends Hibernatedaosupport{public list<category> findAll () {String hql = "from Category"; list<category> list = This.gethibernatetemplate (). Find (HQL); return list;}}
Category.hbm.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">
3. Configure the service and DAO
Applicationcontext.xmlJoin Category.hbm.xml
<!--configuring Hibernate's Mapping File--><property name= "Mappingresources" ><list><value>cn/xdy/shop/user/ Vo/user.hbm.xml</value><value>cn/xdy/shop/category/vo/category.hbm.xml</value></list> </property>
Join the service and DAO configuration
<bean id= "Categoryservice" class= "Cn.xdy.shop.category.service.CategoryService" ><property name= " CategoryDao "ref=" CategoryDao "></property></bean>
<bean id= "CategoryDao" class= "Cn.xdy.shop.category.dao.CategoryDao" ><property name= "sessionfactory" ref= " Sessionfactory "></property></bean>
4, the need to inject a class of service in the Indexaction, the first-class classification of data into the session
Indexaction.javaPackage Cn.xdy.shop.index.action;import Java.util.list;import Org.apache.struts2.servletactioncontext;import Cn.xdy.shop.category.service.categoryservice;import Cn.xdy.shop.category.vo.category;import Com.opensymphony.xwork2.actioncontext;import com.opensymphony.xwork2.actionsupport;/** * Home Access action * @author ASUS * */public class Indexaction extends actionsupport{private categoryservice categoryservice;public void Setcategoryservice (Categoryservice categoryservice) {this.categoryservice = Categoryservice;} /** * Perform a method of accessing the home page */public String execute () throws Exception {list<category> cList = Categoryservice.findall (); Actioncontext.getcontext (). GetSession (). Put ("CList", cList); return "Index";}}
5, the first level classification of data display to the pagemenu.jsp
<div class= "Span24" ><ul class= "Mainnav" ><li><a href= "${pagecontext.request.contextpath}/ Index.action "> Home </a>|</li><s:iterator var=" C "value=" #session. CList "><li><a href=". /vegetable classification. htm "><s:property value=" #c. CNAME "/></a>|</li></s:iterator></ul></div >
12. First level category display