根据前台form传回来的数值进行判断,即类的id是否是空,要是空的话,就直接利用service的方法添加为根节点,要是id不是空就进行更新。要是父类的id不是空的话,就直接添加调用service的添加子节点的方法。
代码如下:
/**添加类别根节点./category/add_category.do*/
@RequestMapping(value="/add_category",method=RequestMethod.POST)
public void addCategory(HttpServletRequest request,PrintWriter writer) {
String text = request.getParameter("text");
String description = request.getParameter("description");
String parent = request.getParameter("parent");
String leaf = request.getParameter("leaf");
if(parent.equals("root")){
//添加根节点
if(categoryService.addTopCategory(text, description)){
writer.write("{success:true, msg:'根节点新增成功!'}");
}else{
writer.write("{success:true, msg:'根节点新增失败!'}");
}
}else{
//添加子节点,把id=parent的节点变为非叶子,让新节点的parent=父节点的id
Category category = new Category();
category.setText(text);
category.setDescription(description);
if(parent.equals("root")){
parent = "0";
}
if(categoryService.addChildCategory(Integer.parseInt(parent), category)){
writer.write("{success:true, msg:'子节点新增成功!'}");
}else{
writer.write("{success:true, msg:'子节点新增失败!'}");
}
}
}
/**删除类别(根据id和pid)./category/delete_category.do*/
@RequestMapping(value="/delete_category",method=RequestMethod.POST)
public void deleteCategoryByIdAdnPid(HttpServletRequest request,PrintWriter writer) {
String id = request.getParameter("id");
String pid = request.getParameter("pid");
if(pid.equals("root")){
pid = "0";
}
try {
if(categoryService.deleteById(Integer.parseInt(id), Integer.parseInt(pid))){
writer.write("{success:true, msg:'类别删除成功!'}");
}else{
writer.write("{success:true, msg:'类别删除失败!'}");
}
}catch (QshException e) {
// TODO Auto-generated catch block
writer.write("{success:true, msg:'"+e+"'}");
}
return;
}
/*修改类别信息(根据id)./category/update_category.do*/
@RequestMapping(value="/update_category",method=RequestMethod.POST)
public void updateCategoryById(HttpServletRequest request,PrintWriter writer) {
String id = request.getParameter("id");
String text = request.getParameter("text");
String description = request.getParameter("description");
String parent = request.getParameter("parent");
String leaf = request.getParameter("leaf");
Category category = new Category();
category.setId(Integer.parseInt(id));
category.setText(text);
category.setDescription(description);
if(categoryService.update(category)){
writer.write("{success:true, msg:'类别修改成功!'}");
}else{
writer.write("{success:true, msg:'类别修改失败!'}");
}
}
/**
* 显示到combobox的列表的store
* @param request
* @param writer
*/
@RequestMapping(value="/combo_category",method=RequestMethod.GET)
public void listCategoryToCombobox(HttpServletRequest request, PrintWriter writer) {
List<Category> categories = categoryService.findAll();
int len = categories.size();
String preStr = "{totalCount:" + len + ",rows:[";
String cenStr = "";
int i=0;
String douhao = ",";
for(Category c : categories) {
i++;
cenStr += "{categoryId:'"+c.getId()+"', " +
"text:'"+c.getText()+"'" +
"}";
if(i < len){
cenStr += douhao;
}
}
String endStr = "]}";
String resultStr = preStr + cenStr + endStr;
writer.write(resultStr);
}