一、工程目录:
二、ExamEntity.java的代码:
package com.esen.esexam.entity;
/**
* exam实体类
*
* @author ligw
* @since 2019年4月10日
*/
public class ExamEntity {
private String examId;
private String examName;
private String examDate;
private String examTask;
private String personalTall;
/**
* 获得Id-主键
*
* @return the examId
*/
public String getExamId() {
return examId;
}
/**
* 设置Id-主键
*
* @param examId the examId to set
*/
public void setExamId(String examId) {
this.examId = examId;
}
/**
* 获得姓名
*
* @return the examName
*/
public String getExamName() {
return examName;
}
/**
* 设置姓名
*
* @param examName the examName to set
*/
public void setExamName(String examName) {
this.examName = examName;
}
/**
* 获得日期
*
* @return the examDate
*/
public String getExamDate() {
return examDate;
}
/**
* 设置日期
*
* @param examDate the examDate to set
*/
public void setExamDate(String examDate) {
this.examDate = examDate;
}
/**
* 获得任务
*
* @return the examTask
*/
public String getExamTask() {
return examTask;
}
/**
* 设置任务
*
* @param examTask the examTask to set
*/
public void setExamTask(String examTask) {
this.examTask = examTask;
}
/**
* 获得自述
*
* @return the personalTall
*/
public String getPersonalTall() {
return personalTall;
}
/**
* 设置自述
*
* @param personalTall the personalTall to set
*/
public void setPersonalTall(String personalTall) {
this.personalTall = personalTall;
}
}
三、Server.java的代码:
package com.esen.esexam;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.esen.jdbc.ConnectFactoryManager;
import com.esen.jdbc.ConnectFactoryManagerImpl;
import com.esen.jdbc.ConnectionFactory;
import com.esen.jdbc.DefaultConnectionFactory;
import com.esen.jdbc.PoolConnectionFactory;
import com.esen.jdbc.ibatis.SqlMapClientFactory;
import com.esen.vfs2.Vfs2;
import com.esen.vfs2.VfsFactoryDB;
import com.esen.vfs2.VfsMgr;
import com.esen.vfs2.VfsOperator;
import com.esen.vfs2.impl.VfsOperatorImpl;
/**
* 此对象为单例,通过getInstance方法获取实例
* 通过该对象获取数据库连接池工厂和VFS实例
*
* @author ligw
* @since 2019年4月10日
*/
public class Server {
/**
* vfs的数据库表
*/
public static final String VFS_TABLENAME = "TB_VFS";
private ConnectFactoryManagerImpl cfm = null;
private Vfs2 vfs;
private ConnectionFactory factory = null;
private VfsOperator admin = null;
/**
* 初始化一个Server的实例
*/
private static Server svr = new Server();
/**
* 私有化构造方法
*/
private Server() {
cfm = new ConnectFactoryManagerImpl();
DefaultConnectionFactory.set(cfm);
}
/**
* 获得Server实例对象
* @return
*/
public static Server getInstance() {
return svr;
}
/**
* 获取Admin身份的操作者
* @return
*/
public synchronized final VfsOperator getVfsOperatorAsAdmin() {
if (admin == null) {
admin = new VfsOperatorImpl("admin", true);
}
return admin;
}
/**
* 获取VFS实例
* 使用代码参考
* @return
* @throws Exception
*/
public synchronized final Vfs2 getVfs() throws Exception {
if (vfs == null) {
Vfs2 _vfs = VfsMgr.createVfsInstance();
if (_vfs == null) {
ConnectionFactory fct = this.getConnectionFactory();
cfm.setConnectionFactory("*", fct);
Properties pp = new Properties();
SqlMapClientFactory.setFacoty(new SqlMapClientFactory());
_vfs = VfsFactoryDB.createVfs(fct, VFS_TABLENAME, pp);
}
vfs = _vfs;
}
return vfs;
}
/**
* 获得ConnectFactoryManager,第二周的orm会使用
* @return
*/
public ConnectFactoryManager getConnectFactoryManager() {
return cfm;
}
/**
* 获取数据库连接池工厂
* @throws Exception
*/
public synchronized final ConnectionFactory getConnectionFactory() throws Exception {
if (factory == null) {
InputStream input = Server.class.getResourceAsStream("jdbc.conf");
try {
Properties p = new Properties();
try {
p.load(input);
} catch (IOException e) {
throw new RuntimeException("jdbc配置文件读取失败!");
}
factory = new PoolConnectionFactory(null, p);
} finally {
input.close();
}
}
return factory;
}
}
四、jdbc.conf的内容:
#charset UTF-8
#Sat Apr 27 11:05:52 GMT 2013
logLevel=DEBUG
catalog=
maxWait=15000
maxActive=80
url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
username=c##peixun4
password=peixun4
driverClassName=oracle.jdbc.driver.OracleDriver
五、examEntity-mapping.xml的代码:
六、ExamDao.java的代码:
package com.esen.esexam.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.esen.esexam.Server;
import com.esen.esexam.entity.ExamEntity;
import com.esen.jdbc.ConnectFactoryManagerImpl;
import com.esen.jdbc.ConnectionFactory;
import com.esen.jdbc.DefaultConnectionFactory;
import com.esen.jdbc.orm.EntityInfoManager;
import com.esen.jdbc.orm.EntityInfoManagerFactory;
import com.esen.jdbc.orm.Session;
import com.esen.jdbc.orm.SessionFactory;
import com.esen.jdbc.orm.SessionFactoryBuilder;
import com.esen.util.StrFunc;
import com.esen.util.UNID;
/**
* examDao类
*
* 连接数据库,操作数据库的方法
* @author ligw
* @since 2019年4月10日
*/
public class ExamDao {
/**
* 工厂管理员:entityManager
*/
private EntityInfoManager entityManager;
/**
* 静态的私有对象实例
*/
private static ExamDao instance;
/**
* session工厂:sessionFactory
*/
private SessionFactory sessionFactory;
/**
* 私有化构造方法,初始化
*
* @throws Exception
*/
private ExamDao() throws Exception {
load();
}
/**
* 获得单一实例对象,主要线程安全
*
* @return
* @throws Exception
*/
public static synchronized ExamDao getInstance() throws Exception {
if (instance == null) {
instance = new ExamDao();
}
return instance;
}
/**
* 获取Session
*
* @return
* @throws Exception
*/
public Session getSession() throws Exception {
return sessionFactory.openSession();
}
/**
* 初始化,连接池,sessionFactory
* @throws Exception
*/
private void load() throws Exception {
// 创建连接池ConnectionFactory,以及连接池管理对象,将ConnectionFactory放入管理对象中,名称是“*”
// 以下代码服务器只会创建一次,如果已经创建过连接池,请不要重复创建
// 定义工厂
ConnectionFactory fct = Server.getInstance().getConnectionFactory();
// 定义工厂管理员
ConnectFactoryManagerImpl fm = new ConnectFactoryManagerImpl();
// 把工厂标记起来
fm.setConnectionFactory("*", fct);
DefaultConnectionFactory.set(fm);
// 根据xml文件,build解析成实体类的管理对象 EntityInfoManager
entityManager = EntityInfoManagerFactory.buildFromURL(this.getClass().getResource("examEntity-mapping.xml"));
// 创建SessionFactory工厂,通过标记来调用工厂
sessionFactory = SessionFactoryBuilder.build("*", entityManager);
}
/**
* 分页查询exam
* @param pi 起始数
* @param ps 单页总数
* @return
* @throws Exception
*/
public List
// 字符串转整形转换
int pageIndex = StrFunc.str2int(pi, 0);
int pageSize = StrFunc.str2int(ps, 14);
// 计算查询分页的索引
int fromNum = pageIndex * pageSize;
int toNum = (pageIndex + 1) * pageSize;
Session session = sessionFactory.openSession();
try {
Connection conn = session.getConnection();
try {
String sql = "select * from (select rownum rn ,examId ,examName, examDate, examTask ,personalTall from exam ) where rn > "
+ fromNum + " and rn <= " + toNum;
PreparedStatement statement = conn.prepareStatement(sql);
try {
ResultSet rs = statement.executeQuery();
try {
List
while (rs.next()) {
ExamEntity exam = new ExamEntity();
exam.setExamId(rs.getString("examId"));
exam.setExamName(rs.getString("examName"));
exam.setExamDate(rs.getString("examDate"));
exam.setExamTask(rs.getString("examTask"));
exam.setPersonalTall(rs.getString("personalTall"));
models.add(exam);
}
return models;
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
conn.close();
}
} finally {
session.close();
}
}
/**
* 查询数据的总条数
* @return
* @throws SQLException
* @throws JSONException
*/
public Object getTotalCount() throws SQLException, JSONException {
Session session = sessionFactory.openSession();
try {
Connection conn = session.getConnection();
try {
String sql = "select count(*) from exam";
PreparedStatement pst = conn.prepareStatement(sql);
try {
ResultSet rs = pst.executeQuery();
try {
JSONArray ja = new JSONArray();
while (rs.next()) {
JSONObject jo = new JSONObject();
jo.put("totalCount", rs.getString(1));
ja.put(jo);
}
return ja;
} finally {
rs.close();
}
} finally {
pst.close();
}
} finally {
conn.close();
}
} finally {
session.close();
}
}
/**
* 添加任务
* @param examName 姓名
* @param examDate 日期
* @param examTask 任务
* @param personalTall 自述
*/
public void addTask(String examName, String examDate, String examTask, String personalTall) throws SQLException {
String examId = UNID.randomID().substring(0, 3);
String insertTask = "insert into exam values ('" + examId + "', '" + examName + "', '" + examDate + "', '"
+ examTask + "', '" + personalTall + "')";
Session session = sessionFactory.openSession();
try {
Connection conn = session.getConnection();
try {
Statement st = conn.createStatement();
try {
st.addBatch(insertTask);
st.executeBatch();
} finally {
st.close();
}
} finally {
conn.close();
}
} finally {
session.close();
}
}
/**
* 根据examId删除exam数据
* @param examIds 前端勾选数据的examId
* @throws SQLException
*/
public void deleteExam(String examIds) throws SQLException {
String[] examIdss = examIds.split(",");
for (int i = 0; i < examIdss.length; i++) {
String deleteExam = "delete from exam where examId = '" + examIdss[i] + "'";
Session session = sessionFactory.openSession();
try {
Connection conn = session.getConnection();
try {
Statement st = conn.createStatement();
try {
st.addBatch(deleteExam);
st.executeBatch();
} finally {
st.close();
}
} finally {
conn.close();
}
} finally {
session.close();
}
}
}
}
七、ActionExamWelcome.java的代码:
package com.esen.esexam.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.esen.esexam.dao.ExamDao;
import com.esen.esexam.entity.ExamEntity;
import com.esen.util.ExceptionHandler;
import com.esen.util.StrFunc;
import com.esen.weblib.ClientResult;
import com.esen.weblib.action.Action_Js;
/**
* examAction类
*
* 负责返回视图 负责接受前台的js请求
* @author ligw
* @since 2019年4月10日
*/
@Controller
@RequestMapping("/examWelcome")
public class ActionExamWelcome extends Action_Js {
/**
* 返回前台页面
*/
@Override
public String execute(HttpServletRequest req, HttpServletResponse res) throws Exception {
// 参数cmd 是判断分支的参数,不同参数走不同的分支处理
String cmd = req.getParameter("cmd");
// 如果cmd为空,返回主界面
if (StrFunc.isNull(cmd)) {
return examWelcome(req, res);
}
// 调用父类的execute方法,以便跳到下面的jsexecute方法
return super.execute(req, res);
}
/**
* 该方法主要负责前台的一些js请求,将请求数据返回前台
*/
@Override
protected String jsexecute(HttpServletRequest req, ClientResult result) throws Exception {
try {
// 参数cmd 是判断分支的参数,不同参数走不同的分支处理
String cmd = req.getParameter("cmd");
// 查询exam数据
if (StrFunc.compareStrIgnoreCase(cmd, "getExam")) {
String pageindex = req.getParameter("pageIndex");
String pagesize = req.getParameter("pageSize");
ExamDao ed = ExamDao.getInstance();
List
JSONArray array = new JSONArray();
for (ExamEntity exam : models) {
JSONObject bj = new JSONObject();
bj.put("examId", exam.getExamId());
bj.put("examName", exam.getExamName());
bj.put("examDate", exam.getExamDate());
bj.put("examTask", exam.getExamTask());
bj.put("personalTall", exam.getPersonalTall());
array.put(bj);
}
result.getWriter().write(array.toString());
}
// 查询数据的总条数
else if (StrFunc.compareStrIgnoreCase(cmd, "getCountExam")) {
ExamDao ed = ExamDao.getInstance();
result.getWriter().write(ed.getTotalCount().toString());
}
// 添加数据
else if (StrFunc.compareStrIgnoreCase(cmd, "addTask")) {
String examName = req.getParameter("examName");
String examDate = req.getParameter("examDate");
String examTask = req.getParameter("examTask");
String personalTall = req.getParameter("personalTall");
ExamDao ed = ExamDao.getInstance();
ed.addTask(examName, examDate, examTask, personalTall);
}
// 根据examId删除数据
else if (StrFunc.compareStrIgnoreCase(cmd, "deleteExam")) {
String examIds = req.getParameter("id");
ExamDao ed = ExamDao.getInstance();
ed.deleteExam(examIds);
}
} catch (Exception e) {
ExceptionHandler.rethrowRuntimeException(e);
}
return null;
}
/**
* 返回xuiExam.ftl页面
* @param req
* @param res
* @return
*/
private String examWelcome(HttpServletRequest req, HttpServletResponse res) {
return "esexam/xuiExam";
}
}
八、xuiExam.flt的代码:
<@comm.page>
<@comm.head title="xuiExam">
@comm.head>
<@comm.body>
九、exam.js 的代码:
/**
* exam类对象的封装
*
* @param wnd
* @param parentElement
* @returns
*/
function xuiExam(wnd, parentElement) {
XComponent.call(this, wnd, parentElement);
this._initDom();
}
extendClass(xuiExam, XComponent, "xuiExam");// xuiExam继承XComponent
/**
* 初始化dom结构
*/
xuiExam.prototype._initDom = function() {
// 记录勾选数据的examId
this.examIds = [];
// 定义xcoolbar
var barDiv = this.parentElement.appendChild(this.doc.createElement("div"));
var coolBar = new XCoolBar(this.wnd, barDiv, "100%", "100%");
var coolDom = coolBar.__getBaseDom();
// 定义xbutton
var buttonDiv = this.parentElement.appendChild(this.doc
.createElement("div"));
buttonDiv.style.cssText += ";border:2px black solid;";
this._initAddButton(coolDom);
this._initDeleteButton(coolDom);
// 定义数据列表
var xlistDiv = this.parentElement
.appendChild(this.doc.createElement("div"));
xlistDiv.style.cssText += ";border:2px black solid;height:94%;width:100%;";
this._initXlist(xlistDiv);
}
/**
* 加载新增button
*/
xuiExam.prototype._initAddButton = function(dom) {
var xbutton = new XButton(this.wnd, dom, "新增", false);
var self = this;
xbutton.setOnClick(function() {
var dlg = new XDialog("添加任务", "460px", "400px", this.wnd);
var dom = dlg.getContent();// 获取弹窗的dom
// 姓名一行
var div1 = dom.appendChild(this.doc.createElement("div"));
div1.style.cssText += ";margin: 15px 120px;";
var span1 = div1.appendChild(this.doc.createElement("span"));
span1.innerHTML = "*姓名:";
var input1 = div1.appendChild(this.doc.createElement("input"));
// 日期一行
var div2 = dom.appendChild(this.doc.createElement("div"));
div2.style.cssText += ";margin: 15px 120px;";
var span2 = div2.appendChild(this.doc.createElement("span"));
span2.innerHTML = "*时间:";
// new日期组件
var date = new XCalendarCombobox(this.wnd, div2, "150px");
// 任务一行
var div4 = dom.appendChild(this.doc.createElement("div"));
div4.style.cssText += ";margin: 15px 120px;";
var div5 = div4.appendChild(this.doc.createElement("div"));
div5.style.cssText += ";float: left;";
var span4 = div5.appendChild(this.doc.createElement("span"));
span4.innerHTML = "任务:";
var div6 = div4.appendChild(this.doc.createElement("div"));
div6.style.cssText += ";float: left;";
var textarea1 = div6.appendChild(this.doc
.createElement("textarea"));
textarea1.style.cssText += ";height:100px;width:150px;";
// 自述一行
var div7 = dom.appendChild(this.doc.createElement("div"));
div7.style.cssText += ";margin: 15px 120px;";
var div8 = div7.appendChild(this.doc.createElement("div"));
div8.style.cssText += ";float: left;";
var span8 = div8.appendChild(this.doc.createElement("span"));
span8.innerHTML = "自述:";
var div9 = div7.appendChild(this.doc.createElement("div"));
div9.style.cssText += ";float: left;";
var textarea2 = div9.appendChild(this.doc
.createElement("textarea"));
textarea2.style.cssText += ";height:100px;width:150px;";
// 确认按钮
var _btOk = dlg.addButton("确认", null, null, null, true);
dlg.open();
_btOk.setOnClick(function() {
if (input1.value == "") {
showMessage("请输入姓名,谢谢!!!");
} else if (date.getDate() == "") {
showMessage("请输入日期,谢谢!!!");
} else {
// 传给服务器的参数
var map = new Map();
map.put("cmd", "addTask");
map.put("examName", input1.value);
map.put("examDate", date.getDate());
map.put("examTask", textarea1.value);
map.put("personalTall", textarea2.value);
QueryObj.create(
sys.getContextPath() + "examWelcome.do", map,
function(query) {
try {
showMessage("添加成功!!!");
dlg.close();
} catch (e) {
showError(e);
}
});
}
location.reload(1000);
});
// 取消按钮
var _btCancel = dlg.addButton("取消");
_btCancel.setOnClick(function() {
dlg.close();
});
});
}
/**
* 加载删除button
*/
xuiExam.prototype._initDeleteButton = function(dom) {
var xbutton = new XButton(this.wnd, dom, "删除", false);
var self = this;
xbutton.setOnClick(function() {
// 删除后台数据
var map = new Map();
map.put("cmd", "deleteExam");
map.put("id", self.examIds);
// 向服务器请求,异步的
QueryObj.create(sys.getContextPath() + "examWelcome.do", map, function(
query) {
try {
var cd = new confirmDialog("删除", "确定删除?", true, null);
showMessage("删除成功!!!");
location.reload(10000);
} catch (e) {
showError(e);
}
});
});
}
/**
* 初始化xlist
*/
xuiExam.prototype._initXlist = function(dom) {
var self = this;// 因为在这个地方 this才是bookxtree这个对象
var columns = [ [ "", "30px" ], [ "姓名", "200px" ], [ "日期", "200px" ],
[ "任务", "200px" ], [ "自述", "200px" ], [ "操作", "200px" ] ];
this.xlist = new XList(this.wnd, dom, "100%", "84.5%");
// 添加复选框,要记得不要再前面的new XList()里头添加columns,要在后面的initwithcolumns()里添加columns
var cols = [ "ck" ];
this.xlist.setAdditionalCols(cols);
this.xlist.initWithColumns(columns);
// 加载首页数据
this.queryexam();
var map = new Map();
map.put("cmd", "getCountExam");
// 向服务器请求,异步的
QueryObj.create(sys.getContextPath() + "examWelcome.do", map, function(
query) {
try {
// 检查返回结果,有异常则抛出来
query.checkResult;
// 获取后台结果
var countExam = query.getResponseJSON();
var tc = countExam[0].totalCount;
var userdata = "";
// 分页条,加载各页图书管理数据
var bar1 = new XPageBar(dom, // 第一个参数,创建分页条的dom元素
{ // 第二个参数,创建分页的一些信息
style : 'text', // 'icon'为图标版本分页,'text'为文字链接分页,默认为'text'
pageSize : 14, // 表示一页显示的行数
totalCount : tc, // 表示总数,可以为-1表示不确定。总数需要请求后台获得
pageIndex : 0, // 表示当前显示的是第几页
hasMoreData : true
// 表示是否还有更多的数据(当totalCount为-1时,此属性起效)
},
// 第三个参数,点击上一页,下一页,翻页的 回调函数
// pageIndex-第几页;pageSize-每页显示的行数;userdata-外面传进来的参数
function(pageIndex, pageSize, userdata) {
// 这是翻页会触发的事件
// 负责处理,向后台请求数据,获得数据后,将数据加载到前台
// 传给服务器的参数
var map = new Map();
map.put("cmd", "getExam");
map.put("pageIndex", pageIndex);
map.put("pageSize", pageSize);
// 获取选中的items
var items = self.xlist.getCheckedItems();
// 点击下一页的时候,获取选中的items,然后获取examId,保存在一个全局变量examIds里
self.getItems(items);
// 向服务器请求,异步的
QueryObj.create(sys.getContextPath() + "examWelcome.do", map,
function(query) {
try {
// 检查返回结果,有异常则抛出来
query.checkResult;
// 获取后台结果
var records = query.getResponseJSON();
// 清空上一个xlist的数据
self.xlist.clear();
// 迭代赋值
for (var i = 0; i < records.length; i++) {
var examId = records[i].examId;
var examName = records[i].examName;
var examDate = records[i].examDate;
var examTask = records[i].examTask;
var personalTall = records[i].personalTall;
var row = self.xlist.addRow("", examName,
examDate, examTask, personalTall,
"编辑 删除 ");
row.setUserObj(examId);
}
} catch (e) {
showError(e);
}
});
}, userdata // 第四个参数,传到第三个参数回调函数中使用的
);
} catch (e) {
showError(e);
}
});
}
/**
* 加载首页数据
*/
xuiExam.prototype.queryexam = function() {
var self = this;
// 生成请求参数
var map = new Map();
map.put("cmd", "getExam");
map.put("pageIndex", 0);
map.put("pageSize", 14);
// 向服务器发出请求
QueryObj.create(sys.getContextPath() + "examWelcome.do", map, function(
query) {
try {
// 检查返回结果,有异常那个就抛出来
query.checkResult();
// 获得后台数据
var arr = query.getResponseJSON();
// 清空上一个xlist的数据
self.xlist.clear();
// 填充xlist
for (var i = 0; i < arr.length; i++) {
var examId = arr[i].examId;
var examName = arr[i].examName;
var examDate = arr[i].examDate;
var examTask = arr[i].examTask;
var personalTall = arr[i].personalTall;
var row = self.xlist.addRow("", examName, examDate, examTask,
personalTall, "编辑 删除 ");
row.setUserObj(examId);
}
} catch (e) {
showError(e);
}
});
}
/**
* 没有按下一页,选择的时候,就对选中的items,getUserObject 以数组的形式保存,选中的examId
*/
xuiExam.prototype.getItems = function(items) {
var len = items.length;
for (var i = 0; i < len; i++) {
var examId = items[i].getUserObj();
this.examIds.push(examId);
}
}
/**
* 销毁对象
*/
xuiExam.prototype.dispose = function() {
XComponent.prototype.dispose.call(this);
}