修改的2种方式

hi, / 2023-08-21 / 原文

方式一:html -> servlet ->jsp->servlet->html

html:

<a  :href="'http://localhost:8080/Servlet-Vue-ElementUI/BrandController?method=selectId&id='+brand.id">修改</a> 

 servlet

 public void selectId(HttpServletRequest request, HttpServletResponse response) throws Exception {

HttpSession session=request.getSession();
//1.接收id的值
    String id=request.getParameter("id");
    //2.调用srvice层的查询方法(根据id查询)
    Brand brand= brandService.selectById(Integer.parseInt(id));
    //3.判断brand是否为null
    if(brand!=null){
    //4.存到session作用域里
    session.setAttribute("brand", brand);
    //5.转发到修改页面上(updateBrand.jsp)
    request.getRequestDispatcher("/updateBrand.jsp").forward(request, response);
    }else{
    //还在主界面   
    }
}
jsp
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
   <h3>修改</h3>
      <form action="/Servlet-Vue-ElementUI/BrandController?method=updateBrand" method="post">
       <input type="hidden" name="id" value="${brand.id }"/>
         品牌名称:<input type="text" name="brandName" value="${brand.brandName}"/> <br />
         企业名称:<input type="text" name="companyName" value="${brand.companyName}"/> <br />
         排序:<input type="text" name="ordered" value="${brand.ordered}"/> <br />
         表述信息:<input type="text" name="description" value="${brand.description}"/> <br />     
         状态:
      <c:if test="${brand.status==0 }">
        <input type="radio" name="status" value="0" checked/> 禁用
        <input type="radio" name="status" value="1"/> 启用<br>
      </c:if> 
      <c:if test="${brand.status==1 }">
        <input type="radio" name="status" value="0" /> 禁用
        <input type="radio" name="status" value="1" checked/> 启用<br>
      </c:if>  
        <input type="submit" value="修改"/>
        <input type="reset" />
      </form>
</body>
</html>
 
servlet
public void updateBrand(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收前端页面发送来的数据,并封装在Brand对象中
Brand b=new Brand();
    Map<String,String[]> map=request.getParameterMap();
    BeanUtils.populate(b, map);
    //2.调用修改方法
    int i=brandService.updateBrand(b);
    //3.判断i>0
    if(i>0){
    //修改成功
    response.sendRedirect(request.getContextPath()+"/brand.html");
    }else{
    //修改失败
    response.sendRedirect(request.getContextPath()+"/updateBrand.jsp");
    }
}