代码如下,jsp还是无法获得categoryList数据
package com.imooc.web.action;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.imooc.domain.Category;
import com.imooc.service.CategoryService;
import com.imooc.service.impl.CategoryServiceImpl;
@WebServlet("/CategoryServlet")
public class CategoryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String methodName = request.getParameter("method");
if("findAll".equals(methodName)) {
findAll(request,response);
}else if("saveUI".equals(methodName)) {
saveUI(request,response);
}else if("save".equals(methodName)) {
save(request,response);
}else if("edit".equals(methodName)) {
edit(request,response);
}else if("update".equals(methodName)) {
update(request,response);
}else if("delete".equals(methodName)) {
delete(request,response);
}
}
/**
* 删除分类
* @param request
* @param response
* @throws IOException
*/
private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
Integer cid = Integer.parseInt(request.getParameter("cid"));
CategoryService categoryService = new CategoryServiceImpl();
categoryService.delete(cid);
response.sendRedirect(request.getContextPath()+"/CategoryServlet?method=findAll");
}
/**
* 后台分类修改分类
* @param request
* @param response
* @throws IOException
*/
private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
//接收数据
Integer cid = Integer.parseInt(request.getParameter("cid"));
String cname = request.getParameter("cname");
String cdesc = request.getParameter("cdesc");
//封装数据
Category category = new Category();
category.setCid(cid);
category.setCname(cname);
category.setCdesc(cdesc);
//调用业务层处理数据
CategoryService categoryService = new CategoryServiceImpl();
categoryService.update(category);
//页面跳转
response.sendRedirect(request.getContextPath()+"/CategoryServlet?method=findAll");
}
/**
* 后台管理编辑分类
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收数据
Integer cid = Integer.parseInt(request.getParameter("cid"));
//调用业务层处理数据
CategoryService categoryService = new CategoryServiceImpl();
Category category = categoryService.findOne(cid);
request.setAttribute("category", category);
request.getRequestDispatcher("/admin/category_update.jsp").forward(request, response);
}
/**
* 后台分类管理保存分类
* @param request
* @param response
* @throws IOException
*/
private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
//接收数据
String cname = request.getParameter("cname");
String cdesc = request.getParameter("cdesc");
//封装数据
Category category = new Category();
category.setCname(cname);
category.setCdesc(cdesc);
//处理数据
CategoryService categoryService = new CategoryServiceImpl();
categoryService.save(category);
//页面跳转
response.sendRedirect(request.getContextPath()+"/CategoryServlet?method=findAll");
}
/**
* 后台分类跳转到添加页面
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void saveUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//单击后进行页面跳转
request.getRequestDispatcher("/admin/category_add.jsp").forward(request, response);
}
/**
* 后台查询所有分类
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理数据
CategoryService categoryService = new CategoryServiceImpl();
List<Category> categoryList = categoryService.findAll();
//保存数据
request.setAttribute("categoryList", categoryList);
System.out.println(categoryList);
//页面跳转
request.getRequestDispatcher("/admin/category_list.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.imooc.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.imooc.dao.CategoryDao;
import com.imooc.domain.Category;
import com.imooc.utils.JDBCUtils;
public class CategoryDaoImpl implements CategoryDao {
@Override
public List<Category> findAll() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Category> list = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select * from category";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
list = new ArrayList<Category>();
while(rs.next()) {
Category category = new Category();
category.setCid(rs.getInt("cid"));
category.setCname(rs.getString("cname"));
category.setCdesc(rs.getString("cdesc"));
list.add(category);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.release(rs, pstmt, conn);
}
return list;
}
@Override
public void save(Category category) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = JDBCUtils.getConnection();
String sql = "insert into category values(null,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, category.getCname());
pstmt.setString(2, category.getCdesc());
pstmt.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.release(pstmt, conn);
}
}
@Override
public Category findOne(Integer cid) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select * from category where cid=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, cid);
rs = pstmt.executeQuery();
if(rs.next()) {
Category category = new Category();
category.setCid(rs.getInt("cid"));
category.setCname(rs.getString("cname"));
category.setCdesc(rs.getString("cdesc"));
return category;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public void update(Category category) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = JDBCUtils.getConnection();
String sql = "update category set cname=?,cdesc=? where cid=?";
pstmt =conn.prepareStatement(sql);
pstmt.setString(1, category.getCname());
pstmt.setString(2, category.getCdesc());
pstmt.setInt(3, category.getCid());
pstmt.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.release(pstmt, conn);
}
}
@Override
public void delete(Integer cid) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = JDBCUtils.getConnection();
String sql = "delete from category where cid=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, cid);
pstmt.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.release(pstmt, conn);
}
}
}
package com.imooc.web.action;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.imooc.domain.Product;
import com.imooc.service.ProductService;
import com.imooc.service.impl.ProductServiceImpl;
/**
* Servlet implementation class ProductServlet
*/
@WebServlet("/ProductServlet")
public class ProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String methodName = request.getParameter("method");
if("findAll".equals(methodName)) {
findAll(request,response);
}else if("saveUI".equals(methodName)) {
saveUI(request,response);
}
}
/**
* 点击添加跳转添加商品页面
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void saveUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/admin/product_add.jsp").forward(request, response);
}
/**
* 查询商品列表
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//不需要接收参数,只是查询所有分类
//调用业务层处理
ProductService productService = new ProductServiceImpl();
List<Product> list = productService.findAll();
//存入并进行请求转发进行页面跳转;
request.setAttribute("list", list);
request.getRequestDispatcher("/admin/product_list.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.imooc.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.imooc.dao.ProductDao;
import com.imooc.domain.Product;
import com.imooc.utils.JDBCUtils;
public class ProductDaoImpl implements ProductDao {
@Override
public List<Product> findAll() {
Connection conn = null ;
PreparedStatement pstmt = null;
ResultSet rs = null ;
List<Product> list = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select * from product p,category c where p.cid=c.cid";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
list = new ArrayList<Product>();
while(rs.next()) {
Product product = new Product();
product.setPid(rs.getInt("pid"));
product.setPname(rs.getString("pname"));
product.setAuthor(rs.getString("author"));
product.setPrice(rs.getDouble("price"));
product.setDescription(rs.getString("description"));
product.setFilename(rs.getString("filename"));
product.setPath(rs.getString("path"));
product.getCategory().setCid(rs.getInt("cid"));
product.getCategory().setCname(rs.getString("cname"));
product.getCategory().setCdesc(rs.getString("cdesc"));
list.add(product);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.release(rs, pstmt, conn);
}
return list;
}
}
<%@ 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>
<!-- Mirrored from admindesigns.com/demos/absolute/1.1/admin_forms-validation.html by HTTrack Website Copier/3.x [XR&CO'2014], Thu, 06 Aug 2015 02:56:15 GMT -->
<head>
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">
<title>油画商城--添加商品</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/skin/default_skin/css/theme.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/css/admin-forms.css">
<link rel="shortcut icon" href="${pageContext.request.contextPath }/assets/img/favicon.ico">
</head>
<body class="admin-validation-page" data-spy="scroll" data-target="#nav-spy" data-offset="200">
<div id="main">
<%@ include file="header.jsp" %>
<%@ include file="left.jsp" %>
<section id="content_wrapper">
<section id="content" class="table-layout animated fadeIn">
<div class="tray tray-center">
<div class="content-header">
<h2> 添加商品 </h2>
<p class="lead"></p>
</div>
<div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
<div class="panel heading-border">
<form id="admin-form" name="addForm" action="${pageContext.request.contextPath }/ProductServlet?method=" method="post">
<div class="panel-body bg-light">
<div class="section-divider mt20 mb40">
<span> 基本信息 </span>
</div>
<div class="section row">
<div class="col-md-2"></div>
<div class="col-md-1">
<label for="sn" class="field prepend-icon">
<label for="sn" class="field-icon">
名称
</label>
</label>
</div>
<div class="col-md-6">
<label for="sn" class="field">
<input name="pname" class="gui-input" placeholder="名称" type="text" value="imooc"/>
</label>
</div>
</div>
<div class="section row">
<div class="col-md-2"></div>
<div class="col-md-1">
<label for="sn" class="field prepend-icon">
<label for="sn" class="field-icon">
价格
</label>
</label>
</div>
<div class="col-md-6">
<label for="sn" class="field">
<input name="price" class="gui-input" placeholder="价格" type="text" value="1000"/>
</label>
</div>
</div>
<div class="section row">
<div class="col-md-2"></div>
<div class="col-md-1">
<label for="sn" class="field prepend-icon">
<label for="sn" class="field-icon">
作者
</label>
</label>
</div>
<div class="col-md-6">
<label for="sn" class="field">
<input name="author" class="gui-input" placeholder="价格" type="text" value="1000"/>
</label>
</div>
</div>
<div class="section row">
<div class="col-md-2"></div>
<div class="col-md-1">
<label for="sn" class="field prepend-icon">
<label for="sn" class="field-icon">
分类
</label>
</label>
</div>
<div class="col-md-6">
<label for="sn" class="field select">
<select name="cid" class="gui-input" placeholder="分类...">
<c:forEach var="c" items="${applicationScope.categoryList}">
<option value="${c.cid}">${c.cname}</option>
</c:forEach>
</select>
<i class="arrow double"></i>
</label>
</div>
</div>
<div class="section row">
<div class="col-md-2"></div>
<div class="col-md-1">
<label for="sn" class="field prepend-icon">
<label for="sn" class="field-icon">
图片
</label>
</label>
</div>
<div class="col-md-6">
<label for="name" class="field">
<input id="filename" name="name" class="gui-input" type="file" value="上传图片"/>
</label>
</div>
</div>
<div class="section row">
<div class="col-md-2"></div>
<div class="col-md-1">
<label for="sn" class="field prepend-icon">
<label for="sn" class="field-icon">
描述
</label>
</label>
</div>
<div class="col-md-6">
<label for="address" class="field">
<input name="description" class="gui-input" placeholder="描述" type="text" value=""/>
</label>
</div>
</div>
<div class="panel-footer text-center">
<button type="submit" class="button"> 保存 </button>
<button type="button" class="button" onclick="javascript:window.history.go(-1);"> 返回 </button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
</section>
</div>
<style>
/* demo page styles */
body { min-height: 2300px; }
.content-header b,
.admin-form .panel.heading-border:before,
.admin-form .panel .heading-border:before {
transition: all 0.7s ease;
}
/* responsive demo styles */
@media (max-width: 800px) {
.admin-form .panel-body { padding: 18px 12px; }
}
</style>
<style>
.ui-datepicker select.ui-datepicker-month,
.ui-datepicker select.ui-datepicker-year {
width: 48%;
margin-top: 0;
margin-bottom: 0;
line-height: 25px;
text-indent: 3px;
color: #888;
border-color: #DDD;
background-color: #FDFDFD;
-webkit-appearance: none; /*Optionally disable dropdown arrow*/
}
</style>
<script src="${pageContext.request.contextPath }/vendor/jquery/jquery-1.11.1.min.js"></script>
<script src="${pageContext.request.contextPath }/vendor/jquery/jquery_ui/jquery-ui.min.js"></script>
<script src="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/js/jquery.validate.min.js"></script>
<script src="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/js/additional-methods.min.js"></script>
<script src="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/js/jquery-ui-datepicker.min.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/utility/utility.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/demo/demo.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/main.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/pages.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/items.js"></script>
</body>
</html>
正在回答
同学你好,同学是要在product_add.jsp文件中展示分类吗?如果是,则无法通过findAll()方法中的数据进行展示的,则建议同学在saveUI()方法中查询数据,然后进行返回。如下所示:
祝学习愉快!
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星