急急急,关于新建图书分类类在jsp页面不显示问题
-categoryList.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">
<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="categoryList.html">分类管理</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.categoryDb }" var="ctg" varStatus="idx">
<tr>
<td>${idx.index+1 }</td>
<td>${ctg.categoryId }</td>
<td>${ctg.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>
-addCategory.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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="categoryId" 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="categoryName" 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>
<p>${ msg}</p>
</div>
</div>
</form>
</div>
<footer class="text-center"> copy@imooc </footer>
</body>
</html>
-InitServletContextListener
package com.imooc.web.listener;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.imooc.domain.Category;
import com.imooc.domain.User;
public class InitServletContextListener implements ServletContextListener {
public InitServletContextListener() {
}
// 销毁
public void contextDestroyed(ServletContextEvent sce) {
}
// 初始化用户和图书分类类
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
// 创建一个用于保存用户信息的List集合
List<User> userList = new ArrayList<User>();
// 将List集合存入到ServletContext域中
userList.add(new User("aaa", "123456"));
userList.add(new User("bbb", "234567"));
userList.add(new User("ccc", "345678"));
context.setAttribute("userList", userList);
if (context.getAttribute("cateogryDb") == null) {
List<Category> ctgList = new ArrayList<Category>();
ctgList.add(new Category("ca0001", "计算机类"));
context.setAttribute("categoryDb", ctgList);
}
}
}
-接口
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 addCategory(String categoryId, String categoryName);
public void deleteCatgory(String categoryId);
}
-实现类
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>();
public static List<Category> getCategoryDb() {
return categoryDb;
}
//纯添加,判断在servlet里进行
@Override
public void addCategory(String categoryId, String categoryName) {
Category category = new Category(categoryId, categoryName);
categoryDb.add(category);
}
@Override
public void deleteCatgory(String categoryId) {
Category categoryExist = null;
for (Category c : categoryDb) {
if (categoryId.equals(c.getCategoryId())) {
categoryExist = c;
}
}
}
}
-AddCategoryServlet
package com.imooc.web.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.service.CategoryService;
import com.imooc.service.impl.CategoryServiceImpl;
@WebServlet("/AddCategoryServlet")
public class AddCategoryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AddCategoryServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 接受数据
String categoryId = request.getParameter("categoryId");
String categoryName = request.getParameter("categoryName");
// 在这里判断输入的是否为空(根据id)
if (categoryId.isEmpty() && categoryName.isEmpty()) {
request.setAttribute("msg", "id及名称为空!");
request.getRequestDispatcher("/addCategory.jsp").forward(request, response);
} else {
// 封装数据
Category ctg = new Category(categoryId, categoryName);
CategoryService ctgService = new CategoryServiceImpl();// 接口引用指向具体的实现类,多态的体现
// 处理数据
// 在这里判断一下列表里是否已经有id重名的情况再存进去
List<Category> list = CategoryServiceImpl.getCategoryDb();
for (Category cExist : list) {
if (cExist.getCategoryId().equals(ctg.getCategoryId())
&& cExist.getCategoryName().equals(ctg.getCategoryName())) {
request.setAttribute("msg", "该分类已存在!");
request.getRequestDispatcher("/addCategory.jsp").forward(request, response);
return;
} else {
ctgService.addCategory(categoryId, categoryName);
request.getServletContext().setAttribute("categoryDb", CategoryServiceImpl.getCategoryDb());
}
}
}
// 交给jsp展示数据
request.getRequestDispatcher("/categoryList.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
老师请问
1、为什么我新建图书分类在jsp页面不显示,点击前后如图。自己已经找到了一些问题了,比如因为监听器初始化不显示设置在了全局域中,应该从ServletContext域中获取
request.getServletContext().setAttribute("categoryDb", CategoryServiceImpl.getCategoryDb());
2、初始化放在监听器中可以吗,有没有更好的方法
3、怎么可以把判断放在实现类中,我看了老师其他答案在void add方法中返回boolean变量,但在AddServlet中怎么获取这个变量啊
4、关于msg,我判断为空的提示正常输出显示,但重名已存在的msg怎么不显示啊
正在回答
同学你好,老师是将同学的Servlet拼接到自己的项目中,让代码能跑起来,现在测试同学的代码有前面提出的三个问题,同学使用如下修改后的代码再来试试。
package com.imooc.web.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.service.CategoryService; import com.imooc.service.impl.CategoryServiceImpl; @WebServlet("/AddCategoryServlet") public class AddCategoryServlet extends HttpServlet { private static final long serialVersionUID = 1L; public AddCategoryServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 接受数据 String categoryId = request.getParameter("categoryId"); String categoryName = request.getParameter("categoryName"); // 在这里判断输入的是否为空(根据id) if (categoryId.isEmpty() && categoryName.isEmpty()) { request.setAttribute("msg", "id及名称为空!"); request.getRequestDispatcher("/addCategory.jsp").forward(request, response); } else { // 封装数据 Category ctg = new Category(categoryId, categoryName); CategoryService ctgService = new CategoryServiceImpl();// 接口引用指向具体的实现类,多态的体现 // 处理数据 // 在这里判断一下列表里是否已经有id重名的情况再存进去 List<Category> list = CategoryServiceImpl.getCategoryDb(); boolean flag = false; for (Category cExist : list) { if (cExist.getCategoryId().equals(ctg.getCategoryId()) && cExist.getCategoryName().equals(ctg.getCategoryName())) { flag = true; System.out.println("存在相同的"); return; } else { flag = false; System.out.println("没有相同的"); //return; //这里else不需要再次return } } //if (flag = false) { //flag判断要使用== if (flag == false) { ctgService.addCategory(categoryId, categoryName); request.getServletContext().setAttribute("categoryDb", CategoryServiceImpl.getCategoryDb()); System.out.println("添加成功"); request.getRequestDispatcher("/categoryList.jsp").forward(request, response); } else { System.out.println("存在相同的没有添加"); request.setAttribute("msg", "该分类已存在!"); request.getRequestDispatcher("/addCategory.jsp").forward(request, response); } } // 交给jsp展示数据 //if-else条件语句执行完后不需要再次执行跳转语句 //request.getRequestDispatcher("/categoryList.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
如上所示就是修改后的代码,同学再来测试运行一下。
同学你好,测试拼接同学贴出的代码有如下问题:
1、提交响应后无法转发。
因为同学在if--else的条件语句中已经存在转发,在if的条件外面又进行了转发,导致出现这个问题,建议同学直接在if条件中书写对应的跳转,比如:
2、在判断列表里是否已经有id重名的情况时,不要在else中写return语句,return表示返回不再继续执行。
这样书写条件语句有问题,比如: 当传入的分类属性与集合中的第一个对象不一样,它直接判断完成后直接跳出,最后得到的结果就是列表里没有重名的id,但是它只比较了集合中的第一个对象。
修改建议如下:
3、注意判断是"==",赋值是"=",在最后的判断语句中,注意是==,
因为是拼接同学的代码,可能某些方法名或属性与同学的不一样,同学理解意思就好。如上所示的问题修改后再来试试。
package com.imooc.web.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.service.CategoryService;
import com.imooc.service.impl.CategoryServiceImpl;
@WebServlet("/AddCategoryServlet")
public class AddCategoryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AddCategoryServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 接受数据
String categoryId = request.getParameter("categoryId");
String categoryName = request.getParameter("categoryName");
// 在这里判断输入的是否为空(根据id)
if (categoryId.isEmpty() && categoryName.isEmpty()) {
request.setAttribute("msg", "id及名称为空!");
request.getRequestDispatcher("/addCategory.jsp").forward(request, response);
} else {
// 封装数据
Category ctg = new Category(categoryId, categoryName);
CategoryService ctgService = new CategoryServiceImpl();// 接口引用指向具体的实现类,多态的体现
// 处理数据
// 在这里判断一下列表里是否已经有id重名的情况再存进去
List<Category> list = CategoryServiceImpl.getCategoryDb();
boolean flag = false;
for (Category cExist : list) {
if (cExist.getCategoryId().equals(ctg.getCategoryId())
&& cExist.getCategoryName().equals(ctg.getCategoryName())) {
flag = true;
System.out.println("存在相同的");
return;
} else {
flag = false;
System.out.println("没有相同的");
return;
}
}
if (flag = false) {
ctgService.addCategory(categoryId, categoryName);
request.getServletContext().setAttribute("categoryDb", CategoryServiceImpl.getCategoryDb());
System.out.println("添加成功");
} else {
System.out.println("存在相同的没有添加");
request.setAttribute("msg", "该分类已存在!");
request.getRequestDispatcher("/addCategory.jsp").forward(request, response);
}
}
// 交给jsp展示数据
request.getRequestDispatcher("/categoryList.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
唉,还是不行啊老师
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星