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