package edu.cdio.action; import java.io.IOException; import java.io.PrintWriter; import java.sql.Date; import java.text.SimpleDateFormat; 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.Commorder; import edu.cdio.service.CommorderService; import edu.cdio.util.PageBean; @Controller //用于注册spring容器中的adminAction public class CommorderAction extends ActionSupport implements ModelDriven<Commorder>{ /** * */ private static final long serialVersionUID = 1L; @Autowired //注入adminService接口 private CommorderService commorderService; //封装数据 private Commorder commorder = new Commorder(); public Commorder getModel() { return this.commorder; } 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 = commorderService.pageOfOrder(5, pageNum); ActionContext context = ActionContext.getContext(); context.getSession().put("commorderCount", pageBean.getCount()); context.getSession().put("totalPage", pageBean.getTotalPage()); context.getSession().put("currentPage", pageBean.getCurrentPage()); context.getSession().put("listCommorder", pageBean.getList()); return "page"; } //添加 public String add(){ System.out.println("qwertyu"); ActionContext context = ActionContext.getContext(); context.getSession().remove("msg1"); //context.getSession().remove("msg2"); //获取参数 HttpServletRequest request=ServletActionContext.getRequest(); //String repwd = request.getParameter("repeatPassword"); String name = commorder.getOrdername(); //String pwd = admin.getAdminPassword(); System.out.println("qwertyu"); if (this.commorderService.findOneCommorder(name).size()>0) { context.getSession().put("msg1", "订单已存在!"); return "errorAdd"; }else if (name==null || name.equals("")) { context.getSession().put("msg1", "订单不能为空!"); return "errorAdd"; } // else if (pwd==null || pwd.equals("") || repwd==null || repwd.equals("")) { // context.getSession().put("msg2", "密码不能为空"); // return "errorAdd"; // }else if (!pwd.equals(repwd)) { // context.getSession().put("msg2", "两次密码输入不一致"); // return "errorAdd"; // }else{ // //头像是否为空 // if(file != null){ // this.upload(); // admin.setAdminHeader(this.getFileFileName()); // }else{ // //默认头像 // admin.setAdminHeader("1.jpg"); // } //设置时间 // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // String date = sdf.format(new Date(new java.util.Date().getTime())); // admin.setAddTime(date); //执行方法 this.commorderService.saveCommorder(commorder); //弹出提示 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='order/order.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"); context.getSession().remove("msg3"); return "skipAdd"; } //删除 public String delete(){ this.commorderService.deleteCommorder(commorder.getOrderid()); return "delete"; } //查找一个管理员信息 public String show(){ commorder = this.commorderService.findOneCommorder(commorder.getOrdername()).get(0); ActionContext context = ActionContext.getContext(); context.getSession().put("commorder", commorder); return "show"; } //修改信息 public String update() throws IOException{ this.commorderService.update(commorder); return "update"; } //根据名字关键字搜索 public String select(){ //获取输入内容 HttpServletRequest request=ServletActionContext.getRequest(); String input = request.getParameter("search"); String skipPage = request.getParameter("skipPage"); //跳转页面输入是否空 if(skipPage!=null && !skipPage.equals("")){ pageNum = Integer.parseInt(request.getParameter("skipPage")); } //执行方法 pageBean = this.commorderService.pageOfName(5, pageNum, input); ActionContext context = ActionContext.getContext(); context.getSession().put("commorderCount2", pageBean.getCount()); context.getSession().put("totalPage2", pageBean.getTotalPage()); context.getSession().put("currentPage2", pageBean.getCurrentPage()); context.getSession().put("listCommorder2", 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; } }