thymeleaf增删查改操作
-
显示数据库所有数据
-
html中,从session作用域中提取数据,再用each迭代
-
<tr th:if="${#lists.isEmpty(session.fruitList)}"> <td colspan="4">对不起库存为空!</td> </tr> <tr th:unless="${#lists.isEmpty(session.fruitList)}" th:each="fruit : ${session.fruitList}"> <!-- 从session会话作用域里提取数据,each为迭代--> <td ><a th:text="${fruit.fname}" th:href="@{/edit(fid=${fruit.fid})}"> </a> </td> <!--用a标签链接到另外的页面,并把fid传过去--> <td th:text="${fruit.price}"> </td> <td th:text="${fruit.fcount}"> </td> </tr>
-
具体
Servlet
中 -
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session=req.getSession(); //保存到session作用域,使index页面可以获取 FruitDAO fruitDAO=new FruiteDAOImpl(); List<Fruit> fruitList= fruitDAO.getFruitList(); //从数据库中获取数据 session.setAttribute("fruitList",fruitList); //利用session传递fruitList super.processTemplate("index",req,resp); //相当于request.getRequestDispatcher("index.html").forward(request,response); 服务器内部转发 **}
-
-
添加数据**
-
idex.html页面跳转到add.html
-
<a th:href="@{/add.html}">添加新库存记录</a>
-
add.html中使用form表单提交数据
-
<form action="add" method="post" > <p class="center f30"> 编辑库存信息</p> <table id="tbl_fruit" > <!--频繁写fruit.的时候可以用object属性--> <tr> <th class="w20">名称:</th> <th><input type="text" name="fname" /> ></th> <!-- <th><input type="text" name="fname" th:value="${fruit.fname}"/> ></th> --> <!--获取request请求作用域--> </tr> <tr> <th class="w20">单价</th> <th><input type="text" name="price" /></th> </tr> <tr> <th class="w20">库存</th> <th><input type="text" name="fcount" /></th> </tr> <tr> <th class="w20">备注</th> <th><input type="text" name="remark" /></th> </tr> <tr> <th colspan="4" ><input type="submit" name="submit" value="添加"></th> </tr> </table> </form>
-
AddServlet中
-
@WebServlet("/add") //注解,不用再在web.xml中绑定 public class AddServlet extends ViewBaseServlet { FruitDAO fruitDAO=new FruiteDAOImpl(); @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.dopost得设置编码格式 req.setCharacterEncoding("UTF-8"); //2.获取form表单数据 String fname=req.getParameter("fname"); Integer price=Integer.parseInt(req.getParameter("price")); Integer fcount=Integer.parseInt(req.getParameter("fcount")); String remark=req.getParameter("remark"); Fruit fruit=new Fruit(0,fname,price,fcount,remark); fruitDAO.addFruit(fruit); resp.sendRedirect("index"); //客户端重定向 } }
-
-
修改数据
-
index.html跳转中将id传过去作为查询修改依据
-
<td ><a th:text="${fruit.fname}" th:href="@{/edit(fid=${fruit.fid})}">苹果</a> </td>
-
edit.html中使用form表单修改
-
<div id="div_fruit_list"> <form th:action="@{/update}" method="post" th:object="${fruit}"> <!--频繁写fruit.的时候可以用object属性--> <p class="center f30"> 编辑库存信息</p> <input type="hidden" name="fid" th:value="*{fid}"/> <!--hidden隐藏域:功能类似于文本框,它的值会随着表单发送给服务器,但界面上看不到--> <table id="tbl_fruit" > <tr> <th class="w20">名称:</th> <th><input type="text" name="fname" th:value="*{fname}"/> ></th> <!-- <th><input type="text" name="fname" th:value="${fruit.fname}"/> ></th> 不使用object的方法 --> <!--获取request请求作用域--> </tr> <tr> <th class="w20">单价</th> <th><input type="text" name="price" th:value="*{price}"/></th> </tr> <tr> <th class="w20">库存</th> <th><input type="text" name="fcount" th:value="*{fcount}"/ ></th> </tr> <tr> <th class="w20">备注</th> <th><input type="text" name="remark" th:value="${fruit.remark}"/></th> </tr> <tr> <th colspan="4" ><input type="submit" name="submit" value="修改"></th> </tr> </table> </form> </div>
-
EditServlet类
-
@WebServlet("/update") //注解 public class UpdateServlet extends ViewBaseServlet { private FruitDAO fruitDAO= new FruiteDAOImpl(); @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.设置编码 req.setCharacterEncoding("UTF-8"); //2.获取form表单数据 Integer fid=Integer.parseInt(req.getParameter("fid")); //此处获取的fid为index传过来的,作为修改依据 String fname=req.getParameter("fname"); Integer price=Integer.parseInt(req.getParameter("price")); Integer fcount=Integer.parseInt(req.getParameter("fcount")); String remark=req.getParameter("remark"); //3.执行更新 fruitDAO.updateFruit(new Fruit(fid,fname,price,fcount,remark)); //4.资源跳转 resp.sendRedirect("index"); //此处需要重定向,目的是重新给indexServlet发请求, // 重新获取fruitList,然后覆盖到session中,这样index.html页面上现实的session中的数据才是最新的 } }
-
-
删除数据
-
index.html中使用js跳转到DelServlet进行删除
-
<td><img src="imgs/del.jpg" class="delImg" th:onclick="|delFruit(${fruit.fid})|"/></td>
-
index.js函数,把fid传递过去
-
function delFruit(fid) { if (confirm("是否确认删除?")){ //弹窗 window.location.href='del.do?fid='+fid; //链接 } }
-
DelServlet具体
-
@WebServlet("/del.do") public class DelServlet extends ViewBaseServlet { private FruitDAO fruitDAO=new FruiteDAOImpl(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String fid_str=req.getParameter("fid"); //获取get作用域的fid if(StringUtil.isNotEmpty(fid_str)){ int fid=Integer.parseInt(fid_str); fruitDAO.delFruit(fid); //删除 resp.sendRedirect("index"); //客户端重定向刷新数据 } } }
-
-
模糊查询
-
index.html使用form表单查询
-
<form th:action="@{/index}" method="post"> <input type="hidden" name="operate" value="search"/> <!--将隐藏域传递过去作为IndexServlet判断依据来更新数据--> 请输入查询关键词:<input type="text" name="keyword"/> <input type="submit" value="查询" class="btn"/> </form>
-
indexServlet处理查询的dopost大体跟doget方法类似,所以直接调用doget方法
-
@WebServlet("/index") public class index_servlet extends ViewBaseServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); HttpSession session=req.getSession(); String oper=req.getParameter("operate"); //如果oper不是空的话,就是通过form调用doPost方法来的 // 否则就是get方法 String keyword=null; Integer pageNum=1; if (StringUtil.isNotEmpty(oper)&&"search".equals(oper)){ //说明是点击表单查询发送过来的 //keyword从参数中获取 keyword=req.getParameter("keyword"); if(StringUtil.isEmpty(keyword)){ keyword=""; } session.setAttribute("keyword",keyword); //保存到session作用域,使index页面可以获取 }else{ // 说明不是点击表单查询发送过来的 Object keywordObj=session.getAttribute("keyword"); if(keywordObj!=null){ keyword=(String)keywordObj; }else{ keyword=""; } } FruitDAO fruitDAO=new FruiteDAOImpl(); List<Fruit> fruitList= fruitDAO.getFruitList(); session.setAttribute("fruitList",fruitList); super.processTemplate("index",req,resp); } }
-