/**
* 查询所有品牌信息
* @return
*/
@Select("select * from tb_brand")
List<Brand> selectAll();
/**
* 查询所有品牌信息
* @return
*/
public List<Brand> selectAll(){
SqlSession sqlSession = factory.openSession();
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
List<Brand> list = brandMapper.selectAll();
return list;
}
private BrandService brandService=new BrandService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Brand> list = brandService.selectAll();
request.setAttribute("brands",list);
request.getRequestDispatcher("/brand.jsp").forward(request,response);
}
<c:forEach items="${brands}" var="brand" varStatus="vs">
<tr align="center">
<td>${vs.count}td>
<td>${brand.brandName}td>
<td>${brand.companyName}td>
<td>${brand.ordered}td>
<td>${brand.description}td>
<td>${brand.status==1?"启用":"禁用"}td>
<td><a href="/day10-brand-demo/selectByIdServlet?id=${brand.id}">修改a> <a href="/day10-brand-demo/DeleteServlet?id=${brand.id}">删除a>td>
tr>
c:forEach>
<input type="button" value="新增" onclick="toAdd()"><br>
<script>
/*上面需要添加个 点击事件 */
function toAdd() {
/*跳转到添加页面*/
location.href="/day10-brand-demo/addBrand.html"
}
script>
/* 跳转到conrtroller层*/
<form action="/day10-brand-demo/addServlet" method="post">
品牌名称:<input name="brandName"><br>
企业名称:<input name="companyName"><br>
排序:<input name="ordered"><br>
描述信息:<textarea rows="5" cols="20" name="description">textarea><br>
状态:
<input type="radio" name="status" value="0">禁用
<input type="radio" name="status" value="1">启用<br>
<input type="submit" value="提交">
form>
private BrandService brandService =new BrandService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求中文乱码问题
request.setCharacterEncoding("utf-8");
//2.获取请求参数 封装成brand对象
Brand brand = new Brand();
try {
BeanUtils.populate(brand, request.getParameterMap());
} catch (Exception e) {
e.printStackTrace();
}
//3.调用services 的 add方法
brandService.add(brand);
//4.重定向到selectAll这个类.重新查询一下所有,刷新列表
response.sendRedirect(request.getContextPath()+"/selectAllServlet");
}
1 点击跳转到修改页面
<c:forEach items="${brands}" var="brand" varStatus="vs">
<tr align="center">
<td>${vs.count}td>
<td>${brand.brandName}td>
<td>${brand.companyName}td>
<td>${brand.ordered}td>
<td>${brand.description}td>
<td>${brand.status==1?"启用":"禁用"}td>
<td><a href="/day10-brand-demo/selectByIdServlet?id=${brand.id}">修改a> <a href="/day10-brand-demo/DeleteServlet?id=${brand.id}">删除a>td>
tr>
c:forEach>
2 修改页面跳转到前端
<form action="/day10-brand-demo/updateServlet" method="post">
<%--id 隐藏域 加上el 的动态获取数据--%>
<input type="hidden" name="id" value="${brand.id}">
品牌名称:<input name="brandName" value="${brand.brandName}"><br>
企业名称:<input name="companyName" value="${brand.companyName}"><br>
排序:<input name="ordered" value="${brand.ordered}" ><br>
描述信息:<textarea rows="5" cols="20" name="description">
${brand.description}
textarea><br>
状态:
禁用
启用<br>
<input type="submit" value="提交">
form>
3.根据id查询 请求转发到更新页面
private BrandService brandService=new BrandService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Brand brand = brandService.selectById(id );
request.setAttribute("brand",brand);
request.getRequestDispatcher("/update.jsp").forward(request,response);
}
4 更新完成 重定向到 查询所有方法 展示所有数据
private BrandService brandService=new BrandService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//2.获取请求参数 封装成brand对象
Brand brand = new Brand();
try {
BeanUtils.populate(brand, request.getParameterMap());
} catch (Exception e) {
e.printStackTrace();
}
//3.调用services 的 add方法
brandService.update(brand);
//4.请求转发到selectAll这个类.重新查询一下所有,刷新列表
request.getRequestDispatcher("/selectAllServlet").forward(request,response);
}