课程内容:
1、新增书籍
2、上架书籍
3、下架书籍
一、新增页面书籍类别下拉框加载
1、根据下拉框类型写实体类
2、查询所有类型的方法(CategoryDao)
package com.zxy.dao;
import java.util.List;
import com.zxy.entity.Category;
import com.zking.util.BaseDao;
import com.zking.util.PageBean;
public class CategoryDao extends BaseDao<Category>{
public List<Category> list(Category category, PageBean pageBean) throws Exception {
String sql="select * from t_easyui_category where 1=1";
long id = category.getId();
if(id!=0) {
sql+=" and id ="+id;
}
return super.executeQuery(sql, Category.class, pageBean);
}
// select b.*,c.name from t_easyui_book b, t_easyui_category c where b.cid=c.id
}
3、子控制器(CategoryAction)
package com.zxy.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zxy.dao.CategoryDao;
import com.zxy.entity.Category;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.ResponseUtil;
public class CategoryAction extends ActionSupport implements ModelDriver<Category>{
private Category category=new Category();
private CategoryDao categoryDao=new CategoryDao();
public Category getModel() {
return category;
}
/**
* 加载数据类别下拉框
* @param req
* @param resp
* @return
*/
public String combobox(HttpServletRequest req, HttpServletResponse resp) {
try {
List<Category> list = categoryDao.list(category, null);
ResponseUtil.writeJson(resp, list);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
4、在点击菜单栏需弹出一个增加的窗口
$(function(){
$("#bookMenus").tree({
url:$("#ctx").val()+"/permission.action?methodName=tree",
// 给菜单栏一个点击
onClick: function(node){
// 判断面板是否存在
var exists=$("#bookTabs").tabs('exists',node.text);
if(exists){
$("#bookTabs").tabs('select',node.text);
}else{
$('#bookTabs').tabs('add',{
title:node.text,
content:'<iframe width="100%" height="100%" src="'+$("#ctx").val()+node.attributes.self.url+'" />',
closable:true
});
}
}
});
})
5、通过数据库内的类型传到增加窗口的下拉框,使其灵活性
借助API中的ComboBox(下拉列表框)
<input id="cid" name="cid" value="" label="类别" >
js文件:
$(function () {
$('#cid').combobox({
url:'${pageContext.request.contextPath}/category.action?methodName=combobox', valueField:'id',
textField:'text'
});
});
二、书籍上架下架新增功能
1、完成新增功能
①、新增页面所需的属性实体类(Book)
package com.zxy.entity;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class Book {
private long id;
private String name;
private String pinyin;
private long cid;
private String author;
private float price;
private String image;
private String publishing;
private String description;
private int state;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date deployTime;
private int sales;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPinyin() {
return pinyin;
}
public void setPinyin(String pinyin) {
this.pinyin = pinyin;
}
public long getCid() {
return cid;
}
public void setCid(long cid) {
this.cid = cid;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getPublishing() {
return publishing;
}
public void setPublishing(String publishing) {
this.publishing = publishing;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public Date getDeployTime() {
return deployTime;
}
public void setDeployTime(Date deployTime) {
this.deployTime = deployTime;
}
public int getSales() {
return sales;
}
public void setSales(int sales) {
this.sales = sales;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", pinyin=" + pinyin + ", cid=" + cid + ", author=" + author
+ ", price=" + price + ", image=" + image + ", publishing=" + publishing + ", description="
+ description + ", state=" + state + ", deployTime=" + deployTime + ", sales=" + sales + "]";
}
}
②、写方法BookDao
public void add( Book t) throws Exception {
// 转化拼音
t.setPinyin(PinYinUtil.getAllPingYin(t.getName()));
t.setDeployTime(new Date());
String sql="insert into t_easyui_book(name,pinyin,cid,author,price,image,publishing,description,state,deployTime,sales) values(?,?,?,?,?,?,?,?,?,?,?)";
super.executeUpdate(sql,
t, new String[] {"name","pinyin","cid","author","price","image","publishing","description","state","deployTime","sales"});
}
③、写子控制器(BookAction)
public void add(HttpServletRequest req, HttpServletResponse resp) {
try {
bd.add(book);
ResponseUtil.writeJson(resp, 1);
} catch (Exception e) {
e.printStackTrace();
try {
ResponseUtil.writeJson(resp, 0);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
④、获取数据,提交表单
/* 通过form控件提交 */
function submitForm() {
$('#ff').form('submit', {
url:'${pageContext.request.contextPath}/book.action?methodName=add',
success:function(data){
if(data==1){
$('#ff').form('clear');
}
}
});
}
/* 刷新 */
function clearForm() {
$('#ff').form('clear');
}
呈现结果:
增加的数据就到了未上架里:
2、上架与下架
①、上架方法,也就是修改
// 查询
public List<Book> list(Book book, PageBean pageBean) throws Exception {
String sql="select * from t_easyui_book where 1=1";
String name=book.getName();
int state = book.getState();
if(StringUtils.isNotBlank(name)) {
sql+=" and name like '%"+name+"%'";
}
if(state!=0) {
sql+=" and state ="+state;
}
return super.executeQuery(sql, Book.class, pageBean);
}
// 上下架
public void editStatus(Book book) throws Exception {
super.executeUpdate("update t_easyui_book set state=? where id=?",
book,new String[] {"state","id"});
}
②、子控制器
public void list(HttpServletRequest req, HttpServletResponse resp) {
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
try {
List<Book> list = bd.list(book, pageBean);
ResponseUtil.writeJson(resp, new R().data("total", pageBean.getTotal())
.data("rows", list));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 如果上架,书籍的状态改为2
// 如果下架,书籍的状态改为3
public void editStatus(HttpServletRequest req, HttpServletResponse resp) {
try {
bd.editStatus(book);
ResponseUtil.writeJson(resp, 1);
} catch (Exception e) {
e.printStackTrace();
try {
ResponseUtil.writeJson(resp, 0);
} catch (Exception e1) {
e1.printStackTrace();
}
}
调用方法,写js文件:
function shangjia() {
$.messager.confirm('确认','您确认想要上架此书籍吗?',function(r){
if (r){
var row = $('#dg').datagrid('getSelected');
if (row){
$.ajax({
url:'${pageContext.request.contextPath}/book.action?methodName=editStatus&state=2&id=' + row.id,
success:function (data) {
}
})
}
}
});
}
根据状态的不同,改变上下架
function xiajia() {
$.messager.confirm('确认','您确认想要下架此书籍吗?',function(r){
if (r){
var row = $('#dg').datagrid('getSelected');
if (row){
$.ajax({
url:'${pageContext.request.contextPath}/book.action?methodName=editStatus&state=3&id=' + row.id,
success:function (data) {
$('#dg').datagrid('reload'); // 重新载入当前页面数据
}
})
}
}
});
}
展示结果:
已上架:
结束啦~~~~~~~~~~~~~