今天讲解的ecside组件,这个组件听李老师介绍说是有了些改进,不过作为这些组件我觉得搞的头还是挺大的,但其实代码不多,只是需要根据一些文档进行调试就行了,我感觉这些东西整体来讲跟之前学习的并没有多大的不同。
今天所讲的知识点
A ECSide概述
B ECSide实现列表功能
我对知识点的分析
A
ECSide概述
在EC框架基础上出现的,EC是Java SE 的框架,用来完成Swing界面,ECSide在EC
框架的基础上,取出其中比较有用的一些核心代码,完成ECSide框架。
ECSide是Java
EE的框架,使用了AJAX技术,完成信息的显示,及添加修改删除等功能,并可以支持数据导出。
ExtJS也是EC框架发展出来的,可以在页面上完成所有EC之前的功能,但现在已经开始收费。
B ECSide实现列表功能
一、建立数据库
sql脚本
/*==============================================================*/
/*
DBMS name: MySQL 5.0 */
/* Created
on: 2009-5-13 16:04:15 */
/*==============================================================*/
drop database ecside;
create database ecside;
use
ecside;
drop table if exists employee;
drop table if exists
role;
/*==============================================================*/
/* Table: employee */
/*==============================================================*/
create table employee
(
username varchar(30) not
null,
password varchar(32) not null,
name varchar(30) not null,
tel
varchar(20) not null,
roleid int not null,
rolename varchar(50) not null,
status int not
null,
isLock int not null,
primary key (username)
);
/*==============================================================*/
/* Table: role */
/*==============================================================*/
create table role
(
id int not null
auto_increment,
name varchar(50) not null,
description text not null,
primary key (id)
);
insert into role (name,description)
values('系统管理员','最高权限用户,系统的超级用户。不参与具体的技术服务');
insert into role
(name,description) values('业务员','公司业务接收、跟踪人员,可分配任务给技术主管及技术员。不参与具体的技术服务');
insert into role (name,description)
values('技术主管','负责管理技术人员,可分配具体任务给技术人员,在任务较多的情况下,也可以当成一个技术人员负责具体任务的服务');
insert into role (name,description)
values('技术员','公司技术服务人员,负责具体任务的服务工作,如安装操作系统,维修硬件设备');
insert into
employee
values('admin','21232F297A57A5A743894A0E4A801FC3','张三','13661234567',1,'系统管理员',1,1);
insert into employee
values('testaa','21232F297A57A5A743894A0E4A801FC3','李四','13661234567',2,'业务员',1,1);
insert into employee
values('testbb','21232F297A57A5A743894A0E4A801FC3','王五','13661234567',3,'技术主管',1,1);
insert into employee
values('testcc','21232F297A57A5A743894A0E4A801FC3','小六','13661234567',4,'技术员',1,1);
二、建立项目并加入相应的支持
(1)引入Struts支持(并非必须,只是现在刚刚学习了Struts)
(2)
为项目加入ECSide支持
(3)加入支持jar包,将所有jar包和字体支持文件拷贝到项目的lib下;
加入ECSide的标签库,将标签拷贝到项目的WEB-INF下;
加入ECSide的css样式及js支持等内容,将common与ecside文件夹拷贝到项目的WebRoot下;
在web.xml中加入
ECSide的过滤器;
ecsideExport
org.ecside.filter.ECSideFilter
useEasyDataAccess
true
useEncoding
true
encoding
GBK
ecsideExport
/*
三、编写后台DAO代码
vo—dbc—dao—dao_impl—dao_proxy—factory
vo package mldn.lin.vo;
public class Employee {
private String username; //雇员的用户名,用于登陆系统用
private String password; //密码,MD5Code方式加密
private String name;
//雇员真实姓名
private String tel; //雇员联系电话
private int
roleid; //角色编号
private String rolename; //角色名称(冗余字段)
private int status; //雇员休假状态
private int isLock;
//用户名锁定状态
public int getIsLock() {
return isLock;
}
public
void setIsLock(int isLock) {
this.isLock = isLock;
}
public String
getPassword() {
return password;
}
public void setPassword(String
password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name =
name;
}
public int getRoleid() {
return roleid;
}
public
void setRoleid(int roleid) {
this.roleid = roleid;
}
public String
getRolename() {
return rolename;
}
public void setRolename(String
rolename) {
this.rolename = rolename;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status
= status;
}
public String getTel() {
return tel;
}
public
void setTel(String tel) {
this.tel = tel;
}
public String
getUsername() {
return username;
}
public void setUsername(String
username) {
this.username = username;
}
}
dbc
package mldn.lin.dbc;
import java.sql.Connection;
import
java.sql.DriverManager;
import java.sql.SQLException;
//mysql
public class DataBaseConnectionJDBC {
private static final String
DBDRIVER="org.gjt.mm.mysql.Driver";
private static final String
DBURL="jdbc:mysql://localhost:3306/ecside";
private static final String
DBUSER="root";
private static final String DBPASSWORD="mysqladmin";
private Connection conn=null;
public DataBaseConnectionJDBC() {
super();
try {
Class.forName(DBDRIVER);
this.conn=DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}catch
(SQLException e) {
e.printStackTrace();
}
}
//定义取得数据库连接的方法
public Connection getConnection(){
try {
if(this.conn==null ||
this.conn.isClosed()){
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
}
}
catch (SQLException e) {
e.printStackTrace();
} catch
(ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
//定义关闭数据库连接的方法
public void close(){
if(this.conn!=null){
try {
this.conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
dao
package
mldn.lin.dao;
import java.util.List;
import java.util.Map;
import mldn.lin.vo.Employee;
public interface IEmployeeDAO {
/**
* 添加记录的方法
* @param all 所有要添加的记录
* @return 是否插入成功
*
@throws Exception
*/
public boolean doInsert(List
all)throws Exception; /** * 删除记录的方法 * @param ids 所有要删除的记录的主键值
* @return 是否删除成功 * @throws Exception */ public boolean
doDelete(String[] usernames)throws Exception; /** * 更新记录的方法 *
@param all 所有修改后的记录 * @return 是否修改成功 * @throws Exception */
public boolean doUpdate(List all)throws Exception;
/** * 查询全部记录的方法 * @param startRow 开始的记录数(索引) * @param
endRow 结束的记录(索引) * @param searchMap 所有查询条件,key为字段名称,value为所要查询的关键字 *
@param sortMap 所有排序条件,key为排序的字段名称,value为排序规则(升序、降序) * @return 所有查询结果 *
@throws Exception */ public List doSelectAll(int
startRow,int endRow,Map searchMap,Map sortMap)throws Exception; /**
* 查询全部记录数 * @param searchMap 所有查询条件,key为字段名称,value为所要查询的关键字 *
@return 返回符合条件的记录数,没有返回0 * @throws Exception */ public int
getAllCount(Map searchMap)throws Exception; } dao_impl package mldn.lin.impl;
import
java.sql.PreparedStatement; import java.sql.ResultSet; import
java.util.ArrayList; import java.util.Iterator; import java.util.List;
import java.util.Map; import java.util.Map.Entry; import
mldn.lin.dao.IEmployeeDAO; import mldn.lin.dbc.DataBaseConnectionJDBC;
import mldn.lin.vo.Employee; import org.ecside.util.ECSideUtils;
public class EmployeeDAOImpl implements IEmployeeDAO { private
DataBaseConnectionJDBC dbc; public EmployeeDAOImpl
(DataBaseConnectionJDBC dbc){ this.dbc=dbc; } public boolean
doInsert(List all) throws Exception { // TODO Auto-generated
method stub String sql="INSERT INTO employee
(username,password,name,tel,roleid,rolename,status,isLock)
VALUES(?,?,?,?,?,?,?,?)"; PreparedStatement
prmt=this.dbc.getConnection().prepareStatement(sql); Iterator
iter=all.iterator(); Employee emp=new Employee(); while(iter.hasNext()){
emp=(Employee) iter.next(); prmt.setString(1, emp.getUsername());
prmt.setString(2, emp.getPassword()); prmt.setString(3, emp.getName());
prmt.setString(4, emp.getTel()); prmt.setInt(5, emp.getRoleid());
prmt.setString(6, emp.getRolename()); prmt.setInt(7, emp.getStatus());
prmt.setInt(8, emp.getIsLock()); if(prmt.executeUpdate()==0){ return
false; } } return true; } public boolean
doDelete(String[] usernames) throws Exception { // TODO Auto-generated
method stub String sql="DELETE FROM employee WHERE username IN(";
for(int i=0;iif(isql+="?,"; }else{ sql+="?)"; } } PreparedStatement
prmt=this.dbc.getConnection().prepareStatement(sql); for(int
i=0;iprmt.setString(i+1, usernames[i]); }
if(prmt.executeUpdate()>=0){ return true; } return
false; } public boolean doUpdate(List all) throws
Exception { // TODO Auto-generated method stub String sql="UPDATE
employee SET password=?,name=?,tel=?,roleid=?,rolename=?,status=?,isLock=? WHERE
username=?"; PreparedStatement
prmt=this.dbc.getConnection().prepareStatement(sql); Iterator
iter=all.iterator(); Employee emp=new Employee(); while(iter.hasNext()){
emp=(Employee) iter.next(); prmt.setString(1, emp.getPassword());
prmt.setString(2, emp.getName()); prmt.setString(3, emp.getTel());
prmt.setInt(4, emp.getRoleid()); prmt.setString(5, emp.getRolename());
prmt.setInt(6, emp.getStatus()); prmt.setInt(7, emp.getIsLock());
prmt.setString(8, emp.getUsername()); if(prmt.executeUpdate()==0){
return false; } } return true; } public
List doSelectAll(int startRow, int endRow, Map searchMap,
Map sortMap) throws Exception { // TODO Auto-generated method stub
String sql="SELECT username,password,name,tel,roleid,rolename,status,isLock
FROM employee WHERE 1=1"; if(searchMap!=null){ Iterator
iter=searchMap.entrySet().iterator(); if(iter.hasNext()){ sql+=" AND ";
Map.Entry entry=(Entry) iter.next(); if(entry.getKey().equals("roleid")
|| entry.getKey().equals("status") || entry.getKey().equals("isLock")){
sql+=entry.getKey()+"="+entry.getValue(); }else{
sql+=entry.getKey()+" LIKE '%"+entry.getValue()+"%'"; } } }
if(sortMap!=null){ sql+=" "+ ECSideUtils.getDefaultSortSQL(sortMap);
} sql+=" LIMIT ?,?"; PreparedStatement
prmt=this.dbc.getConnection().prepareStatement(sql); prmt.setInt(1,
startRow); prmt.setInt(2, (endRow-startRow)); ResultSet
rs=prmt.executeQuery(); List all=null; while(rs.next()){
if(all==null){ all=new ArrayList(); } Employee
emp=new Employee(); emp.setUsername(rs.getString(1));
emp.setPassword(rs.getString(2)); emp.setName(rs.getString(3));
emp.setTel(rs.getString(4)); emp.setRoleid(rs.getInt(5));
emp.setRolename(rs.getString(6)); emp.setStatus(rs.getInt(7));
emp.setIsLock(rs.getInt(8)); all.add(emp); } return all; }
public int getAllCount(Map searchMap) throws Exception { // TODO
Auto-generated method stub String sql="SELECT count(*) FROM employee WHERE
1=1"; if(searchMap!=null){ Iterator
iter=searchMap.entrySet().iterator(); if(iter.hasNext()){ sql+=" AND ";
Map.Entry entry=(Entry) iter.next(); if(entry.getKey().equals("roleid")
|| entry.getKey().equals("status") || entry.getKey().equals("isLock")){
sql+=entry.getKey()+"="+entry.getValue(); }else{
sql+=entry.getKey()+" LIKE '%"+entry.getValue()+"%'"; } } }
PreparedStatement prmt=this.dbc.getConnection().prepareStatement(sql);
ResultSet rs=prmt.executeQuery(); int count=0; if(rs.next()){
count=rs.getInt(1); } return count; } } dao_proxy
package mldn.lin.proxy; import java.sql.PreparedStatement;
import java.util.Iterator; import java.util.List; import
java.util.Map; import mldn.lin.dao.IEmployeeDAO; import
mldn.lin.dbc.DataBaseConnectionJDBC; import mldn.lin.impl.EmployeeDAOImpl;
import mldn.lin.vo.Employee; public class EmployeeDAOProxy
implements IEmployeeDAO { private DataBaseConnectionJDBC dbc; private
IEmployeeDAO iemployeedao; public EmployeeDAOProxy (){ this.dbc=new
DataBaseConnectionJDBC(); this.iemployeedao=new EmployeeDAOImpl(this.dbc);
} public boolean doInsert(List all) throws Exception
{ // TODO Auto-generated method stub if(all==null){ return false;
} this.dbc.getConnection().setAutoCommit(false); boolean flag=false;
try { flag=this.iemployeedao.doInsert(all); if(flag){
this.dbc.getConnection().commit(); }else{
this.dbc.getConnection().rollback(); } } catch (RuntimeException e)
{ this.dbc.getConnection().rollback(); e.printStackTrace();
}finally{ this.dbc.close(); } return true; } public
boolean doDelete(String[] usernames) throws Exception { // TODO
Auto-generated method stub if(usernames==null){ return false; }
boolean flag=false; try {
flag=this.iemployeedao.doDelete(usernames); } catch (RuntimeException e)
{ e.printStackTrace(); }finally{ this.dbc.close(); } return
flag; } public boolean doUpdate(List all) throws
Exception { // TODO Auto-generated method stub if(all==null){ return
false; } this.dbc.getConnection().setAutoCommit(false); boolean
flag=false; try { flag=this.iemployeedao.doUpdate(all); if(flag){
this.dbc.getConnection().commit(); }else{
this.dbc.getConnection().rollback(); } } catch (RuntimeException e)
{ this.dbc.getConnection().rollback(); e.printStackTrace();
}finally{ this.dbc.close(); } return true; } public
List doSelectAll(int startRow, int endRow, Map searchMap,
Map sortMap) throws Exception { // TODO Auto-generated method stub
List all=null; try { all=this.iemployeedao.doSelectAll(startRow,
endRow, searchMap, sortMap); } catch (RuntimeException e) {
e.printStackTrace(); }finally{ this.dbc.close(); } return
all; } public int getAllCount(Map searchMap) throws Exception {
// TODO Auto-generated method stub int count=0; try {
count=this.iemployeedao.getAllCount(searchMap); } catch
(RuntimeException e) { e.printStackTrace(); }finally{
this.dbc.close(); } return count; } } factory
package mldn.lin.dao.factory; import mldn.lin.dao.IEmployeeDAO;
import mldn.lin.dao.IRoleDAO; import mldn.lin.proxy.EmployeeDAOProxy;
import
mldn.lin.proxy.RoleDAOProxy; public class DAOFactory {
public static IEmployeeDAO getIEmployeeDAOInstance(){ return new
EmployeeDAOProxy(); } } 四、建立超链接列表页 <%@ page
language="java" pageEncoding="GBK"%> <% String path =
request.getContextPath(); String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> My JSP 'index.jsp'
starting page
进入列表
五、建立Action与ActionForm
ActionForm /* * Generated by MyEclipse Struts * Template path:
templates/java/JavaClass.vtl */ package mldn.lin.struts.form;
import javax.servlet.http.HttpServletRequest; import
org.apache.struts.action.ActionErrors; import
org.apache.struts.action.ActionForm; import
org.apache.struts.action.ActionMapping; /** * MyEclipse Struts *
Creation date: 05-13-2009 * * XDoclet definition: * @struts.form
name="employForm" */ public class EmployForm extends ActionForm { /*
* Generated fields */ /** status property */ private String
status; /* * Generated Methods */ /** * Method
validate * @param mapping * @param request * @return ActionErrors
*/ public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) { // TODO Auto-generated method stub
return null; } /** * Method reset * @param mapping *
@param request */ public void reset(ActionMapping mapping,
HttpServletRequest request) { // TODO Auto-generated method stub }
/** * Returns the status. * @return String */ public
String getStatus() { return status; } /** * Set the status.
* @param status The status to set */ public void setStatus(String
status) { this.status = status; } } Action /* *
Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl
*/ package mldn.lin.struts.action; import java.util.List;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import
mldn.lin.dao.factory.DAOFactory; import mldn.lin.struts.form.EmployForm;
import org.apache.struts.action.ActionForm; import
org.apache.struts.action.ActionForward; import
org.apache.struts.action.ActionMapping; import
org.apache.struts.actions.DispatchAction; import
org.ecside.table.limit.Limit; import org.ecside.util.RequestUtils;
/** * MyEclipse Struts * Creation date: 05-13-2009 * *
XDoclet definition: * @struts.action path="/employ" name="employForm"
input="/error.jsp" parameter="status" scope="request" validate="true" */
public class EmployAction extends DispatchAction { /* * Generated
Methods */ /** * Method execute * @param mapping *
@param form * @param request * @param response * @return
ActionForward */ public ActionForward list(ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse response) {
EmployForm employForm = (EmployForm) form;// TODO Auto-generated method stub
Limit limit=RequestUtils.getLimit(request); Map
searchMap=limit.getFilterSet().getPropertyValueMap(); Map
sortMap=limit.getSort().getSortValueMap(); try { int allRecorders =
DAOFactory.getIEmployeeDAOInstance().getAllCount(searchMap); int[]
startEndRow=RequestUtils.getRowStartEnd(request, allRecorders, 5); List
all=DAOFactory.getIEmployeeDAOInstance().doSelectAll(startEndRow[0],
startEndRow[1], searchMap, sortMap); request.setAttribute("allEmployee",
all); } catch (Exception e) { // TODO Auto-generated catch block
e.printStackTrace(); } return mapping.findForward("list"); }
} web.xml
action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml
debug
3
detail
3
0
action
*.do
ecsideExport
org.ecside.filter.ECSideFilter
useEasyDataAccess
true
useEncoding
true
encoding
GBK
ecsideExport
/*
index.jsp
struts_config.xml
attribute="employForm"
input="/error.jsp" name="employForm"
parameter="status" path="/employee" scope="request"
type="mldn.lin.struts.action.EmployAction" >
六、在列表页上导入ECSide的标签、支持js、样式表 <%@
page language="java" pageEncoding="GBK"%> <%@ taglib
uri="" prefix="ec"%> <%@ taglib
uri="" prefix="bean"%> <%@ taglib
uri="" prefix="html"%> <%@ taglib
uri="" prefix="logic"%> <%@ taglib
uri="" prefix="tiles"%>
list.jsp
href="${pageContext.request.contextPath
}/ecside/css/ecside_style.css"> 七、列表显示数据
retrieveRowsCallback="limit" sortRowsCallback="limit"
filterRowsCallback="limit" action="employee.do?status=list"
useAjax="true" pageSizeList="2,5,10,50,all" rowsDisplayed="5" title=""
editable="true" xlsFileName="文件1.xls" csvFileName="文件2.csv"
pdfFileName="文件3.pdf" showPrint="true" sortable="true"
resizeColWidth="true" filterable="true"
updateAction="employee.do?status=update"
insertAction="employee.do?status=insert"
deleteAction="employee.do?status=delete" classic="true">
八、实现查询功能 retrieveRowsCallback="limit"
sortRowsCallback="limit" filterRowsCallback="limit"
action="employee.do?status=list" useAjax="true" pageSizeList="2,5,10,50,all"
rowsDisplayed="5" title="" editable="true" xlsFileName="文件1.xls"
csvFileName="文件2.csv" pdfFileName="文件3.pdf" showPrint="true" sortable="true"
resizeColWidth="true" filterable="true"
updateAction="employee.do?status=update"
insertAction="employee.do?status=insert"
deleteAction="employee.do?status=delete" classic="true">