更新菜单与更新桌号步骤完全一样,需要注意的是菜单名称含有中文,所以在servlet中将信息以xml形式发出时,应注意将其编码设定为gbk,如下:
<?xml version='1.0' encoding='gbk'?>
新建Menu数据实体类:
public class Menu { private int id; private int price; private int typeId; private String name; private String pic; private String remark; getters & setters .. }
在UpdateDao与UpdateDaoImpl中添加新方法getMenuList()获取菜单列表:
public interface UpdateDao { // 获得餐桌列表 public List
@Override public List
然后是专门为更新菜单服务的UpdateMenuServlet:
public class UpdateMenuServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter pw = new PrintWriter(resp.getOutputStream()); // 获取所有菜单信息列表 UpdateDao updateDao = new UpdateDaoImpl(); ListmenuList = updateDao.getMenuList(); // 将所有信息以xml格式输出, 此处设定编码为gbk, 否则中文乱码 pw.println(" "); pw.println(" "); for (int i = 0; i < menuList.size(); i++) { Menu menu = menuList.get(i); pw.println(" "); pw.flush(); pw.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }}"); pw.print(" "); } pw.println(""); pw.print(menu.getId()); pw.print(" "); pw.print(""); pw.print(menu.getPrice()); pw.print(" "); pw.print(""); pw.print(menu.getTypeId()); pw.print(" "); pw.print(""); pw.print(menu.getName()); pw.print(" "); pw.print(""); pw.print(menu.getPic()); pw.print(" "); pw.print(""); pw.print(menu.getRemark()); pw.print(" "); pw.println("
注册servlet:
UpdateMenuServlet com.moka.servlet.UpdateMenuServlet UpdateMenuServlet /servlet/UpdateMenuServlet