标签:manage 创建 doc row imp https get tty ==
manage-----index.jsp
<em>商品管理</em>
</a>
<dl class="layui-nav-child">
<dd><a href="${pageContext.request.contextPath }/ListProductServlet">商品列表</a></dd>
创建ListProductServlet类:直接调用查询所有商品的方法。
public class ListProductServlet extends HttpServlet { private ProductService productService=new ProductService(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取所有商品 List<Product> list=productService.getProduct(); //将商品放到request域中 request.setAttribute("ProductList",list); //请求转发 request.getRequestDispatcher("/manage/product/list.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
manage---product----list.jsp:展示商品:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <link rel="stylesheet" href="${pageContext.request.contextPath }/manage/assets/css/layui.css"> <link rel="stylesheet" href="${pageContext.request.contextPath }/manage/assets/css/view.css" /> <c:forEach items="${ProductList }" var="pro" varStatus="vs"> <tr> <td>${vs.count }</td><!-- ${vs.index }从1开始 --> <td><img src="${pro.pimage }"></td> <td>${pro.pname }</td> <td>${pro.market_price }</td> <td>${pro.is_hot==1?"是":"否" }</td> <td> <div class="layui-btn-group"> <div class="oper"> <button type="button" class="layui-btn layui-btn-blue layui-btn-sm" onClick="editProduct()"> <i class="layui-icon"></i> 编辑 </button> </div> <div class="del"> <button type="button" class="layui-btn-danger layui-btn layui-btn-red layui-btn-sm"> <i class="layui-icon"></i>删除 </button> </div> </div> </td> </tr> </c:forEach>
<script type="text/javascript">
function addProduct(){
location.href="${pageContext.request.contextPath }/ProductAddUIServlet";
}
function editProduct(){
location.href="edit.jsp";
}
</script>
当一点击新增按钮,跳转到ProductAddUIServlet类,创建ProductAddUIServlet类,只需要查询数据,直接写导层。
先封装实体类:
package com.oracle.domain; public class Category { private String cid; private String cname; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } @Override public String toString() { return "Category [cid=" + cid + ", cname=" + cname + "]"; } }
package com.oracle.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.oracle.domain.Category;
import com.oracle.domain.Product;
import com.oracle.tools.MyDBUtils;
public class CategoryDao {
//查询所有分类
public List<Category> getCategory() throws SQLException{
QueryRunner qr=new QueryRunner(MyDBUtils.getDataSource());
String sql="select * from category";
List<Category> list=qr.query(sql,new BeanListHandler<Category>(Category.class));
return list;
}
}
service层:
package com.oracle.service; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.oracle.dao.CategoryDao; import com.oracle.domain.Category; import com.oracle.tools.MyDBUtils; public class CategoryService { private CategoryDao categoryDao=new CategoryDao(); //查询所有分类 public List<Category> getCategory() { List<Category> list=null; try { list=categoryDao.getCategory(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }
ProductAddUIServlet类:
package com.oracle.web.product; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.oracle.domain.Category; import com.oracle.service.CategoryService; public class ProductAddUIServlet extends HttpServlet { private CategoryService categoryService=new CategoryService(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Category> list=categoryService.getCategory(); request.setAttribute("CategoryList",list); //请求转发 request.getRequestDispatcher("/manage/product/add.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
product下的add.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="${pageContext.request.contextPath }/manage/assets/css/layui.css">
<link rel="stylesheet" href="${pageContext.request.contextPath }/manage/assets/css/view.css" />
<title></title>
<div class="layui-form-item"> <label class="layui-form-label">所属分类</label> <div class="layui-input-inline"> <select name="cid" lay-verify="required"> <option value="">请选择</option> <c:forEach items="${CategoryList }" var="c"> <option value="${c.cid }">${c.cname }</option> </c:forEach> </select> </div>
修改返回按钮返回上一级:
<script src="${pageContext.request.contextPath }/manage/assets/layui.all.js"></script> <script> var form = layui.form, layer = layui.layer; function go(){ location.href="${pageContext.request.contextPath }/ListProductServlet"; } </script>
标签:manage 创建 doc row imp https get tty ==
原文地址:https://www.cnblogs.com/maxuefeng/p/14016018.html