|
@@ -0,0 +1,67 @@
|
|
|
+package com.lovecoding.study.servlet;
|
|
|
+
|
|
|
+import com.lovecoding.study.domian.Brand;
|
|
|
+import com.lovecoding.study.mapper.BrandMapper;
|
|
|
+import com.lovecoding.study.utils.MybatisUtils;
|
|
|
+import org.apache.ibatis.session.SqlSession;
|
|
|
+
|
|
|
+import javax.servlet.ServletException;
|
|
|
+import javax.servlet.annotation.WebServlet;
|
|
|
+import javax.servlet.http.HttpServlet;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@WebServlet("/updateBrand")
|
|
|
+public class UpdateBrandServlet extends HttpServlet {
|
|
|
+
|
|
|
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
+ //品牌ID
|
|
|
+ String id = req.getParameter("id");
|
|
|
+ if ( id != null && !id.equals("") ) {
|
|
|
+ //获得mybatis session 对象
|
|
|
+ SqlSession sqlSession = MybatisUtils.getSession().openSession();
|
|
|
+ //获得 mapper接口对象
|
|
|
+ BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
|
|
|
+ Brand brand = mapper.selecBrand( Integer.valueOf(id) );
|
|
|
+ req.setAttribute( "brand", brand );
|
|
|
+ req.getRequestDispatcher("/update.jsp").forward(req, resp);
|
|
|
+ } else {
|
|
|
+ resp.sendRedirect(req.getContextPath() + "/brand.jsp");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
+ req.setCharacterEncoding("UTF-8");
|
|
|
+ String brandName = req.getParameter("brandName");
|
|
|
+ String companyName = req.getParameter("companyName");
|
|
|
+ String ordered = req.getParameter("ordered");
|
|
|
+ String description = req.getParameter("description");
|
|
|
+ String status = req.getParameter("status");
|
|
|
+ String id = req.getParameter("id");
|
|
|
+
|
|
|
+ if ( brandName != null && !brandName.equals("") &&
|
|
|
+ companyName != null && !companyName.equals("") &&
|
|
|
+ ordered != null && !ordered.equals("") &&
|
|
|
+ description != null && !description.equals("") &&
|
|
|
+ id != null && !id.equals("") &&
|
|
|
+ status != null && !status.equals("")) {
|
|
|
+ SqlSession sqlSession = MybatisUtils.getSession().openSession();
|
|
|
+ BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
|
|
|
+ Brand brand = new Brand();
|
|
|
+ brand.setId(Integer.valueOf(id));
|
|
|
+ brand.setBrandName(brandName);
|
|
|
+ brand.setCompanyName(companyName);
|
|
|
+ brand.setDescription(description);
|
|
|
+ brand.setOrdered(Integer.valueOf(ordered));
|
|
|
+ brand.setStatus(Integer.valueOf(status));
|
|
|
+ mapper.updateBrand( brand );
|
|
|
+ sqlSession.commit();
|
|
|
+ resp.sendRedirect(req.getContextPath()+"/brands");
|
|
|
+ } else {
|
|
|
+ resp.sendRedirect(req.getContextPath()+"/updateBrand.jsp?id=" + id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|