关于修改图书中编号设置的问题

来源:6-2 项目作业

xxxxxxb

2020-01-20 20:36:39

http://img.mukewang.com/climg/5e259d450965f4f611890698.jpg关于上面的图书编号怎么设置我自己想了两个方法

一个是在updateBook.jsp中将value值改为 ”${book.getBookId() }“,因为我在AddBookServlet中已经实例化了Book对象book,我的想法是可以直接通过这个el表达式提取到图书编号,不过显示不行;

第二个方法是点击修改转到一个BookIdServlet中request.getParameter("bookId");然后将这里获得的ID放在修改页面中,不过输出值显示为null;所以我想问下这个图书编号该如何获得?

===================

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.Book;


/**

 * 新增图书的servlet,新增完后转到bookList.jsp显示

 */

@WebServlet("/addBook")

public class AddBookServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


//创建一个list存放封装的数据,并且放在最上面防止多次new

List<Book> bookList = new ArrayList<Book>();

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// response.setCharacterEncoding("text/html;charset=UTF-8");

request.setCharacterEncoding("UTF-8");

//获取新增页面输入的值

String bookId = request.getParameter("bookId");

String bookName = request.getParameter("bookName");

String categoryId = request.getParameter("categoryId");

System.out.println(categoryId);

String bookPrice = request.getParameter("bookPrice");

String remarks = request.getParameter("remarks");

//封装

Book book = new Book();

book.setBookId(bookId);

book.setBookName(bookName);

book.setCategoryId(categoryId);

book.setBookPrice(bookPrice);

book.setRemarks(remarks);

//将获得的数据放到全局中供bookList.jsp使用

bookList.add(book);

ServletContext context = request.getServletContext();

context.setAttribute("bookLists", bookList);

System.out.println("bookList为"+bookList);

//跳转到新建分类页面

response.sendRedirect(request.getContextPath()+"/bookList.jsp");

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

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/index.css">

<link rel="stylesheet" href="css/bootstrap.min.css">


</head>


<body>

<header>

<div class="container">

<nav>

<a href="${pageContext.request.contextPath }/bookList.jsp">图书信息管理</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">

<form class="form-horizontal" action="/searchBook" method="post">

<div class="form-group" style="float: right;">

<div class="col-sm-offset-2 col-sm-10">

<button type="submit" class="btn btn-primary">查询</button>

&nbsp;&nbsp;&nbsp;

</div>

</div>

<div class="form-group" style="float: right; width: 300px;">

<div class="col-sm-8">

<input name="searchContent" class="form-control"

id="searchContent" placeholder="输入要查询的分类" style="width: 250px">

</div>

</div>



</form>

</div>

<div class="container">


<table class="table table-striped">

<thead>

<tr>

<th>序号</th>

<th>图书编号</th>

<th>图书名称</th>

<th>分类</th>

<th>价格</th>

<th>图书封面</th>

<th>操作</th>


</tr>

</thead>

<%-- <tr id="tr1">

<td>1</td>

<td>book0001</td>

<td>Java基础</td>

<td>计算机类</td>

<td>¥29</td>

<td><img src="img/g1.jpg"></td>

<td><a

href="${pageContext.request.contextPath }/updateBook?bookId=book0001">修改</a>

<a

href="${pageContext.request.contextPath }/deleteBook?bookId=book0001">删除</a>


</td>

<!--在循环显示数据时,此处的book0001可以用EL表达式进行替换--> --%>

</tr>

<tbody>

<c:forEach items="${applicationScope.bookLists }" var="book"

varStatus="idx">

<tr id="tr1">

<td>${idx.index+1 }</td>

<td>${book.bookId }</td>

<td>${book.bookName }</td>

<td>${book.categoryId }</td>

<td>¥${book.bookPrice }</td>

<td><img src="img/g1.jpg"></td>

<!-- 点击修改>转到BookIdServlet.java以存储bookId然后放到后面的jsp页面中>转到修改页面updateBook.jsp>修改页面点击保存>UpdateBookServlet.java> -->

<td>

<a

href="${pageContext.request.contextPath }/bookId">修改</a>

<a

href="${pageContext.request.contextPath }/deleteBook?bookId=${book.bookId }">删除</a>


</td>

<!--在循环显示数据时,此处的book0001可以用EL表达式进行替换-->

</tr>

</c:forEach>

</tbody>

</table>

</div>

</section>

<section class="page">

<div class="container">

<div id="fatie">

<a href="${pageContext.request.contextPath }/addBook.jsp"><button>新建</button></a>

</div>

</div>

</section>

<footer> copy@慕课网 </footer>

</body>

</html>

=================

package com.imooc.web.servlet;


import java.io.IOException;

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.Book;


import jdk.nashorn.internal.runtime.Context;


/**

 * 修改图书的servlet

 */

@WebServlet("/updateBook")

public class UpdateBookServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// response.setCharacterEncoding("text/html;charset=UTF-8");

// request.setCharacterEncoding("UTF-8");

//获取bookId,根据bookId修改List

String bookId = request.getParameter("bookId");

System.out.println("要修改的bookId为"+bookId);

//封装bookId

Book book = new Book();

book.setBookId(bookId);

//获取context中的bookLists

//1.获取存放在全局中的bookList

ServletContext context = request.getServletContext();

List<Book> bookList = (List<Book>) context.getAttribute("bookLists");

System.out.println("全局中的bookList为"+bookList);

//修改后跳转到bookList.jsp

response.sendRedirect(request.getContextPath()+"/bookList.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="${pageContext.request.contextPath }/bookList.jsp">

                        图书信息管理

                    </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 }/updateBook?bookId=${book.bookId }" method="post">


                <div class="form-group">

                    <label for="name" class="col-sm-2 control-label">图书编号 :</label>

                    <div class="col-sm-8">

                        <input name="bookId" class="form-control" id="bookId" readonly="readonly" value="${book.getBookId() }">

                    </div>

                </div>

                <div class="form-group">

                    <label for="name" class="col-sm-2 control-label">图书名称 :</label>

                    <div class="col-sm-8">

                        <input name="bookName" class="form-control" id="bookName">

                    </div>

                </div>

                <div class="form-group">

                    <label for="categoryId" class="col-sm-2 control-label">分类 :</label>

                    <select id="categoryId" name="categoryId" class="col-sm-2 form-control" style="width: auto;margin-left: 15px">

                       <option value="ca0001" selected="">计算机</option>

                       <option value="ca0002">文学</option>

                       <option value="ca0003">历史</option>

                       <!-- 下拉列表的内容要从分类中进行读取,value值是分类id -->

                    </select>

                </div>


                 <div class="form-group">

                    <label for="name" class="col-sm-2 control-label">价格 :</label>

                    <div class="col-sm-8">

                        <input name="bookPrice" class="form-control" id="bookPrice">

                    </div>

                  </div>

                   

                  <div class="form-group" >

                    <label for="name" class="col-sm-2 control-label">图书封面 :</label>

                    <input type="file" id="bookPic" name="bookPic" style="padding-left: 15px">

                  </div>


                  <div class="form-group">

                    <label for="name" class="col-sm-2 control-label">备注 :</label>

                    <div class="col-sm-8">

                        <input name="remarks" class="form-control" id="remarks">

                    </div>

                  </div>


                <div class="form-group">

                    <div class="col-sm-offset-2 col-sm-10">

                        <button type="submit" class="btn btn-primary">修改</button>&nbsp;&nbsp;&nbsp;

                    </div>

                </div>

            </form>

        </div>

        <footer class="text-center" >

            copy@imooc

        </footer>

    </body>

</html>

==================

package com.imooc.web.servlet;


import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

 * BookIdServlet.java以存储bookId放到全局,然后在修改的jsp页面中提取出来

 */

@WebServlet("/bookId")

public class BookIdServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String bookId = request.getParameter("bookId");

System.out.println(bookId);

//跳转到修改页面

response.sendRedirect(request.getContextPath()+"/updateBook.jsp");

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}


}


写回答

3回答

好帮手慕阿满

2020-01-21

同学你好,在同学的代码中,是查询后的book存入request中,但是在跳转页面时,使用的是重定向,如:

http://img.mukewang.com/climg/5e265ffe097b154207350319.jpg

在重定向时,request中参数并不会传递到页面中,所以携带参数跳转时,应该使用转发跳转到页面。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

0
hxxxxxb
h 确实是这里的问题,感谢老师
h020-01-21
共1条回复

xxxxxxb

提问者

2020-01-20

我还有一问,为什么

我在BookIdServlet中获取了Id值,

但是没法再jsp中调取啊?

=============

package com.imooc.web.servlet;


import java.io.IOException;

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.Book;


/**

 * BookIdServlet.java以存储bookId放到全局,然后在修改的jsp页面中提取出来

 */

@WebServlet("/updateBookId")

public class BookIdServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String bookId = request.getParameter("bookId");

System.out.println("要修改的bookId为"+bookId);

//封装

Book book = new Book();

book.setBookId(bookId);

request.setAttribute("book", book);

//

// ServletContext context = request.getServletContext();

// List<Book> bookList = (List<Book>) context.getAttribute("bookLists");

//跳转到修改页面

response.sendRedirect(request.getContextPath()+"/updateBook.jsp");

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}


}

================

<%@ 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="${pageContext.request.contextPath }/bookList.jsp">

                        图书信息管理

                    </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 }/updateBook?bookId=${book.bookId }" method="post">


                <div class="form-group">

                    <label for="name" class="col-sm-2 control-label">图书编号 :</label>

                    <div class="col-sm-8">

                        <input name="bookId" class="form-control" id="bookId" readonly="readonly" value="${requestScope.book.bookId }">

                    </div>

                </div>

                <div class="form-group">

                    <label for="name" class="col-sm-2 control-label">图书名称 :</label>

                    <div class="col-sm-8">

                        <input name="bookName" class="form-control" id="bookName">

                    </div>

                </div>

                <div class="form-group">

                    <label for="categoryId" class="col-sm-2 control-label">分类 :</label>

                    <select id="categoryId" name="categoryId" class="col-sm-2 form-control" style="width: auto;margin-left: 15px">

                       <option value="ca0001" selected="">计算机</option>

                       <option value="ca0002">文学</option>

                       <option value="ca0003">历史</option>

                       <!-- 下拉列表的内容要从分类中进行读取,value值是分类id -->

                    </select>

                </div>


                 <div class="form-group">

                    <label for="name" class="col-sm-2 control-label">价格 :</label>

                    <div class="col-sm-8">

                        <input name="bookPrice" class="form-control" id="bookPrice">

                    </div>

                  </div>

                   

                  <div class="form-group" >

                    <label for="name" class="col-sm-2 control-label">图书封面 :</label>

                    <input type="file" id="bookPic" name="bookPic" style="padding-left: 15px">

                  </div>


                  <div class="form-group">

                    <label for="name" class="col-sm-2 control-label">备注 :</label>

                    <div class="col-sm-8">

                        <input name="remarks" class="form-control" id="remarks">

                    </div>

                  </div>


                <div class="form-group">

                    <div class="col-sm-offset-2 col-sm-10">

                        <button type="submit" class="btn btn-primary">修改</button>&nbsp;&nbsp;&nbsp;

                    </div>

                </div>

            </form>

        </div>

        <footer class="text-center" >

            copy@imooc

        </footer>

    </body>

</html>

===================

如上,value的值为value="${requestScope.book.bookId }

但是无法显示出来

http://img.mukewang.com/climg/5e25ae0c09a66dab14610688.jpghttp://img.mukewang.com/climg/5e25ae1d0919cb1e11690613.jpg

0
hxxxxxb
h 另外我试了下${book.getBookId() }也不行
h020-01-20
共1条回复

xxxxxxb

提问者

2020-01-20

我知道了,

<a

href="${pageContext.request.contextPath }/updateBookId?bookId=${book.bookId }">修改</a>

只要加个?bookId=${book.bookId }就能在Servlet中通过request.getParameter("bookId");获取bookId了,和删除的方法获取ID是一样的

0

0 学习 · 9666 问题

查看课程