目录
- easyui的crud(dialog,datagrid,form讲解)
- 陈旧的开发模式
- 前后端分离
- 代码演示
easyui的crud(dialog,datagrid,form讲解)
- datagrid布局
- dialog布局
- form布局
- 通用的JsonBaseDao的增删改方法
- dao层
- web层
- 功能完善
陈旧的开发模式 美工(ui工程师:出一个项目模型)
Java工程师:将原有的html转成jsp,动态展示数据
缺点:客户需要调节前端的展示效果,耦合性非常高;项目周期较长
解决:由美工区重新排版,重新选色
前后端分离 美工、java工程师都是独立工作,彼此之间开发过程中是没有任何交际。项目周期缩短。
在开发前约定数据交互的格式。
Java工程师的工作:写方法返回数据如tree_data1.json
美工:只管展示tree_data1.json
小结:实际上企业开发用的前后端分离技术不是easyui,本次只是用easyui阐述前后端分离的开发模式。当然也不可能是bootstrap和layui
学习方法
api
demo
本次操作演示需要用到页面:
展示如下:
用api学习:
代码演示
前端
根据easyui控件(上) 代码基础上进行操作演示。
创建userManage页面写入通过api找到需要用到的控件代码<%@ 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">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/userManage.js"></script>
<title>后台管理主界面</title>
</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,buttons:'#bb',closed:true">
<!-- 自定义正则属性 -->
<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>
<div id="bb">
<a href="#" class="easyui-linkbutton" onclick='ok()'>保存</a> <a href="#"
class="easyui-linkbutton">关闭</a>
</div>
</body>
</html> 在demo中找到要用的json把它放入和jsp同级目录下
api查找:分页:pagination
把右边挤满:fitColumns
占满全屏:fit
绑定工具栏按钮:toolbar (搜索icon.css看按钮样式)
Window(窗体)-->dialog(对话框窗口)--->用法;
控件Form表单-->form(表单)-->用法;
获取单行:getSelected
获取多行:getSelections 创建userManage.js文件$(function(){
$('#dg').datagrid({
url:'datagrid_data1.json',
pagination:true,
fitColumns:true,
columns:[[
{field:'uid',title:'代码',width:100},
{field:'uname',title:'名称',width:100},
{field:'upwd',title:'价格',width:100,align:'right'}
]],
toolbar: [
{
iconCls: 'icon-add',
handler: function(){alert('新增按钮')}
},{
iconCls: 'icon-edit',
handler:function(){
$('#dd').dialog('open');
//通过easyui的form控件直接回填选中行的数据
var row=$('#dg').datagrid('getSelected');
if(row){
$('#ff').form('load',row);
}else{
aler("请选择你要修改的数据");
}
}
},'-',{
iconCls: 'icon-remove',
handler: function(){alert('删除按钮')}
}]
});
})
function ok(){
$('#ff').form('submit', {
url:'',
success:function(data){
alert(data)
// 比如说如果返回1代表成功,0代表失败,还有业务逻辑需要处理的话,由前端完成
if(data.code == 1){
}else{
}
}
});
} 运行结果:
后端
编写UserActionpackage com.myy.web;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.myy.dao.UserDao;
import com.myy.framework.ActionSupport;
import com.myy.util.PageBean;
import com.myy.util.ResponseUtil;
public class UserAction extends ActionSupport {
private UserDao userDao = new UserDao();
public String login(HttpServletRequest req, HttpServletResponse resp) {
String code = "index";
// 登录
try {
List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), null);
if (list.size() == 1 && list != null) {
// 用户存在
List<Map<String, Object>> menuList = this.userDao.getMenuseByUser(req.getParameterMap(), null);
StringBuilder sb = new StringBuilder();
for (Map<String, Object> map : menuList) {
sb.append("," + map.get("menuId"));
}
// ,001,002 第一个下标开始截取
req.setAttribute("menuIds", sb.substring(1));
} else {
// 用户不存在
req.setAttribute("msg", "用户不存在");
code = "login";
}
} catch (InstantiationException | IllegalAccessException | SQLException e) {
e.printStackTrace();
code = "login";
}
return code;
}
/**
* easyui的datagrid的数据来源
*
* @param req
* @param resp
* @return
*/
public String list(HttpServletRequest req, HttpServletResponse resp) {
Map<String, Object> map = new HashMap<String, Object>();
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
try {
List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), pageBean);
map.put("total", pageBean.getTotal());
map.put("rows", list);
ObjectMapper om = new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String edit(HttpServletRequest req, HttpServletResponse resp) {
try {
this.userDao.edit(req.getParameterMap());
ObjectMapper om = new ObjectMapper();
Map<String, Object> map = new HashMap<String,Object>();
map.put("code",1);
map.put("msg","成功");
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
} UserDaopackage com.myy.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.myy.util.JsonBaseDao;
import com.myy.util.JsonUtils;
import com.myy.util.PageBean;
import com.myy.util.StringUtils;
public class UserDao extends JsonBaseDao {
/**
* 用于查询用户分页列表所用
* 用于用户登录所用
* @param map
* @param pageBean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<Map<String, Object>> list(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_user_version2 where true ";
String uid = JsonUtils.getParamVal(map, "uid");
String upwd = JsonUtils.getParamVal(map, "upwd");
if(StringUtils.isNotBlank(uid)) {
sql += " and uid = "+uid;
}
if(StringUtils.isNotBlank(upwd)) {
sql += " and upwd = "+upwd;
}
return super.executeQuery(sql, pageBean);
}
/**
* 通过用户登录的唯一账号,在用户父权限中间表中获取菜单ID的集合
* @param map
* @param pageBean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<Map<String, Object>> getMenuseByUser(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_usermenu where true ";
String uid = JsonUtils.getParamVal(map, "uid");
if(StringUtils.isNotBlank(uid)) {
sql += " and uid = "+uid;
}
return super.executeQuery(sql, pageBean);
}
/**
* 修改
* @param map
* @param pageBean
* @return
*/
public int edit(Map<String, String[]> map) throws Exception{
String sql="update t_easyui_user_version2 set uid=?,uname=?,upwd=? where serialno = ? ";
return super.executeUpdate(sql, new String[] {"uid","uname","upwd","SerialNo" },map);
}
} userManage.js$(function(){
$('#dg').datagrid({
url:'../userAction.action?methodName=list',
pagination:true,
fitColumns:true,
columns:[[
{field:'uid',title:'代码',width:100},
{field:'uname',title:'名称',width:100},
{field:'upwd',title:'价格',width:100,align:'right'}
]],
toolbar: [
{
iconCls: 'icon-add',
handler: function(){alert('新增按钮')}
},{
iconCls: 'icon-edit',
handler:function(){
$('#ff').form('clear');
$('#dd').dialog('open');
//通过easyui的form控件直接回填选中行的数据
var row=$('#dg').datagrid('getSelected');
if(row){
$('#ff').form('load',row);
}else{
aler("请选择你要修改的数据");
}
}
},'-',{
iconCls: 'icon-remove',
handler: function(){alert('删除按钮')}
}]
});
})
function ok(){
$('#ff').form('submit', {
url:'../userAction.action?methodName=edit',
success:function(data){
// 将json串转成json对象
var res = eval('('+ data +')');
// 比如说如果返回1代表成功,0代表失败,还有业务逻辑需要处理的话,由前端完成
if(res.code == 1){
$('#dd').dialog('close');
$('#dg').datagrid('reload');
}else{
}
}
});
}
路径放mvc配置的
运行结果
增删改查:
UseDaopackage com.myy.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.myy.util.JsonBaseDao;
import com.myy.util.JsonUtils;
import com.myy.util.PageBean;
import com.myy.util.StringUtils;
public class UserDao extends JsonBaseDao {
/**
* 用于查询用户分页列表所用
* 用于用户登录所用
* @param map
* @param pageBean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<Map<String, Object>> list(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_user_version2 where true ";
String uid = JsonUtils.getParamVal(map, "uid");
String upwd = JsonUtils.getParamVal(map, "upwd");
if(StringUtils.isNotBlank(uid)) {
sql += " and uid = "+uid;
}
if(StringUtils.isNotBlank(upwd)) {
sql += " and upwd = "+upwd;
}
return super.executeQuery(sql, pageBean);
}
/**
* 通过用户登录的唯一账号,在用户父权限中间表中获取菜单ID的集合
* @param map
* @param pageBean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<Map<String, Object>> getMenuseByUser(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_usermenu where true ";
String uid = JsonUtils.getParamVal(map, "uid");
if(StringUtils.isNotBlank(uid)) {
sql += " and uid = "+uid;
}
return super.executeQuery(sql, pageBean);
}
/**
* 修改
* @param map
* @param pageBean
* @return
*/
public int edit(Map<String, String[]> map) throws Exception{
String sql="update t_easyui_user_version2 set uid=?,uname=?,upwd=? where serialno = ? ";
return super.executeUpdate(sql, new String[] {"uid","uname","upwd","SerialNo" },map);
}
/**
* 新增
* @param map
* @return
* @throws Exception
*/
public int add(Map<String, String[]> map) throws Exception{
List<Map<String, Object>> SerialNo = this.SerialNoMax(map);//获取最大唯一标识符
int serialno = Integer.valueOf(SerialNo.toString().substring(16,17))+1;
String sql = "insert into t_easyui_user_version2 values('"+serialno+"',?,?,?)";
return super.executeUpdate(sql, new String [] {"uid","uname","upwd"},map);
}
/**
* 查询最大SerialNo
* @param map
* @return
* @throws Exception
*/
public List<Map<String, Object>> SerialNoMax(Map<String, String [] > map) throws Exception{
String sql = "select max(SerialNo) from t_easyui_user_version2 ";
return super.executeQuery(sql, null);
}
/**
* 删除
* @param map
* @return
* @throws Exception
*/
public int del(Map<String, String[]> map) throws Exception{
String sql="delete from t_easyui_user_version2 where serialno = ? ";
return super.executeUpdate(sql, new String[] {"SerialNo"},map);
}
/**
* 查询
* @param paMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> query(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_user_version2 where true";
String uname = JsonUtils.getParamVal(paMap, "uname");
if (StringUtils.isNotBlank(uname)) {
sql = sql + " and uname like '%" + uname + "%'";
}
return super.executeQuery(sql, pageBean);
}
} web层UserActionpackage com.myy.web;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.myy.dao.UserDao;
import com.myy.framework.ActionSupport;
import com.myy.util.PageBean;
import com.myy.util.ResponseUtil;
public class UserAction extends ActionSupport {
private UserDao userDao = new UserDao();
public String login(HttpServletRequest req, HttpServletResponse resp) {
String code = "index";
// 登录
try {
List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), null);
if (list.size() == 1 && list != null) {
// 用户存在
List<Map<String, Object>> menuList = this.userDao.getMenuseByUser(req.getParameterMap(), null);
StringBuilder sb = new StringBuilder();
for (Map<String, Object> map : menuList) {
sb.append("," + map.get("menuId"));
}
// ,001,002 第一个下标开始截取
req.setAttribute("menuIds", sb.substring(1));
} else {
// 用户不存在
req.setAttribute("msg", "用户不存在");
code = "login";
}
} catch (InstantiationException | IllegalAccessException | SQLException e) {
e.printStackTrace();
code = "login";
}
return code;
}
/**
* easyui的datagrid的数据来源
*
* @param req
* @param resp
* @return
*/
public String list(HttpServletRequest req, HttpServletResponse resp) {
Map<String, Object> map = new HashMap<String, Object>();
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
try {
List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), pageBean);
map.put("total", pageBean.getTotal());
map.put("rows", list);
ObjectMapper om = new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 查询
*
* @param req
* @param resp
* @return
*/
public String query(HttpServletRequest req, HttpServletResponse resp) {
try {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
List<Map<String, Object>> list = this.userDao.query(req.getParameterMap(), pageBean);
ObjectMapper om = new ObjectMapper();
Map<String, Object> map = new HashMap<>();
map.put("total", pageBean.getTotal());
map.put("rows", list);
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
e.printStackTrace();
}
return "index";
}
/**
* 修改
*
* @param req
* @param resp
* @return
*/
public String edit(HttpServletRequest req, HttpServletResponse resp) {
try {
this.userDao.edit(req.getParameterMap());
ObjectMapper om = new ObjectMapper();
Map<String, Object> map = new HashMap<String, Object>();
map.put("code", 1);
map.put("msg", "成功");
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 新增
*
* @param req
* @param resp
* @return
*/
public String add(HttpServletRequest req, HttpServletResponse resp) {
try {
this.userDao.add(req.getParameterMap());
ObjectMapper om = new ObjectMapper();
Map<String, Object> map = new HashMap<String, Object>();
map.put("code", 1);
map.put("msg", "成功");
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 删除
*
* @param req
* @param resp
* @return
*/
public String del(HttpServletRequest req, HttpServletResponse resp) {
try {
int del = this.userDao.del(req.getParameterMap());
ObjectMapper om = new ObjectMapper();
Map<String, Object> map = new HashMap<String, Object>();
map.put("code", 1);
map.put("msg", "成功");
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
} 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">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/userManage.js"></script>
<title>后台管理主界面</title>
</head>
<body>
<div id="tb">
<input type="text" id="qq">
<button >搜索</button>
</div>
<table id="dg"></table>
<input type="hidden" id="myy"
value="${pageContext.request.contextPath }">
<!-- 新增修改对应的编辑窗体 -->
<div id="dd" class="easyui-dialog" title="编辑窗体"
style="width: 400px; height: 200px;"
data-options="iconCls:'icon-save',resizable:true,modal:true,buttons:'#bb',closed:true">
<!-- 自定义正则属性 -->
<form id="ff" method="post">
<input type="hidden" name="SerialNo" id="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>
<div id="bb">
<a href="#" class="easyui-linkbutton" onclick='ok()' >保存</a>
<a href="#" class="easyui-linkbutton" >关闭</a>
</div>
</body>
</html> userManage.js文件$(function() {
$('#dg').datagrid({
url : $("#myy").val() + '/userAction.action?methodName=list',
pagination : true,
fitColumns : 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');//打开窗体
}
}, {
iconCls : 'icon-edit',
handler : function() {
$('#dd').dialog('open');
// 通过easyui的form控件直接回填选中行的数据
var row = $('#dg').datagrid('getSelected');
if (row) {
$('#ff').form('load', row);
} else {
alert("请选择你要修改的数据");
}
}
}, '-', {
iconCls : 'icon-remove',
handler : function() {
var row = $('#dg').datagrid('getSelected');
if (null == row) {
$.messager.alert('警告', '请选中行');
return false;
}
$.messager.confirm('确认', '您确认要删除记录吗', function(r) {
if(r){
$.ajax({
url:$("#myy").val()+'/userAction.action?methodName=del&SerialNo='+row.SerialNo,
success: function(param){
//重新载入页面
$('#dg').datagrid('reload');
}
})
}
})
}
} ]
});
})
// 新增或修改
function ok() {
var seturl = '../userAction.action?methodName=edit';
var SerialNo = $('#SerialNo').val();
if(SerialNo == null || SerialNo == ""){
seturl = '../userAction.action?methodName=add';
}
$('#ff').form('submit', {
url :seturl,
success : function(data) {
// 将json串转成json对象
var res = eval('(' + data + ')');
// 比如说如果返回1代表成功,0代表失败,还有业务逻辑需要处理的话,由前端完成
if (res.code == 1) {
$('#dd').dialog('close');
$('#dg').datagrid('reload');
} else {
}
}
});
}
// 关闭
function no() {
$('#dd').dialog('close');
}
// 查询
function qq() {
var query = $("#qq").val();
$("#dg").datagrid(
{
url : $("#myy").val()
+ '/userAction.action?methodName=query&uname=' + query
});
} 页面运行结果
增加
点击删除没有选中:
选中删除:
删除成功:
修改:
修改成功:
根据名字模糊查询:
|