12.首页一级分类的显示

首页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;

2、建包及相应的类

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

}

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


3、对Service和DAO进行配置
applicationContext.xml

加入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>

4、需要在IndexAction中注入一级分类的Service,将一级分类的数据存入到session中
IndexAction.java

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";
	}
}

5、将一级分类的数据显示到页面中

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值