package com.use.action;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.junit.Test;
import com.alibaba.druid.pool.vendor.SybaseExceptionSorter;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.use.modal.PageBean;
import com.use.modal.Student;
import com.use.service.StudentService;
import com.use.service.impl.StudentServiceImpl;
public class StudentAction extends ActionSupport implements ModelDriven<Student>{
private List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
public List<Map<String, Object>> getList() {
return list;
}
public void setList(List<Map<String, Object>> list) {
this.list = list;
}
private StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
PageBean pageBean=new PageBean();
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
private int pageIndex=1;
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
//公共list
public List<Map<String, Object>> publist(){
list=studentService.allStudent();
return list;
}
//list
public String list(){
System.out.println("in action list");
publist();
return list.size()>0?"ListSuccess":ERROR;
}
//save
public String save(){
boolean config=studentService.addStudent(student);
publist();
return config?"editsuccess":ERROR;
}
//select
public String listbyname(){
System.out.println("in action listbyname");
publist();
return list.size()>0?"Listbyname":ERROR;
}
public String listbyname1(){
System.out.println("in action listbyname1");
System.out.println(student.getSsname());
list=studentService.selStudent(student.getSsname());
System.out.println(list.size());
return list.size()>0?"Listbyname":ERROR;
}
//edit
public String edit(){
System.out.println("in action edit");
publist();
return list.size()>0?"editsuccess":ERROR;
}
//modify
public String modify(){
return "modify";
}
public String modify1(){
System.out.println("in action modify1");
boolean config=studentService.modStudent(student);
publist();
return config? "editsuccess":ERROR;
}
//delete
public String delete(){
System.out.println("in action delete");
System.out.println(student.getSsno());
boolean config=studentService.detStudent(student.getSsno());
if (config) {
publist();
}
return config? "editsuccess":ERROR;
}
//listbypage
@Test
public String listbypage(){
System.out.println("in the action listbypage");
pageBean.setPageIndex(pageIndex);
List<Map<String, Object>> students=publist();
pageBean.setCountPrePage(5);//每页显示记录数
pageBean.setStudentCount(students.size());
HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session=request.getSession();
session.setAttribute("studentCount", students.size());
if(students.size()%pageBean.getCountPrePage()==0){//计算总页数
pageBean.setPageCount(students.size()/pageBean.getCountPrePage());
}else{
pageBean.setPageCount(students.size()/pageBean.getCountPrePage()+1);
}
System.out.println("pageindex:"+pageBean.getPageIndex());
if (pageBean.getPageIndex()==0) {
//startSelect=0;
pageBean.setStartSelect(0);
//System.out.println("1111111:"+startSelect);
}else{
pageBean.setStartSelect(pageBean.getPageIndex()*pageBean.getCountPrePage()-pageBean.getCountPrePage());
}
session.setAttribute("pageCount", pageBean.getPageCount());
session.setAttribute("currentPageIndex", pageBean.getPageIndex());
System.out.println("in the action count:"+pageBean.getCountPrePage());
System.out.println(pageBean.getStartSelect());
list=studentService.bypageStudent(pageBean.getCountPrePage(), pageBean.getStartSelect());
return list.size()>0?"listbypage":ERROR;
}
private Student student = new Student();
public Student getModel() {
// TODO Auto-generated method stub
return student;
}
}