一、下拉框
1.将下拉框需要显示的值写入实体类
package com.mjx.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方法进行查询
public List<Category> list(Category category, PageBean pageBean) throws Exception {
String sql = "select * from t_easyui_category";
long id = category.getId();
if (id != 0) {
sql += "and id=" + id;
}
return super.executeQuery(sql, Category.class, pageBean);
}
3.action子控制器内调用
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.配置mvc文件
<action path="/category" type="com.mjx.web.CategoryAction">
</action>
5.在表单中添加下拉框组件
<input id="cid" name="cid" value="" label="类别" >
$(function () {
$('#cid').combobox({
url:'${pageContext.request.contextPath}/category.action?methodName=list',
valueField:'id',
textField:'name'
});
});
二、添加
1.添加表单
<form id="ff" action="" 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:'作者:',required:true">
</div>
<div style="margin-bottom:20px">
<input class="easyui-textbox" name="price" style="width:100%"
data-options="label:'价格:',required:true">
</div>
<div style="margin-bottom:20px">
<input class="easyui-textbox" name="publishing" style="width:100%"
data-options="label:'出版社:',required:true">
</div>
<div style="margin-bottom:20px">
<input class="easyui-textbox" name="description" style="width:100%;height:60px"
data-options="label:'简介:',required:true">
</div>
<%--默认未上架--%>
<input type="hidden" name="state" value="1">
<%--默认起始销量为0--%>
<input type="hidden" name="sales" value="0">
</form>
2.给表单添加点击事件
<div style="text-align:center;padding:5px 0">
<a href="javascript:void(0)" class="easyui-linkbutton" οnclick="submitForm()" style="width:80px">Submit</a>
<a href="javascript:void(0)" class="easyui-linkbutton" οnclick="clearForm()" style="width:80px">Clear</a>
</div>
3.提交表单和清空
//提交编辑信息的表单
function submitForm() {
$('#ff').form('submit', {
success : function(param) {
$('#dd').dialog('close');
$('#dg').datagrid('reload');
$('#ff').form('clear');
}
});
}
function clearForm() {
$('#ff').form('clear');
}
三、上架和下架
1.书籍实体类
package com.mjx.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 + "]";
}
}
2.书籍的dao方法增改查
public List<Book> list(Book book, PageBean pageBean) throws Exception {
String sql = "select * from t_eastui_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 edit(Book t) throws Exception {
// TODO Auto-generated method stub
super.executeUpdate("update t_easyui_book set name=?,pinyin=?,cid=?,image=?,state=?,sales=? where id=?", t,
new String[] { "name", "pinyin", "cid", "image", "state", "sales", "id" });
}
public void add(Book t) throws Exception {
t.setPinyin(PinYinUtil.getAllPingYin(t.getName()));
super.executeUpdate(
"insert into t_easyui_book(name,pinyin,cid,author,price,image,publishing,description,state,deployTime,sales) values(?,?,?,?,?,?,?,?,?,?,?)",
t, new String[] { "name", "pinyin", "cid", "author", "price", "image", "publishing", "description",
"state", "deployTime", "sales" });
}
3.书籍的action
package com.mjx.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mjx.dao.BookDao;
import com.mjx.entity.Book;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.PageBean;
import com.zking.util.R;
import com.zking.util.ResponseUtil;
public class BookAction extends ActionSupport implements ModelDriver<Book> {
private Book book = new Book();
private BookDao bookDao = new BookDao();
@Override
public Book getModel() {
// TODO Auto-generated method stub
return book;
}
public void list(HttpServletRequest req, HttpServletResponse resp) {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
try {
List<Book> list = bookDao.list(book, pageBean);
ResponseUtil.writeJson(resp, new R().data("total", pageBean.getTotal()).data("rows", list));
} catch (Exception e) {
e.printStackTrace();
}
}
public void add(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.add(book);
ResponseUtil.writeJson(resp, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 如果上架,书籍的状态改为2
* 如果下架,书籍的状态改为3
* @param req
* @param resp
*/
public void upDown(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.edit(book);
ResponseUtil.writeJson(resp, 1);
} catch (Exception e) {
e.printStackTrace();
try {
ResponseUtil.writeJson(resp, 1);
} catch (Exception e1) {
e.printStackTrace();
}
}
}
public void editStatus(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.editStatus(book);
ResponseUtil.writeJson(resp, 1);
} catch (Exception e) {
e.printStackTrace();
try {
ResponseUtil.writeJson(resp, 1);
} catch (Exception e1) {
e.printStackTrace();
}
}
}
}
4.配置mvc文件
<action path="/book" type="com.mjx.web.BookAction">
</action>
5.上架的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) {
}
})
}
}
});
}
6.下架的js代码
function xiajia() {
$.messager.confirm('确认','您确认想要下架此书籍吗?',function(r){
if (r){
var row = $('#dg').datagrid('getSelected');
if (row){
$.ajax({
url:'${pageContext.request.contextPath}/book.action?methodName=shangjia&state=3&id=' + row.id,
success:function (data) {
}
})
}
}
});
}
注意:书籍状态(1 未上架 2 已上架 3 已下架 默认值1)
效果:
新增了一本书
上架
下架