自定mvc之新增,下架以及上架

本文详细介绍了如何使用自定义的MVC框架进行书籍管理,包括书籍类别的下拉框实现,新增书籍的步骤,书籍上架和下架的功能。涉及到的步骤包括实体类、DAO方法、Action类以及XML配置文件的编写,最后展示了运行效果。
摘要由CSDN通过智能技术生成

一.书籍类别下拉框

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:'作者:&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值