问题出现:
在跟随视频进行Thymeleaf练习时,出现服务器报错500:
解决过程:
发现是在我的UpdateServlet类中的doPost方法中出现了问题(可以在报错中,找到属于自己写的类中找到错误,按照代码行提示去更改)
错误代码如下:
@WebServlet("/update.do")
public class UpdateServlet extends ViewBaseServlet {
private FruitDAO fruitDAO = new FruitDAOImpl();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置编码
req.setCharacterEncoding("utf-8");
//2.获取参数
String fidStr = req.getParameter("fid");
int fid = Integer.parseInt(fidStr);
String fname = req.getParameter("fname");
String priceStr = req.getParameter("price");
int price = Integer.parseInt(priceStr);
String fcountStr = req.getParameter("fcount");
int fcount = Integer.parseInt(fcountStr);
String remark = req.getParameter("remark");
//3.执行更新
fruitDAO.updateFruit(new Fruit(fid,fname,price,fcount,remark));
//4.资源跳转
super.processTemplate("index",req,resp);
}
}
edit.html中的部分代码错误如下:
<p class="center f30">编辑库存信息</p>
<form th:action="@{update.do}" method="post">
<!-- 隐藏域:功能类似于文本框,他的值表单发送时会随着表单的发送,但界面上用户看不到 -->
<input type="hidden" name="fid" th:value="*{fid}"/>
<table id="tbl_fruit" th:object="${fruit}">
th标签的作用范围是在table范围内,放在table之外的位置无效,所以产生报错
可以将table标签中定义的th标签,移到form中去
更改正确代码如下:
<div id="div_fruit_list">
<p class="center f30">编辑库存信息</p>
<form th:action="@{update.do}" method="post" th:object="${fruit}">
<!-- 隐藏域:功能类似于文本框,他的值表单发送时会随着表单的发送,但界面上用户看不到 -->
<input type="hidden" name="fid" th:value="*{fid}"/>
<table id="tbl_fruit">