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.Role; import edu.cdio.service.RoleService; import edu.cdio.util.PageBean; @Controller //用于注册spring容器中的roleAction public class RoleAction extends ActionSupport implements ModelDriven<Role>{ /** * */ private static final long serialVersionUID = 1L; @Autowired //注入roleService接口 private RoleService roleService; //封装数据 private Role role = new Role(); public Role getModel() { return this.role; } 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 = roleService.pageOfRole(5, pageNum); ActionContext context = ActionContext.getContext(); context.getSession().put("roleCount", pageBean.getCount()); context.getSession().put("totalPage3", pageBean.getTotalPage()); context.getSession().put("currentPage3", pageBean.getCurrentPage()); context.getSession().put("listRole", pageBean.getList()); return "page"; } //添加 public String add(){ ActionContext context = ActionContext.getContext(); context.getSession().remove("msg"); //获取参数 HttpServletRequest request=ServletActionContext.getRequest(); String name = role.getRolename(); if (this.roleService.findOneRole(name).size()>0) { context.getSession().put("msg", "用户名已存在!"); return "errorAdd"; } //执行方法 this.roleService.saveRole(role); //弹出提示 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='roleAction_page';</script>"); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } return null; } //跳转页面 public String skipAdd(){ ActionContext context = ActionContext.getContext(); context.getSession().remove("msg"); context.getSession().remove("msg1"); return "skipAdd"; } //删除 public String delete(){ this.roleService.deleteUser(role.getRoleid()); return "delete"; } //查找一个用户信息 public String show(){ role = this.roleService.findOneRole(role.getRolename()).get(0); ActionContext context = ActionContext.getContext(); context.getSession().put("role", role); return "show"; } //修改信息 public String update() throws IOException{ this.roleService.update(role); 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.roleService.pageOfName(5, pageNum, input); ActionContext context = ActionContext.getContext(); context.getSession().put("roleCount2", pageBean.getCount()); context.getSession().put("totalPage2", pageBean.getTotalPage()); context.getSession().put("currentPage2", pageBean.getCurrentPage()); context.getSession().put("listRole2", pageBean.getList()); return "select"; } }