public void addUser() { PrintWriter out = null; try { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8"); String account = request.getParameter("account"); String name = request.getParameter("name"); String address = request.getParameter("address"); User user = new User(); user.setAccount(account); user.setAddress(address); user.setName(name); userService.add(user); out = response.getWriter(); out.write(new Gson().toJson("success")); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); if (out != null) out.write(new Gson().toJson("fail")); } finally { out.flush(); out.close(); } }
public void queryAllUser() { PrintWriter out = null; // logger.error("i"); try { HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8"); Gson gson = new Gson(); List<User> userList = userService.queryAllUser(); String gsonStr = gson.toJson(userList); out = response.getWriter(); out.write(gsonStr); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); if (out != null) out.write(new Gson().toJson("fail")); } finally { out.flush(); out.close(); } }