1、在edit.html修改库存页面添加点击事件
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><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>
</head>
<body>
<div id="div0"><div id="div_title"><p>编辑水果库存信息</p></div><div id="div_fruit_table"><input type="hidden" id="fid" value="0"/><table id="fruit_tbl"><tr><th class="w25">名称:</th><td><input type="text" id="fname"/></td></tr><tr><th class="w25">单价:</th><td><input type="text" name="price" id="price"/> </td></tr><tr><th class="w25">库存:</th><td><input type="text" name="fcount" id="fcount"/> </td></tr><tr><th class="w25">备注:</th><td><input type="text" name="remark" id="remark"/> </td></tr><tr><th colspan="2"><input type="button" value="修改" onclick="update()"/><input type="button" value="取消"/></th></tr></table></div>
</div>
</body>
</html>
2、在edit.js中添加update = function ( ) { } 功能
2.1、common.js
function $(key){if(key){if(key.startsWith("#")){key = key.substring(1)return document.getElementById(key)}else{let nodeList = document.getElementsByName(key)return Array.from(nodeList)}}
}
2.2、edit.js
let queryString = window.location.search.substring(1)
if(queryString){var fid = queryString.split("=")[1]window.onload=function(){loadFruit(fid)}loadFruit = function(fid){axios({method:'get',url:'edit',params:{fid:fid}}).then(response=>{debuggerlet fruit = response.data.data$("#fid").value=fruit.fid$("#fname").value=fruit.fname$("#price").value=fruit.price$("#fcount").value=fruit.fcount$("#remark").value=fruit.remark})}update=function(){let fid = $("#fid").valuelet fname = $("#fname").valuelet price = $("#price").valuelet fcount = $("#fcount").valuelet remark = $("#remark").valueaxios({method:'post',url:"update",data:{fid:fid,fname:fname,price:price,fcount:fcount,remark:remark}}).then(response=>{if(response.data.flag){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();String str = null;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;
import java.util.List;
public class FruitDaoImpl extends BaseDao<Fruit> implements FruitDao {@Overridepublic void addFruit(Fruit fruit) {String sql = "insert into t_fruit values (0,?,?,?,?)";super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark());}@Overridepublic void delFruit(String fname) {String sql = "delete from t_fruit where fname=?";super.executeUpdate(sql, fname);}//通过 fid 可以修改所有的属性值@Overridepublic 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());}@Overridepublic List<Fruit> getFruitList() {return super.executeQuery("select * from t_fruit");}@Overridepublic Fruit getFruitByFname(String fname) {return load("select * from t_fruit where fname = ?", fname);}@Overridepublic 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;
@WebServlet("/update")
public class UpdateServlet extends HttpServlet {private FruitDao fruitDao = new FruitDaoImpl();@Overrideprotected 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());}
}