今天在做PetStore时要用到doubleselect,但数据需要从数据库里提取.说说简单,做做难啊,下面,给大家看下代码:
sql1:
引用
DROP TABLE IF EXISTS `petstore`.`category`;
CREATE TABLE `petstore`.`category` (
`category_id` int(10) unsigned NOT NULL auto_increment COMMENT '类别ID',
`category_name` varchar(45) character set utf8 NOT NULL COMMENT '类别名',
`category_desc` varchar(500) character set utf8 default NULL COMMENT '详细描述',
`category_url` varchar(100) character set utf8 default NULL COMMENT '图片URL',
PRIMARY KEY (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
sql2:
引用
DROP TABLE IF EXISTS `petstore`.`product`;
CREATE TABLE `petstore`.`product` (
`product _id` int(10) unsigned NOT NULL auto_increment COMMENT '子类别ID',
`product _name` varchar(45) NOT NULL COMMENT '子类别名',
`product _desc` varchar(500) default NULL COMMENT '子类别图片URL',
`product _url` varchar(100) default NULL COMMENT '详细描述',
`category_id` int(10) unsigned NOT NULL COMMENT '类别ID',
PRIMARY KEY (`product _id`),
KEY `FK_product_1` (`category_id`),
CONSTRAINT `FK_product_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`category_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
action:
package petstore.action.redirect;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import petstore.dao.CategoryDao;
import petstore.dao.ProductDao;
import petstore.model.Category;
import petstore.model.Product;
public class ItemAddRedirectAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public static final String GLOBAL_CATEGORY = "global_category_list"; // 宠物大类列表
public static final String GLOBAL_DOUBLE_LIST = "global_double_list"; // 宠物类列表
private ProductDao productDao;
private CategoryDao categoryDao;
private List<Category> categorys;
@SuppressWarnings("unchecked")
private Map doubleMap = new HashMap();
public List<Category> getCategorys() {
return categorys;
}
public void setCategorys(List<Category> categorys) {
this.categorys = categorys;
}
@SuppressWarnings("unchecked")
public Map getDoubleMap() {
return doubleMap;
}
@SuppressWarnings("unchecked")
public void setDoubleMap(Map doubleMap) {
this.doubleMap = doubleMap;
}
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
public void setCategoryDao(CategoryDao categoryDao) {
this.categoryDao = categoryDao;
}
@SuppressWarnings("unchecked")
public String execute() throws Exception {
//优化数据库性能
categorys = (List) ActionContext.getContext().getSession().get(
GLOBAL_CATEGORY);
if (categorys == null) {
categorys = categoryDao.findAll();
ActionContext.getContext().getSession().put(GLOBAL_CATEGORY,
categorys);
}
for (int i = 0; i < categorys.size(); i++) {
List<Product> products = productDao
.findProductByCategoryId(categorys.get(i).getCategoryId());
List c = new LinkedList();
for (int j = 0; j < products.size(); j++) {
c.add(products.get(j));
}
doubleMap.put(categorys.get(i), c);
}
return SUCCESS;
}
}
jsp page:
引用
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title><s:text name="additem_page" /></title>
</head>
<body>
<s:actionerror />
<p id="title">
增加宠物
</p>
<s:form action="itemAddAction" enctype="multipart/form-data" name="itemAdd" id="itemAdd">
<s:doubleselect name="category" list="a" doubleName="product"
doubleList="b.get(top)"></s:doubleselect>
<s:submit key="submit"></s:submit><s:reset key="reset"></s:reset>
</s:form>
</body>
</html>
struts.xml:
引用
<action name="directItemAdd" class="petstore.action.redrect.ItemAddRedirectAction">
<result>/jsp/itemAdd.jsp</result>
</action>
值得注意的是jsp这里,必须在form里加上name属性,名字随意
不加上的话会出现错误:
引用
FreeMarker template error!
Expression parameters.formName is undefined on line 98, column 43 in template/simple/doubleselect.ftl.
The problematic instruction:
----------
==> ${parameters.formName} [on line 98, column 41 in template/simple/doubleselect.ftl]
in include "/${parameters.templateDir}/simple/doubleselect.ftl" [on line 25, column 1 in template/xhtml/doubleselect.ftl]
----------
第二,还必须加上id,否则当第一次打加页面时会报错,联级菜单不会正常显示,须刷新后才能正常!