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.Care;
import edu.cdio.service.CareService;
import edu.cdio.service.UserService;
import edu.cdio.util.PageBean;
@Controller //用于注册spring容器中的careAction
public class CareAction extends ActionSupport implements ModelDriven<Care> {
/**
*
*/
private static final long serialVersionUID = 1L;
@Autowired //注入adminService接口
private CareService careService;
//封装数据
private Care care = new Care();
public Care getModel() {
return this.care;
}
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 = careService.pageOfCare(5, pageNum);
ActionContext context = ActionContext.getContext();
context.getSession().put("careCount", pageBean.getCount());
context.getSession().put("totalPage", pageBean.getTotalPage());
context.getSession().put("currentPage", pageBean.getCurrentPage());
context.getSession().put("listCare", pageBean.getList());
return "page";
}
//添加
public String add(){
ActionContext context = ActionContext.getContext();
context.getSession().remove("msg1");
//context.getSession().remove("msg2");
//获取参数
HttpServletRequest request=ServletActionContext.getRequest();
//String repwd = request.getParameter("repeatPassword");
String name = care.getTheme();
//String pwd = user.getUserpassword();
System.out.println("d");
if (this.careService.findOneCare(name).size()>0) {
context.getSession().put("msg1", "主题已存在!");
return "errorAdd";
}else if (name==null || name.equals("")) {
context.getSession().put("msg1", "主题不能为空!");
return "errorAdd";
}
// else if (!pwd.equals(repwd)) {
// context.getSession().put("msg2", "两次密码输入不一致");
// return "errorAdd";
// }
//设置时间
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//String date = sdf.format(new Date(new java.util.Date().getTime()));
// user.setAddTime(date);
//执行方法
this.careService.saveCare(care);
//弹出提示
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
try {
PrintWriter out = response.getWriter();
System.out.println("wss");
out.println("<script type='text/javascript'>alert('添加成功!');"
+ "window.location.href='customer/customer_care.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.careService.deleteCare(care.getCareid());
return "delete";
}
//查找一个用户信息
public String show(){
care = this.careService.findOneCare(care.getTheme()).get(0);
ActionContext context = ActionContext.getContext();
context.getSession().put("care", care);
return "show";
}
//修改信息
public String update() throws IOException{
this.careService.update(care);
return "update";
}
//根据名字关键字搜索
public String select(){
//获取输入内容
HttpServletRequest request=ServletActionContext.getRequest();
String input = request.getParameter("search3");
String skipPage = request.getParameter("skipPage");
//跳转页面输入是否空
if(skipPage!=null && !skipPage.equals("")){
pageNum = Integer.parseInt(request.getParameter("skipPage"));
}
//执行方法
pageBean = this.careService.pageOfName(5, pageNum, input);
ActionContext context = ActionContext.getContext();
context.getSession().put("careCount2", pageBean.getCount());
context.getSession().put("totalPage7", pageBean.getTotalPage());
context.getSession().put("currentPage7", pageBean.getCurrentPage());
context.getSession().put("listCare2", pageBean.getList());
return "select";
}
}