action

来源:互联网 发布:sql server count 慢 编辑:程序博客网 时间:2024/05/21 08:53
package Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import Bean.Student;
import Service.StudentService;

public class StudentAction extends ActionSupport implements ModelDriven<Student>{
    
    private StudentService service;
    //set()接收jsp的form ModelDriven<Student>自动帮我们接收数据
    private Student stu=new Student();
    
    //set()接收参数
    //封装客户提交的一组产品ID
    private int[] checkboxid;
    

    public void setService(StudentService service) {
        this.service = service;
    }

    public String showAll(){
        List<Student> list=service.showAll();
        ActionContext.getContext().put("list", list);
        return "showAll";
    }
    public String cdAddPage(){
        return "enterAdd";
    }
    public String cdUpdatePage(){
        System.out.println(checkboxid[0]);
    stu=service.getStudentById(checkboxid[0]);  //    .put("id",checkboxid[0]);
        
        return "enterUpd";
    }
    public String add(){
         service.add(stu);
         return "flush";
        
    }
    public String deleteStudents() throws IOException{
        for(int i=0;i<checkboxid.length;i++){
        System.out.println(checkboxid[i]);
        }
         HttpServletResponse response= ServletActionContext.getResponse();
         response.setContentType("text/html;charset=UTF-8");
          PrintWriter out= response.getWriter();
          if(service.deleteStudentsById(checkboxid)){
              out.println("<script>alert('删除数据成功!');location.href='default.jsp';</script>");
          }else{
              out.println("<script>alert('删除数据失败!');location.href='default.jsp';</script>");
          }
          out.close();
        return null;
    }
    public String doUpdateStudent() throws IOException{
         HttpServletResponse response= ServletActionContext.getResponse();
         response.setContentType("text/html;charset=UTF-8");
         PrintWriter out=response.getWriter();
//         System.out.println(stu.toString());
         if(service.updateStudent(stu)){
              out.println("<script>alert('修改数据成功!');location.href='default.jsp';</script>");
         }else{
              out.println("<script>alert('修改数据失败!');location.href='default.jsp';</script>");
         }
         out.close();
        return null;
        
    }
    @Override
    public Student getModel() {
        // TODO Auto-generated method stub
        return stu;
    }

    public int[] getCheckboxid() {
        return checkboxid;
    }

    public void setCheckboxid(int[] checkboxid) {
        this.checkboxid = checkboxid;
    }

    
    
    
}

0 0