前言:基于mvc&EasyUI项目01进行管理员功能实现
1.新增
实体类entity
Book(书籍)
package com.wyy.entity;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class EasyuiBook {
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",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 "EasyuiBook [id=" + id + ", name=" + name + ", pinyin=" + pinyin + ", cid=" + cid + ", author="
+ author + ", price=" + price + ", image=" + image + ", publishing=" + publishing + ", description="
+ description + ", state=" + state + ", deployTime=" + deployTime + ", sales=" + sales + "]";
}
public EasyuiBook() {
// TODO Auto-generated constructor stub
}
public EasyuiBook(long id, String name, String pinyin, long cid, String author, float price, String image,
String publishing, String description, int state, Date deployTime, int sales) {
super();
this.id = id;
this.name = name;
this.pinyin = pinyin;
this.cid = cid;
this.author = author;
this.price = price;
this.image = image;
this.publishing = publishing;
this.description = description;
this.state = state;
this.deployTime = deployTime;
this.sales = sales;
}
}
Category(类别)
package com.wyy.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 + "]";
}
public Category(long id, String name) {
super();
this.id = id;
this.name = name;
}
public Category() {
// TODO Auto-generated constructor stub
}
}
增加页面
在增加页面加载时,需要访问后台获取所有类别
<%@ 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}/EasyuiBook.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="类别" >
</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>
<div style="text-align:center;padding:5px 0">
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">Submit</a>
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">Clear</a>
</div>
</div>
<script>
$(function () {
/* 查询类别 */
$('#cid').combobox({
url:'${pageContext.request.contextPath}/Category.action?methodName=Categorylist',
valueField:'id',
textField:'name'
});
});
function submitForm() {
$('#ff').form('submit',{
success:function (param) {
$('#ff').form('clear');
}
});
}
function clearForm() {
$('#ff').form('clear');
}
</script>
</body>
</html>
配置mvc
<action path="/EasyuiBook" type="com.wyy.Action.EasyuibookAction">
</action>
<action path="/Category" type="com.wyy.Action.CategoryAction">
</action>
增加页面效果
2上架/下架
无论是上架还是下架,只是改变书籍状态
已上架页面
<%@ 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>
<table id="dg" style="style=" width:400px;height:200px;
"></table>
<script>
/* 下架书籍 */
function xiajia() {
$.messager.confirm('确认','您确认想要下架此书籍吗?',function(r){
if (r){
var row = $('#dg').datagrid('getSelected');
if (row){
$.ajax({
url:'${pageContext.request.contextPath}/EasyuiBook.action?methodName=editt&state=3&id='+ row.id,
success:function (data) {
}
})
}
}
});
}
/* 显示所有上架书籍 */
$(function () {
$('#dg').datagrid({
url: '${pageContext.request.contextPath}/EasyuiBook.action?methodName=list&&state=2',
fit: true,
fitColumns: true,
pagination: true,
singleSelect: true,
columns: [[
// {field:'id',title:'id',width:100},
{field: 'id', title: '书籍名称', hidden: true},
{field: 'name', title: '书籍名称', width: 50},
{field: 'pinyin', title: '拼音', width: 50},
{
field: 'cid', title: '书籍类别', width: 50, formatter: function (value, row, index) {
if (row.cid == 1) {
return "文艺";
} else if (row.cid == 2) {
return "小说";
} else if (row.cid == 3) {
return "青春";
} else {
return value;
}
}
},
{field: 'author', title: '作者', width: 50},
// {field:'price',title:'价格',width:100},
{
field: 'image', title: '图片路径', width: 100, formatter: function (value, row, index) {
return '<img style="width:80px;height: 60px;" src="' + row.image + '"></img>';
}
},
{field: 'publishing', title: '出版社', width: 50},
{field: 'sales', title: '销量', width: 50},
{field: 'deployTime', title: '上架时间', width: 50, align: 'right'},
{
field: 'xxxx', title: '操作', width: 100, formatter: function (value, row, index) {
return '<a href="#" onclick="xiajia();">下架</a>';
}
}
]]
});
})
</script>
</body>
</html>
BookDao
package com.wyy.dao;
import java.util.List;
import com.wyy.entity.EasyuiBook;
import com.wyy.entity.Permission;
import com.zking.util.BaseDao;
import com.zking.util.PageBean;
import com.zking.util.StringUtils;
public class EasyuiBookDao extends BaseDao<EasyuiBook> {
/**
* 查询书籍 当前台传值再根据条件查询
* @param b
* @param pageBean
* @return
* @throws Exception
*/
public List<EasyuiBook> list(EasyuiBook b, PageBean pageBean) throws Exception {
String sql = "select * from t_easyui_book where 1=1 ";
int state = b.getState();
String name = b.getName();
if (StringUtils.isNotBlank(name)) {
sql += " and name like '%" + name + "%'";
}
if (state != 0) {
sql += " and state=" + state;
}
return super.executeQuery(sql, EasyuiBook.class, pageBean);
}
/**
* 上架/下架书籍
* @param b
* @throws Exception
*/
public void edit(EasyuiBook b) throws Exception {
String sql = "UPDATE t_easyui_book set state=? where id=?";
String[] tre = new String[] { "state", "id" };
super.executeUpdate(sql, b, tre);
}
/**
* 增加数据的方法
* @param b
* @throws Exception
*/
public void add(EasyuiBook b) throws Exception {
String sql = "insert into t_easyui_book(name,pinyin,cid,author,price,image,publishing,description,state,deployTime,sales) values(?,?,?,?,?,?,?,?,?,?,?)";
String[] stt = new String[] { "name", "pinyin", "cid", "author", "price", "image", "publishing", "description",
"state", "deployTime", "sales" };
super.executeUpdate(sql, b, stt);
}
/**
* 修改
* @param b
* @throws Exception
*/
public void upd(EasyuiBook b) throws Exception {
String sql = "update t_easyui_book set name=?,pinyin=?,cid=?,author=?,price=?,image=?,publishing=?,description=?,state=?,deployTime=?,sales=? where id=?";
String[] stt = new String[] { "name", "pinyin", "cid", "author", "price", "image", "publishing", "description",
"state", "deployTime", "sales", "id" };
super.executeUpdate(sql, b, stt);
}
}
BookAction
package com.wyy.Action;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wyy.dao.EasyuiBookDao;
import com.wyy.entity.EasyuiBook;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.PageBean;
import com.zking.util.PinYinUtil;
import com.zking.util.ResponseUtil;
public class EasyuibookAction extends ActionSupport implements ModelDriver<EasyuiBook> {
private EasyuiBook b=new EasyuiBook();
private EasyuiBookDao bdao=new EasyuiBookDao();
@Override
public EasyuiBook getModel() {
// TODO Auto-generated method stub
return b;
}
/**
* 所有类别查询书籍方法
* @param req
* @param resp
* @return
* @throws Exception
*/
public String list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
PageBean p=new PageBean();
p.setRequest(req);
List<EasyuiBook> list = bdao.list(b, p);
Map<String , Object> map=new HashMap<String, Object>();
map.put("total", p.getTotal());
map.put("rows", list);
ResponseUtil.writeJSON(resp, map);
return null;
}
/**
* 上架/下架
* @param req
* @param resp
* @return
* @throws Exception
*/
public String editt(HttpServletRequest req, HttpServletResponse resp) throws Exception {
bdao.edit(b);
return null;
}
/**
* 增加
* @param req
* @param resp
* @return
* @throws Exception
*/
public String add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
String allPingYin = PinYinUtil.getAllPingYin(b.getName());
b.setPinyin(allPingYin);
b.setDeployTime(new Date());
bdao.add(b);
return null;
}
/**
* 修改
* @param req
* @param resp
* @return
* @throws Exception
*/
public String upd(HttpServletRequest req, HttpServletResponse resp) throws Exception {
String allPingYin = PinYinUtil.getAllPingYin(b.getName());
b.setPinyin(allPingYin);
b.setDeployTime(new Date());
bdao.upd(b);
return null;
}
}
修改
当点击修改时,需要将所有类别加载,打开窗口,当前选中行进行数据回显
function edit() {
$('#cid').combobox({
url:'${pageContext.request.contextPath}/Category.action?methodName=Categorylist',
valueField:'id',
textField:'name'
});
var row = $('#dg').datagrid('getSelected');
if (row) {
$('#ff').form('load', row);
$('#dd').dialog('open');
}
}
表单提交
function submitForm() {
$('#ff').form('submit',{
url:'${pageContext.request.contextPath}/EasyuiBook.action?methodName=upd',
success: function (param) {
$('#dd').dialog('close');
$('#dg').datagrid('reload');
$('#ff').form('clear');
}
});
}
Category加载
package com.wyy.Action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wyy.dao.CategoryDao;
import com.wyy.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 c=new Category();
private CategoryDao cdao=new CategoryDao();
public Category getModel() {
// TODO Auto-generated method stub
return c;
}
public String Categorylist(HttpServletRequest req, HttpServletResponse resp) throws Exception {
List<Category> categoryList = cdao.categoryList(c, null);
ResponseUtil.writeJSON(resp, categoryList);
return null;
}
}