1、在edit.html修改库存页面添加点击事件
<link rel="stylesheet" href="style/index.css">
<script src="script/axios.min.js">script>
<script src="script/edit.js">script>
<script src="script/common.js">script>
<div id="div_fruit_table">
<input type="hidden" id="fid" value="0"/>
<td><input type="text" id="fname"/>td>
<td><input type="text" name="price" id="price"/> td>
<td><input type="text" name="fcount" id="fcount"/> td>
<td><input type="text" name="remark" id="remark"/> td>
<input type="button" value="修改" onclick="update()"/>
<input type="button" value="取消"/>
2、在edit.js中添加update = function ( ) { } 功能
2.1、common.js
return document.getElementById(key)
let nodeList = document.getElementsByName(key)
return Array.from(nodeList)
2.2、edit.js
let queryString = window.location.search.substring(1)
var fid = queryString.split("=")[1]
window.onload=function(){
loadFruit = function(fid){
let fruit = response.data.data
$("#fid").value=fruit.fid
$("#fname").value=fruit.fname
$("#price").value=fruit.price
$("#fcount").value=fruit.fcount
$("#remark").value=fruit.remark
let fid = $("#fid").value
let fname = $("#fname").value
let price = $("#price").value
let fcount = $("#fcount").value
let remark = $("#remark").value
window.location.href="index.html"
3、封装读取json字符串方法创建RequestUtil工具类
package com.csdn.fruit.util;
import jakarta.servlet.ServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
public class RequestUtil {
public static Object readObject(ServletRequest request, Class clazz) throws IOException {
BufferedReader br = request.getReader();
StringBuilder stringBuilder = new StringBuilder();
while ((str = br.readLine()) != null) {
stringBuilder.append(str);
str = stringBuilder.toString();
return GsonUtil.fromJson(str, clazz);
4、修改FruitDaoImpl类中的updateFruit方法中sql语句
- 因为之前写的是只能通过 fname 修改 fcount
- 现在想要通过 fid 可以修改所有的属性值,所以需要修改sql语句
package com.csdn.fruit.dao.impl;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.pojo.Fruit;
import com.csdn.mymvc.dao.BaseDao;
public class FruitDaoImpl extends BaseDao implements FruitDao {
public void addFruit(Fruit fruit) {
String sql = "insert into t_fruit values (0,?,?,?,?)";
super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark());
public void delFruit(String fname) {
String sql = "delete from t_fruit where fname=?";
super.executeUpdate(sql, fname);
public void updateFruit(Fruit fruit) {
String sql = "update t_fruit set fname=?,price=?,fcount=?,remark=? where fid = ?";
super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark(), fruit.getFid());
public List getFruitList() {
return super.executeQuery("select * from t_fruit");
public Fruit getFruitByFname(String fname) {
return load("select * from t_fruit where fname = ?", fname);
public Fruit getFruitByFid(Integer fid) {
return load("select * from t_fruit where fid=?", fid);

5、编写Controller层UpdateServlet
package com.csdn.fruit.servlet;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.dao.impl.FruitDaoImpl;
import com.csdn.fruit.dto.Result;
import com.csdn.fruit.pojo.Fruit;
import com.csdn.fruit.util.RequestUtil;
import com.csdn.fruit.util.ResponseUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UpdateServlet extends HttpServlet {
private FruitDao fruitDao = new FruitDaoImpl();
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Fruit fruit = (Fruit) RequestUtil.readObject(req, Fruit.class);
fruitDao.updateFruit(fruit);
ResponseUtil.print(resp, Result.OK());
