如何获取url的参数设置
来源:6-2 项目作业
weixin_慕仰6475988
2020-07-12 08:29:25
在UpdateBook.jsp中
String paraId=request.getParameter("bookId");
这个paraId一直是空的
//bookList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<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">
<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>
</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>
<tbody>
<c:forEach items="${applicationScope.bookList }" var="curBook" varStatus="idx">
<tr id="tr1">
<td>${idx.index+1 }</td>
<td>${curBook.id }</td>
<td>${curBook.bookName }</td>
<td>${curBook.className }</td>
<td>¥${curBook.price }</td>
<td><img src=${curBook.bookCover }></td>
<td>
<a href="${pageContext.request.contextPath }/updateBook.jsp?bookId=${curBook.id }">修改</a>
<a href="${pageContext.request.contextPath }/deleteBook.jsp?bookId=${curBook.id }">删除</a>
</td>
<!--在循环显示数据时,此处的book0001可以用EL表达式进行替换-->
</tr>
</c:forEach>
</tbody>
</table>
</div>
</section>
<section class="page">
<div class="container">
<div id="fatie">
<a href="addBook.jsp"><button>新建</button></a>
</div>
</div>
</section>
<footer>
copy@慕课网
</footer>
</body>
</html>
//updateBook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<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="/dept/list.do">
图书信息管理
</a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, ${sessionScope.existUser.username }!</h1>
<p>请小心的修改图书信息。。。</p>
</div>
<div class="page-header">
<h3><small>修改</small></h3>
</div>
<form class="form-horizontal" action="${pageContext.request.contextPath }/UpdateBook" method="post">
<div class="form-group">
<label for="bookId" class="col-sm-2 control-label">图书编号 :</label>
<div class="col-sm-8">
<input name="bookId" class="form-control" id="bookId" readonly="readonly" >
</div>
</div>
<div class="form-group">
<label for="bookName" 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="bookPrice" 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="bookPic" 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="remarks" 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>
</div>
</div>
</form>
</div>
<footer class="text-center" >
copy@imooc
</footer>
</body>
</html>
//UpdateBook
package com.imooc.addanddelete;
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;
/**
* Servlet implementation class UpdateBook
*/
@WebServlet("/UpdateBook")
public class UpdateBook extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String bookName = request.getParameter("bookName");
String categoryId = request.getParameter("categoryId");
/*
* if (categoryId.equals("ca0001")) categoryId = "计算机类"; else if
* (categoryId.equals("ca0002")) categoryId = "文学类"; else categoryId = "历史类";
*/
String bookPrice = request.getParameter("bookPrice");
String bookPic = request.getParameter("bookPic");
String temp = "img/";
bookPic = temp + bookPic;
String remarks = request.getParameter("remarks");
// public Book(int id, String bookName, String className, Float price, String bookCover, String remark) {
// Book b = new Book(bookId, bookName, categoryId, Float.parseFloat(bookPrice), bookPic,
// remarks);
ServletContext context = request.getServletContext();
List<Book> book = (List<Book>) context.getAttribute("bookList");
String paraId = request.getParameter("bookId");
for(Book curBook:book) {
if(curBook.getId().equals(paraId)) {
curBook.setBookCover(bookPic);
curBook.setBookName(bookName);
curBook.setClassName(categoryId);
curBook.setPrice(Float.parseFloat(bookPrice));
curBook.setRemark(remarks);
}
}
context.setAttribute("bookList", book);
response.sendRedirect(request.getContextPath() + "/bookList.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4回答
好帮手慕阿园
2020-07-12
同学你好,因为新增图书信息时有文件上传项,所以建议同学使用上传文件的方式来接收数据,可以参考课程中的第三章来完成,如下
关于修改图书的代码同学可参考以下这个问答中老师的代码
http://class.imooc.com/course/qadetail/225742
祝学习愉快
weixin_慕仰6475988
提问者
2020-07-12
我的添加图书是这样实现的
所以不会你说的方法
可以给个示例的代码?
//addBook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<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="/dept/list.do">
图书信息管理
</a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, ${sessionScope.existUser.username }!</h1>
<p>请小心地新增图书信息,要是建了一个错误的就不好了。。。</p>
</div>
<div class="page-header">
<h3><small>新建</small></h3>
</div>
<form class="form-horizontal" action="${pageContext.request.contextPath }/AddBook" method="post">
<div class="form-group">
<label for="bookId" class="col-sm-2 control-label">图书编号 :</label>
<div class="col-sm-8">
<input name="bookId" class="form-control" id="bookId">
</div>
</div>
<div class="form-group">
<label for="bookName" 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="bookPrice" 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="bookPic" 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="remarks" 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>
</div>
</div>
</form>
</div>
<footer class="text-center" >
copy@imooc
</footer>
</body>
</html>
//AddBook.java
package com.imooc.addanddeleteandsearch;
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 com.imooc.domain.Category;
/**
* Servlet implementation class AddBook
*/
@WebServlet("/AddBook")
public class AddBook extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = request.getServletContext();
List<Category> categoryList = (List<Category>) context.getAttribute("categoryList");
String bookId = request.getParameter("bookId");
String bookName = request.getParameter("bookName");
String categoryId = request.getParameter("categoryId");
String categoryName;
if (categoryId.equals("ca0001"))
categoryName = "计算机类";
else if (categoryId.equals("ca0002"))
categoryName = "文学类";
else
categoryName = "历史类";
boolean flag = true;
for (Category curCategory : categoryList) {
if (curCategory.getClassName().equals(categoryName)) {
flag = false;
break;
}
}
if (flag)
categoryList.add(new Category(categoryId, categoryName));
String bookPrice = request.getParameter("bookPrice");
String bookPic = request.getParameter("bookPic");
String temp = "img/";
bookPic = temp + bookPic;
String remarks = request.getParameter("remarks");
// public Book(int id, String bookName, String className, Float price, String bookCover, String remark) {
Book b = new Book(bookId, bookName, categoryName, Float.parseFloat(bookPrice), bookPic, remarks,categoryId);
System.out.println(b);
List<Book> book = (List<Book>) context.getAttribute("bookList");
book.add(b);
context.setAttribute("bookList", book);
context.setAttribute("categoryList", categoryList);
response.sendRedirect(request.getContextPath() + "/bookList.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
好帮手慕阿园
2020-07-12
同学你好,编程题实现的方式有很多种,每个人的思路也不一样,所以老师这里也没有标准答案,
修改的Servlet与添加图书的Servlet相似,同学可以参考添加图书的servlet来实现接收页面中提交的内容,比如:
2)接收完数据内容后,封装成一个Book对象,调用update修改方法,将这个Book对象传入,调用修改方法。
3)调用update修改方法,需要将封装好的Book对象传入,遍历所有图书,判断每个图书id与传入的图书id是否一致,如果一致,则对该对象的属性进行判断,比如同学的代码:
祝学习愉快
好帮手慕阿园
2020-07-12
同学你好,获取修改后的图书信息应该以上传文件的方式接收数据,并且在jsp页面的form表单中增加enctype属性enctype="multipart/form-data",设置post提交
UpdateBookServlet文件上传参考代码如下
jsp页面的form表单中添加enctype属性,如下
祝学习愉快
相似问题