日期:2014-05-16  浏览次数:20382 次

Struts2+DAO+ExtJS 框架征程才开始_step1

?? 最近开始着手struts2的学习,版本是struts-2.0.6,倒腾了一个多月了,零零散散的,想法一直在改变,起先就是想做个普通的信息管理系统,用struts2+JSP,接着发现web页面太丑,我不会界面操作,对于html语言也只是停留在简单的能看,想起来以前接触过ExtJs,哎呀 ,现成的界面做好调用就可以岂不快哉,说干就干下了个ext-2.0.2., 一晚上加一上午加一下午终于调通了,暂时可以把数据利用DAO的方式简单的从mysql5.1里提取然后显示在Ext的表格(grid)上了==! 似不似很丢人

效果图如下:

?好吧,我想你看出来了我是个小菜鸟,原谅我吧 ,我有大牛的梦想的啦,有兴趣的话看看我接下来的具体实施吧,其实还是很纠结的

开发工具 MyEclipse5.5.1,部署如下:

?

DAO模式 连接数据库

package DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

// 连接数据库,配置数据库
public class DAOBase implements DAO {
    public Connection getConnection()  {  
		String connectURL = "jdbc:mysql://localhost/test";
		String user = "root";
		String password = "123";
		try{
		Class.forName("com.mysql.jdbc.Driver");// 加载驱动程序
		Connection connection = DriverManager.getConnection(connectURL, user, password);

        return connection;
		}catch (SQLException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();

		}
		return null;
			
    }
   
}

?struts2作为图文表现层,使模型和动作分开,Employee.java模型类

package entity;

public class Employee {
    private int id;
    private String firstName;
    private String lastName;

    public Employee() {
    }

    public Employee(int id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    
    //转化为JSON格式
    public String toString() {
        return "{id:" + id +
        	",firstName:'" + firstName +
            "',lastName:'" + lastName +
            "'}";
    }
}

?EmployeeActioin.java动作类

package entity;
import entity.Employee;
import entity.EmployeeManager;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import java.util.List;
import com.opensymphony.xwork2.ModelDriven;

// 使用ModelDriven拦截器
public class EmployeeAction extends ActionSupport implements Preparable,ModelDriven{
	private static final long serialVersionUID = 1L;

	private Employee employee;

	private int employeeId;

	private List<Employee> employees;
	

//ModelDriven拦截器自动调用的方法
	public Object getModel(){
		return employee;
	}
//Prepable拦截器自动调用的方法	
    public void prepare() throws Exception {
        if (employeeId == 0) {
            employee = new Employee();
        } else {
            employee = EmployeeManager.find(employeeId);
        }
    }
	
//	set、get方法
	public int getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}

	public List<Employee> getEmployees() {
		return employees;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}

//方法
	public String list() {
		employees = EmployeeManager.getEmployees();
		return SUCCESS;
	}

	public String create() {
		EmployeeManager.create(employee);
		return SUCCESS;
	}

	public String edit() {
		return SUCCESS;
	}

	public String update() {
		EmployeeManager.update(employee);
		employees = EmployeeManager.getEmployees();
		return SUCCESS;
	}

	public String delete() {
		EmployeeManager.delete(employeeId);
		employees = EmployeeManager.getEmpl