老师帮忙看一下是哪里出问题了
来源:6-2 项目作业
J_DP
2020-07-03 07:04:27
import java.util.ArrayList;
import java.util.List;
import com.imooc.domain.Category;
import com.imooc.web.service.CategoryService;
public class CategoryServiceImpl implements CategoryService {
private static final List<Category> categoryDb = new ArrayList<Category>();
@Override
public void addCategory(String categoryId,String categoryName) {
Category cate = new Category(categoryId,categoryName);
categoryDb.add(cate);
System.out.println(categoryDb);
}
@Override
public void deleteCatgory(String categoryId) {
for(Category category:categoryDb) {
if(category.getCategoryId().equals(categoryId)) {
categoryDb.remove(category);
break;
}
}
}
@Override
public List<Category> getCategoryDb() {
System.out.println(categoryDb);
return categoryDb;
}
}
package com.imooc.servlet;
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.web.service.CategoryService;
import com.imooc.web.service.impl.CategoryServiceImpl;
@WebServlet("/AddCategoryServlet")
public class AddCatgoryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 接收数据
request.setCharacterEncoding("UTF-8");
String categoryId = request.getParameter("categoryId");
String categoryName = request.getParameter("categoryName");
System.out.println("id:"+ categoryId );
// 封装数据
Category cate = new Category(categoryId,categoryName);
// 处理数据
CategoryService src = new CategoryServiceImpl();
src.addCategory(categoryId, categoryName);
request.getSession().setAttribute("cateList",src.getCategoryDb());
// 显示结果
response.sendRedirect(request.getContextPath() + "/categoryList.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
<%@ 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">
<title>新建图书分类</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/add.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="">
图书分类管理
</a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, XXX!</h1>
<p>请小心地新增图书分类,要是建了一个错误的就不好了。。。</p>
</div>
<div class="page-header">
<h3><small>新建</small></h3>
</div>
<form class="form-horizontal" action="${pageContext.request.contextPath }/AddCategoryServlet" method="post">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">分类ID :</label>
<div class="col-sm-8">
<input name="categoryId" class="form-control" id="categoryId">
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">分类名称 :</label>
<div class="col-sm-8">
<input name="categoryName" class="form-control" id="categoryName">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</form>
</div>
<footer class="text-center" >
copy@imooc
</footer>
</body>
</html>
<%@ 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">
<title>图书后台管理</title>
<link rel="stylesheet" href="css/index.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<header>
<div class="container">
<nav>
<a href="bookList.html" >图书信息管理</a>
</nav>
<nav>
<a href="${pageContext.request.contextPath }/categoryList.jsp" >分类管理</a>
</nav>
</div>
</header>
<section class="banner">
<div class="container">
<div>
<h1>图书管理系统</h1>
<p>图书分类管理</p>
</div>
</div>
</section>
<section class="main">
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>序号</th>
<th>分类编号</th>
<th>分类名称</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${sessionScope.cateList }" var ="ca" varStatus="idx">
<tr>
<td>${idx.index+1}</td>
<td>${ca.categoryId}</td>
<td>${ca.categoryName}</td>
<td><a href="/deleteCategory?categoryId=ca0001">删除</a></td>
<!--在循环显示数据时,此处的ca0001可以用EL表达式进行替换-->
</tr>
</c:forEach>
</tbody>
</table>
</div>
</section>
<section class="page">
<div class="container">
<div id="fatie">
<a href="addCategory.jsp"><button>新建</button></a>
</div>
</div>
</section>
<footer>
copy@慕课网
</footer>
</body>
</html>
传值一只是null。。
[Category [categoryId=null, categoryName=null], Category [categoryId=null, categoryName=null], Category [categoryId=null, categoryName=null]]
老师这是哪里出问题了,试好多种方法,找不到问题所在,就是一直无法传值进去
新增分类就只有序号,其他都是null
1回答
同学你好,检查同学贴出代码,在servlet中获取参数,调用addCategory方法的书写并没有问题,同学输出内容为空,是不是在Category 的构造方法的赋值有问题,同学可以将Category的内容贴出,老师来查看一下。
Ps: 在servlet中获取参数,比如 categoryName能正常获取到吗?
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题