我这样为啥不能添加新建项了,哪里错了
package com.imooc.web.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
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("/AddCategoryServlet")
public class AddCategoryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//if(request.getServletContext().getAttribute("categorys")==null) {
//List<Category> categroyList=new ArrayList<Category>();
String categoryId=request.getParameter("categoryId");
String categoryName=request.getParameter("categoryName");
/*
* Category category=new Category(categoryId, categoryName);
* categroyList.add(category);
*/
CategoryService categoryService=new CategoryServiceImpl();
categoryService.addCatetory(categoryId, categoryName);
request.setAttribute("categorys", categoryService.categoryDb);
// }else {
// String categoryId=request.getParameter("categoryId");
// String categoryName=request.getParameter("categoryName");
// //Category category=new Category(categoryId, categoryName);
// ServletContext context=request.getServletContext();
// /*List<Category> categroyList=(List<Category>)context.getAttribute("categorys");
// categroyList.add(category);*/
// CategoryService categoryService=new CategoryServiceImpl();
// categoryService.addCatetory(categoryId, categoryName);
// request.getServletContext().setAttribute("categorys", categoryService.categoryDb);
// }
//response.sendRedirect(request.getContextPath()+"/categoryList.jsp");
request.getRequestDispatcher("/categoryList.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.imooc.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.imooc.domain.Category;
import com.imooc.service.CategoryService;
public class CategoryServiceImpl implements CategoryService {
private static final List<Category> categoryDb=new ArrayList<Category>();
@Override
public void addCatetory(String categoryId, String categoryName) {
categoryDb.add(new Category(categoryId, categoryName));
}
@Override
public void deleteCatetory(String categoryId) {
// TODO Auto-generated method stub
}
public static List<Category> categoryDb(){
return categoryDb;
}
}
package com.imooc.service;
import java.util.ArrayList;
import java.util.List;
import com.imooc.domain.Category;
public interface CategoryService {
public static final List<Category> categoryDb=new ArrayList<Category>();
public void addCatetory(String categoryId,String categoryName);
public void deleteCatetory(String categoryId);
}
<%@ 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.jsp" >图书信息管理</a>
</nav>
<nav>
<a href="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="${applicationScope.categorys }" var="cate" varStatus="idx">
<tr>
<td>${idx.index+1 }</td>
<td>${cate.categoryId }</td>
<td>${cate.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>
正在回答
同学你好,虽然在接口中也可以定义常量,但是同学的写法,直接定义后在Servlet中,通过service获取到的就是他最初的空值,而在实现类中有了添加的方法,所以通过实现类可以获取到
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
为啥需要使用这个实现类呀,之前课程里面讲的不是都直接使用的CategoryService吗
还有划线的地方有啥用啊,我不写他也可以运行
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星