标签:
首页index.jsp上面一级分类显示是包含的menu.jsp,页面上的一级分类是在进入主页时就显示,所以要在IndexAction中查询到一级分类信息后再返回逻辑视图。
1、先创建一级分类的表
CREATE TABLE `category` ( `cid` int(11) NOT NULL AUTO_INCREMENT, `cname` varchar(255) DEFAULT NULL, PRIMARY KEY (`cid`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
cn.xdy.shop.category
* action
* service
* CategoryService:业务层对象
* dao
* CategoryDao:持久层对象
* vo
* Category:实体对象
* Category.hbm.xml:映射文件
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; /** * 一级分类的业务层 * @author asus * */ public class CategoryService { private CategoryDao categoryDao; public void setCategoryDao(CategoryDao categoryDao) { this.categoryDao = categoryDao; } /** * 查询所有分类 * @return */ public List<Category> findAll() { return categoryDao.findAll(); } }
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; } }
<?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"> <hibernate-mapping> <class name="cn.xdy.shop.category.vo.Category" table="category"> <id name="cid"> <generator class="native"/> </id> <property name="cname"/> </class> </hibernate-mapping>
加入Category.hbm.xml
<!-- 配置Hibernate的映射文件 --> <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>
加入service和dao的配置
<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>
package 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; /** * 首页访问的action * @author asus * */ public class IndexAction extends ActionSupport{ private CategoryService categoryService; public void setCategoryService(CategoryService categoryService) { this.categoryService = categoryService; } /** * 执行访问首页的方法 */ public String execute() throws Exception { List<Category> cList = categoryService.findAll(); ActionContext.getContext().getSession().put("cList", cList); return "index"; } }
menu.jsp
<div class="span24"> <ul class="mainNav"> <li><a href="${pageContext.request.contextPath }/index.action">首页</a>|</li> <s:iterator var="c" value="#session.cList"> <li><a href="./蔬菜分类.htm"><s:property value="#c.cname"/></a>|</li> </s:iterator> </ul> </div>
标签:
原文地址:http://blog.csdn.net/xiongwt/article/details/45347195