一.书籍类别下拉框
1.下拉框的实体类
package com.zking.entity;
public class Category {
private long id;
private String name;
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;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
}
2.dao方法
package com.zking.dao;
import java.util.Date;
import java.util.List;
import com.zking.entity.Book;
import com.zking.util.BaseDao;
import com.zking.util.PageBean;
import com.zking.util.PinYinUtil;
import com.zking.util.StringUtils;
public class BookDao extends BaseDao<Book> {
/**
* 查询
* @param book
* @param pageBean
* @return
* @throws Exception
*/
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);
}
/**
* 修改
* @param book
* @param attrs
* @throws Exception
*/
public void edit( Book book) throws Exception {
String sql="update t_easyui_book set name=?,pinyin=?,cid=?,image=?,state=?,sales=? where id=?";
super.executeUpdate(sql, book, new String[] {"name","pinyin","cid","image","state","sales","id"});
}
/**
* 增加
* @param book
* @param attrs
* @throws Exception
*/
public void add( Book book) throws Exception {
String sql="insert into t_easyui_book(name,pinyin,cid,author,price,image,publishing,description,state,deployTime,sales) values(?,?,?,?,?,?,?,?,?,?,?)";
//增加拼音
book.setPinyin(PinYinUtil.getAllPingYin(book.getName()));
//增加时间
book.setDeployTime(new Date());
super.executeUpdate(sql, book, new String[] {"name","pinyin","cid","author","price","image","publishing","description","state","deployTime","sales"});
}
//上架
public void editStatus(Book book) throws Exception {
String sql="update t_easyui_book set state=? where id=?";
super.executeUpdate(sql, book, new String[] {"state","id"});
}
}
3.CategoryAction
package com.zking.web;
import com.zking.dao.CategoryDao;
import com.zking.entity.Category;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.ResponseUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
public class CategoryAction extends ActionSupport implements ModelDriver<Category> {
private Category category = new Category();
private CategoryDao categoryDao = new CategoryDao();
/**
* 加载书籍类别下拉框
* @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;
}
@Override
public Category getModel() {
return category;
}
}
4.配置mvc.xml文件
<action type="com.zking.web.CategoryAction" path="/Category">
</action>
5.新增下拉框組
$(function () {
$('#cid').combobox({
url:'${pageContext.request.contextPath}/category.action?methodName=combobox',
valueField:'id',
textField:'name'
});
});
6.运行效果
二、新增书籍
1.增加书籍的jsp界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书籍新增</title>
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/static/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/easyui/themes/icon.css">
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath}/static/js/easyui/jquery.easyui.min.js"></script>
<script src="${pageContext.request.contextPath}/static/js/main.js"></script>
</head>
<body>
<div style="margin:20px 0;"></div>
<div class="easyui-panel" title="已下架书籍" style="width:100%;padding:30px 60px;">
<form id="ff" action="${pageContext.request.contextPath}/book.action?methodName=add" method="post">
<div style="margin-bottom:20px">
<input class="easyui-textbox" name="name" style="width:100%" data-options="label:'书名:',required:true">
</div>
<div style="margin-bottom:20px">
<input id="cid" name="cid" value="" label="类别" >
<!-- <select class="easyui-combobox" name="cid" label="类别" style="width:100%">
<option value="1">文艺</option>
<option value="2">小说</option>
<option value="3">青春</option>
</select> -->
</div>
<div style="margin-bottom:20px">
<input class="easyui-textbox" name="author" style="width:100%" data-options="label:'作者:&