设置了enctype属性,获取不到url中bookId的属性值,其他表单数据都可以why?
<%@ 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="/dept/list.do">
图书信息管理
</a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, ${sessionScope.userName}!</h1>
<p>请小心的修改图书信息。。。</p>
</div>
<div class="page-header">
<h3><small>修改</small></h3>
</div>
<form encType="multipart/form-data" class="form-horizontal" action="${pageContext.request.contextPath}/updateBookServlet" method="post">
<div class="form-group">
<label for="bookId" class="col-sm-2 control-label">图书编号 :</label>
<div class="col-sm-8">
<!-- 这里要动态获取url地址传过来的bookId -->
<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">
<!-- (也可以用el表达式)
<c:forEach items="${categoryList }" var="c" varStatus="idx">
<option value="${c.cateId }" selected="">${c.cateName }</option>
下拉列表的内容要从分类中进行读取,value值是分类id
</c:forEach> -->
</select>
</div>
<div class="form-group">
<label for="bookPrice" class="col-sm-2 control-label">价格 :</label>
<div class="col-sm-8">
<input onkeyup="value=value.replace(/[^\d]/g,'') "
onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
ID="price" NAME="bookPrice" class="form-control">
</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>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript">
/*------------------------------------------------------------------------------------------------*/
$(function(){
//1.设置bookid
$("#bookId").val();
//根據分類集合獲取所有分類名稱展示下拉列表
$.ajax({
"url":"/BookShoopDome/selectCategoryServlet",
//"data" :"",
"type":"post",
"dataType":"json",
"success":function(json){
var html = "";
$("#categoryId>option").remove();
for(var i=0;i<json.length;i++){
var category = json[i];
if(i==0){
html=html+ "<option value="+category.categoryId+" selected=''>"+category.categoryName+"</option>";
}else{
html=html+ "<option value="+category.categoryId+">"+category.categoryName+"</option>";
}
}
$("#categoryId").append(html);
}
})
})
</script>
</body>
</html>
package com.dragon.web.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.dragon.domain.Book;
import com.dragon.domain.Category;
import com.dragon.service.impl.BookService;
import com.dragon.service.impl.BookServiceImpl;
import com.dragon.service.impl.CategoryService;
import com.dragon.service.impl.CategoryServiceImpl;
import com.dragon.utils.UploadUtils;
/**
* Servlet implementation class newBookServlet
*/
@WebServlet("/updateBookServlet")
public class updateBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取分類列表
// 接收数据
// 创建Map集合用于保存数据:
Map<String,String> map = new HashMap<String,String>();
// 文件上传的代码:
// 1.创建磁盘文件项工厂
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
// 2.创建核心解析类
ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory);
// 3.解析请求对象,将请求分成几个部分(FileItem)
try {
List<FileItem> list = fileUpload.parseRequest(request);
// 4.遍历集合获得每个部分的对象
for (FileItem fileItem : list) {
// 判断是普通项还是文件上传项
if (fileItem.isFormField()) {
// 普通项--用户名(username--输入的值) 密码 确认密码
// 获得普通项的名称:
String name = fileItem.getFieldName();
// 获得普通项的值:
String value = fileItem.getString("UTF-8");
// 保存数据:
map.put(name, value);
}else{
// 文件上传项
// 获得文件的名称:
String fileName = fileItem.getName();
// (利用工具类UploadUtils)获得唯一文件名:
String uuidFileName = UploadUtils.getUuidFileName(fileName);
// 获得文件的输入流:
InputStream is = fileItem.getInputStream();
// 需要将文件写入到服务器的某个路径即可:
String path = getServletContext().getRealPath("/upload");
System.out.println(path);// D:/xxx/ddd/
// 显示图片<img src="/regist_login/upload/a.jpg">
// 创建输出流 与 输入流进行对接:
String url = path + "\\" + uuidFileName;//绝对路径
//设置相对路径,以便后面获取图片显示
map.put("path", request.getContextPath()+"/upload/"+uuidFileName);
System.out.println(request.getContextPath()+"/upload/"+uuidFileName);
OutputStream os = new FileOutputStream(url);
int len = 0;
byte[] b = new byte[1024];
while ((len = is.read(b)) != -1) {
os.write(b, 0, len);//将文件输出到服务器文件里
}
is.close();
os.close();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//修改图书的表单数据(通过bookId属性)
String bookId = (String)request.getParameter("bookId");
System.out.println(map);
//遍历图书集合,找到图书进行修改
BookService bookService = new BookServiceImpl();
CategoryService categoryService =new CategoryServiceImpl();
List<Category> categoryList = categoryService.getCategoryList();
Book book = new Book();
book.setBookId(bookId);
book.setBookName(map.get("bookName"));
for(Category cate:categoryList) {
if(cate.getCategoryId().equals(map.get("categoryId"))) {
book.setCategoryName(cate.getCategoryName());
}
}
book.setImgPath(map.get("path"));
book.setMassage(map.get("remarks"));
book.setPrice(map.get("bookPrice"));
//修改数据
bookService.updateBook(book);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
获取到表单的其他数据都可以,就只有onready的bookId获取值为空,但是在访问地址栏中bookId有值
http://localhost:8080/BookShoopDome/updateBook.jsp?bookId=002
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星