package edu.cdio.action; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import edu.cdio.domain.Customer; import edu.cdio.service.CustomerService; import edu.cdio.util.PageBean; @Controller //用于注册spring容器中的adminAction public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{ /** * */ private static final long serialVersionUID = 1L; @Autowired //注入adminService接口 private CustomerService customerService; //封装数据 private Customer customer = new Customer(); public Customer getModel() { return this.customer; } private PageBean pageBean; private int pageNum =1; //表示网页中返回的当前的值,默认为1 //实现分页功能 public String page(){ HttpServletRequest request=ServletActionContext.getRequest(); String skipPage = request.getParameter("skipPage"); //跳转页面输入是否为空 if(skipPage!=null && !skipPage.equals("")){ pageNum = Integer.parseInt(request.getParameter("skipPage")); } this.pageBean = customerService.pageOfCustomer(5, pageNum); ActionContext context = ActionContext.getContext(); context.getSession().put("customerCount", pageBean.getCount()); context.getSession().put("totalPage4", pageBean.getTotalPage()); context.getSession().put("currentPage4", pageBean.getCurrentPage()); context.getSession().put("listCustomer", pageBean.getList()); return "page"; } //添加 public String add(){ ActionContext context = ActionContext.getContext(); context.getSession().remove("msg1"); //获取参数 HttpServletRequest request=ServletActionContext.getRequest(); String name = customer.getCustomername(); if (this.customerService.findOneCustomer(name).size()>0) { context.getSession().put("msg1", "用户名已存在!"); return "errorAdd"; }else if (name==null || name.equals("")) { context.getSession().put("msg1", "用户名不能为空!"); return "errorAdd"; }else{ String valuename = request.getParameter("valuename"); //执行方法 this.customerService.saveCustomer(customer); System.out.println("67"); //弹出提示 HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); try { PrintWriter out = response.getWriter(); out.println("<script type='text/javascript'>alert('添加成功!');" + "window.location.href='customer/customer_list.jsp';</script>"); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } return null; } } //跳转页面 public String skipAdd(){ ActionContext context = ActionContext.getContext(); context.getSession().remove("msg1"); context.getSession().remove("msg2"); return "skipAdd"; } //删除 public String delete(){ this.customerService.deleteCustomer(customer.getCustomerid()); return "delete"; } //查找一个用户信息 public String show(){ customer = this.customerService.findOneCustomer(customer.getCustomername()).get(0); ActionContext context = ActionContext.getContext(); context.getSession().put("customer", customer); return "show"; } //修改信息 public String update() throws IOException{ this.customerService.update(customer); return "update"; } //根据名字关键字搜索 public String select(){ //获取输入内容 HttpServletRequest request=ServletActionContext.getRequest(); String input = request.getParameter("search1"); String skipPage = request.getParameter("skipPage"); //跳转页面输入是否空 if(skipPage!=null && !skipPage.equals("")){ pageNum = Integer.parseInt(request.getParameter("skipPage")); } //执行方法 pageBean = this.customerService.pageOfName(5, pageNum, input); ActionContext context = ActionContext.getContext(); context.getSession().put("customerCount2", pageBean.getCount()); context.getSession().put("totalPage5", pageBean.getTotalPage()); context.getSession().put("currentPage5", pageBean.getCurrentPage()); context.getSession().put("listCustomer2", pageBean.getList()); return "select"; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public PageBean getPageBean() { return pageBean; } public void setPageBean(PageBean pageBean) { this.pageBean = pageBean; } }