重点:
我们今天用easyui来实现增删改查
我们今天要用到的布局有datagrid,dialog,form布局,用这几个控件完成一个简单的布局
第二个重点:
1.实现前后端的分离
我们要如何实现呢,接下来
思路:
首先利用form 和dialog进行布局,然后利用datadrid进行数据的回显以及传递数据操作所需要的值,然后我们利用修改传递过来的值进行增删改查。
UserDao
package com.liuxia.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public class UserDao extends JsonBaseDao {
public List<Map<String, Object>> list(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql ="select * from t_easyui_user_version2 where true ";
String uid= JsonUtils.getParamVal(paMap,"uid");
String upwd= JsonUtils.getParamVal(paMap,"upwd");
if(StringUtils.isNotBlank(uid)) {
sql +=" and uid = "+uid;
}
if(StringUtils.isNotBlank(upwd)) {
sql +=" and upwd ="+upwd;
}
return super.executeQuery(sql, pageBean);
}
/**
* 修改
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int edit(Map<String, String[]> paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql= "update t_easyui_user_version2 set uid=?,uname=?,upwd=? where serialno=?";
return super.executeUpdate(sql,new String[] {"uid","uname","upwd","SerialNo"}, paMap);
}
/**
* 删除
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int del(Map<String, String[]> paMap) throws Exception, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql ="delete from t_easyui_user_version2 where SerialNo =? ";
return super.executeUpdate(sql, new String[] {"SerialNo"} , paMap);
}
/**
* 添加
* @param paMap
* @return
* @throws Exception
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int add(Map<String, String[]> paMap) throws Exception, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql ="insert into t_easyui_user_version2 (uid,uname,upwd) values (?,?,?)";
return super.executeUpdate(sql, new String[] {"uid","uname","upwd"} , paMap);
}
/**
* 根据当前用户登录的ID去查询对应的所有菜单
* @param paMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> getMenuByUid(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql ="select * from t_easyui_usermenu where true ";
String uid= JsonUtils.getParamVal(paMap,"uid");
String upwd= JsonUtils.getParamVal(paMap,"upwd");
if(StringUtils.isNotBlank(uid)) {
sql +=" and uid = "+uid;
}
return super.executeQuery(sql, pageBean);
}
UserAction
package com.liuxia.web;
public class UserAction extends ActionSupport {
private UserDao userDao = new UserDao();
/**
* 登录成功后跳转index.jsp
* @param req
* @param resp
* @return
*/
public String login(HttpServletRequest req,HttpServletResponse resp) {
// 系统中是否有当前用户
try {
Map<String, Object> map = null;
try {
map = this.userDao.list(req.getParameterMap(), null).get(0);
} catch (Exception e) {
req.setAttribute("msg", "用户不存在");
return "login";
}
有
// 查询用户菜单中间表,获取对应的menuid的集合
if(map != null && map.size() > 0) {
// [{Menuid:002,...},{Menuid:003}]
StringBuilder sb = new StringBuilder();
List<Map<String, Object>> menuIdArr = this.userDao.getMenuByUid(req.getParameterMap(), null);
for (Map<String, Object> m : menuIdArr) {
002,003
sb.append(","+m.get("menuId"));
}
req.setAttribute("menuIds", sb.substring(1));
return "index";
}else {
没有
req.setAttribute("msg", "用户不存在");
return "login";
}
} catch (InstantiationException | IllegalAccessException | SQLException e) {
e.printStackTrace();
return "login";
}
}
/**
* 数据加载
* @param req
* @param resp
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public String list(HttpServletRequest req,HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException {
ObjectMapper om=new ObjectMapper();
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
try {
List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), pageBean);
Map<String, Object> map= new HashMap<String,Object>();
map.put("total",pageBean.getTotal());
map.put("rows",list);
ResponseUtil.write(resp,om.writeValueAsString(map));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* form组件提交方式
* @param req
* @param resp
* @return
* @throws Exception
* @throws JsonProcessingException
*/
public String edit(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
try {
int code=this.userDao.edit(req.getParameterMap());
ObjectMapper om=new ObjectMapper();
Map<String, Object> map= new HashMap<String,Object>();
map.put("code",code);
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException e) {
e.printStackTrace();
}
return null;
}
/**
* 删除
* @param req
* @param resq
* @return
*/
public String del(HttpServletRequest req,HttpServletResponse resq) {
try {
int del = this.userDao.del(req.getParameterMap());
ObjectMapper om =new ObjectMapper();
ResponseUtil.write(resq, om.writeValueAsString(del));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 添加
* @param req
* @param resq
* @return
*/
public String add(HttpServletRequest req,HttpServletResponse resq) {
try {
int add = this.userDao.add(req.getParameterMap());
ObjectMapper om =new ObjectMapper();
ResponseUtil.write(resq, om.writeValueAsString(add));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
userManage.js
$(function () {
$('#dg').datagrid({
url:'../userAction.action?methodName=list',
fit:true,
fitColumns:true,
pagination:true,
singleSelect:true,
columns:[[
{field:'uid',title:'ID',width:100},
{field:'uname',title:'用户名',width:100},
{field:'upwd',title:'密码',width:100,align:'right'}
]],
toolbar: [{
iconCls: 'icon-add',
handler: function(){
$('#ff').form('clear');//清空文本框的值
$('#dd').dialog('open');//打开表格
$("#dd").attr("title","增加用户");//增加信息
$("#lin").val("add"); //通过隐藏ID来设置增加方法
}
},'-',{
iconCls: 'icon-edit',
handler: function(){
$('#dd').dialog('open')
//到datagrid控件中找需要填数据
var row = $('#dg').datagrid('getSelected');
if(row){
//get_data.php指的是回填的数据
$('#ff').form('load',row);
}else{
alert('请选择你要修改的数据!');
}
}
},'-',{
iconCls: 'icon-remove',
handler: function(){
var row =$('#dg').datagrid('getSelected');//选择你要删除的行
if(row){//是否选中
$.ajax({
url:'../userAction.action?methodName=del&&SerialNo='+row.SerialNo //传一个删除del方法跟serialNo列名值
});
alert('删除成功');
$('#dg').datagrid('reload');//刷新方法
}else{
alert('删除失败');
}
}
},'-',{
iconCls: 'icon-reload',
handler: function(){alert('刷新按钮')}
}]
});
})
function ok() {
alert('ok');
$('#ff').form('submit', {
url:'../userAction.action?methodName=edit',
success:function(data){
$('#ff').form('clear');
$('#dd').dialog('close');
$('#dg').datagrid('reload');
/* alert(data) */
//针对于后端返回的结果集进行处理
}
});
}
userManage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>人员信息维护管理界面</title>
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/userManage.js"></script>
</head>
<body>
<!-- 展示数据 -->
<table id="dg"></table>
<!-- 弹窗 -->
<div id="dd" class="easyui-dialog" title="编辑窗体"
style="width: 400px; height: 200px;"
data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true,buttons:'#bb'">
<!-- 提交的from 表单 -->
<form id="ff" method="post">
<input type="hidden" name="SerialNo">
<div>
<label for="uid">uid:</label> <input class="easyui-validatebox"
type="text" name="uid" data-options="required:true" />
</div>
<div>
<label for="uname">uname:</label> <input class="easyui-validatebox"
type="text" name="uname" data-options="required:true" />
</div>
<div>
<label for="upwd">upwd:</label> <input class="easyui-validatebox"
type="text" name="upwd" data-options="required:true" />
</div>
</form>
</div>
<a href="#" class="easyui-linkbutton" onclick="ok()">保存</a>
<a href="#" class="easyui-linkbutton">关闭</a>
</div>
</body>
</html>
全部结果出来了,数据回显还有增删改查功能