package com.yh.controller;
import com.yh.model.Customer;
import com.yh.service.CustomerService;
import com.yh.service.impl.CustomerServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
@WebServlet("/UpdateCustomerServlet")
public class UpdateCustomerServlet extends ViewBaseServlet{
CustomerService customerService=new CustomerServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Customer customer=new Customer();
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取请求参数
Map<String,String[]> map=request.getParameterMap();
try {
//把参数封装成对象
BeanUtils.populate(customer, map);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
boolean b = customerService.updateService(customer);
if (b){
//重定向
response.sendRedirect(request.getContextPath()+"/QueryPageCustomerServlet");
}else{
request.setAttribute("msg","邮箱已被占用!!!");
request.setAttribute("customer",customer);
super.processTemplate("update",request,response);
}
}
}