首页 > 其他分享 >Servlet12 - 实现编辑和修改

Servlet12 - 实现编辑和修改

时间:2023-01-18 15:25:07浏览次数:40  
标签:do 编辑 edit request value 修改 fruit fid Servlet12

1. 点击水果名称跳转到编辑相应水果信息的页面

  • 主页中设置跳转超链接

    • <td><a th:text="${fruit.fname}" th:href="@{/edit.do(fid=${fruit.fid})}">苹果</a></td>
      
    • 跳转到 /edit.do 同时传入相应水果的fid -- key = value ( fid = fruit.fid )

  • EditServlet 继承 ViewBaseServlet 实现 edit.do 功能

    • a 标签中超链接跳转默认为 get 请求 -- EditServlet 中重写 doGet 方法

    • 传入的 key = value 中 key 和 value 都是 String 格式,调用 request.getParameter("fid") 得到 value,再将 value 转为 int 型,通过 fruitDAO 中的 .getFruitByFid(fid) 方法,从数据库中获取 fid 对应的 Fruit 对象

    • 将获取到的 Fruit 对象保存到 request 请求域中,当次请求的其他地方就可以从请求域中得到 Fruit 对象和对象的各种属性

      • <p th:text="${helloRequestAttr}">request field value</p>
        
      • <p th:text="${session.helloSessionAttr}">这里显示会话域数据</p>
        
      • <p th:text="${application.helloAppAttr}">这里显示应用域数据</p>
        
    • 最后调用父类的 super.processTemplate( templateName, request, response ) 方法渲染模板,templateName = "edit" ,在页面中显示 /edit.html

    • public void doGet(HttpServletRequest request , HttpServletResponse response)throws IOException, ServletException {
              String fidStr = request.getParameter("fid");
              if(StringUtil.isNotEmpty(fidStr)){
                  int fid = Integer.parseInt(fidStr);
                  Fruit fruit = fruitDAO.getFruitByFid(fid);
                  request.setAttribute("fruit",fruit);
                  super.processTemplate("edit",request,response);
              }
          }
      
  • 在 edit.html 页面中,数据直接从请求域中读取,只要设置样式就行

    • 每一次读取同一 Object 的属性值都需要 Object.Attribute

    • <input type="text" th:value="${fruit.price}"/>
      
    • 在外部标签中声明 th:object = "${Object}",则内部标签读取对象的属性时就无需再声明是哪个对象的属性,直接通过属性名读取就行 th:value = "*{Attribute}"

    • <form ... th:object="${fruit}">
          ...
          <input type="text" th:value="*{price}"/>
          ...
      </form>
          
      
    • fid 之类的数据不显示在页面中,但需要提交给后台服务端,使用隐藏域即可

    • <!-- 隐藏域 : 功能类似于文本框 , 它的值会随着表单的发送也会发送给服务器,但是界面上用户看不到 -->
      <input type="hidden" name="fid" th:value="*{fid}"/>
      
    • form 表单使用 th:action = "@{/update.do}" 提交数据,就不用考虑路径深度了,统一从根目录开始

    • 将表单数据提交到 update.do 后,edit.do 就结束了,后面再实现 update.do 即可


2. 将表单提交的数据写入数据库

  • UpdateServlet 继承 ViewBaseServlet 实现 update.do 功能

    • 表单提交数据使用 post 方法,在 UpdateServlet 中实现父类的 doPost 方法,获取传入的所有属性值,然后调用 fruitDAO 类中的 .updateFruit() 方法,传入 Fruit 对象

    • fruitDAOImpl 类 -- 继承父类 BaseDAO + 实现接口类 fruitDAO

    • 实现接口类 fruitDAO 中声明的 .updateFruit() 方法, 其中调用父类 BaseDAO 中封装好的 .executeUpdate() 方法,只需传入SQL语句结构和参数就能完成数据库相关操作

    • public class FruitDAOImpl extends BaseDAO<Fruit> implements FruitDAO {
      	@Override
          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());
              )
          }
      }
      
    • UpdateServlet 结束后需要从 edit 页面 跳转会 index 主页 --> 使用客户端重定向,让客户端自动重新访问 index 主页

    • //4.资源跳转 -- 不能使用服务器内部转发
      
      //super.processTemplate("index",request,response);
      // = request.getRequestDispatcher("index.html").forward(request,response);
      
      //.processTemplate() 重新渲染主页,因为没有重新建立会话,session中的数据未更新
      //此处需要重定向,目的是重新给IndexServlet发请求,重新从数据库中获取furitList,然后覆盖到session中,这样index.html页面上显示的session中的数据才是最新的
      response.sendRedirect("index");
      

标签:do,编辑,edit,request,value,修改,fruit,fid,Servlet12
From: https://www.cnblogs.com/Ashen-/p/17059872.html

相关文章