6月20日 SSH 周三

package com.action;

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

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.dto.Student;
import com.opensymphony.xwork2.ActionSupport;
import com.service.StudentService;

public class Action extends ActionSupport{
    private StudentService service;
    private List<Student> studentList;
    private Student student;

    public String list() throws Exception {
        // TODO Auto-generated method stub
        studentList = service.getStudentList();
        return "list";
    }
    public void getDownList() throws IOException{
        List downList = service.getDownList();
        JSONArray jsonObject = JSONArray.fromObject(downList);
        System.out.println(jsonObject);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.getWriter().print(jsonObject);
    }
    public void getObj() throws IOException{
        student = service.getObj(student);
        System.out.println(student);
        JSONObject object = JSONObject.fromObject(student);
        System.out.println(object);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(object);
    }
    public void saveOrUpdate() throws IOException{
        int i = service.saveOrUpdate(student);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.getWriter().print(i);
    }

    public StudentService getService() {
        return service;
    }

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

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }


}
package com.dao;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dto.Student;

public class StudentDao extends HibernateDaoSupport{

    public List<Student> getStudentList() {
        return getHibernateTemplate().find("from Student");
    }

    public List getDownList() {
        return getHibernateTemplate().find("from Clazz");
    }

    public Student getObj(Student student) {
        // TODO Auto-generated method stub
        return (Student) getHibernateTemplate().get(Student.class, student.getId());
    }

    public int saveOrUpdate(Student student) {
        // TODO Auto-generated method stub
        try {
            getHibernateTemplate().saveOrUpdate(student);
            return 1;
        } catch (DataAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }

}

愿与最重要之人能再次相会。
——艾拉《可塑性记忆》

猜你喜欢

转载自blog.csdn.net/helloworld_1996/article/details/80753709