Easyui(三)之高级控件

本文详细介绍使用EasyUI实现前后端分离的开发模式,包括页面布局、数据交互、增删改查操作及代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Easyui(三)之高级控件

说到easyui我们就不得不了解一下,easyui过去与现在的两种开发模式

以前的开发模式:

美工(ui工程师:出一个项目模型)
java工程师:将原有的html转成jsp,动态展示数据
缺点:
客户需要调节前端的展示效果
解决:由美工去重新排版,重新选色,java工程师工程师再次去转换完成

前后端分离

美工、java工程师都是独立工作的,彼此之间在开发过程中是没有任何交际。
在开发前约定数据交互的格式。
java工程师的工作:写方法返回数据如tree_data1.json
美工:只管展示tree_data1.json

而我们今天就将easyui前后端分离
首先我们先写一个前端jsp页面并且在前台进行简单的
增删查改

案列事列如下:

{"total":28,"rows":[
	{"uid":"FI-SW-01","uname":"Koi","upwd":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
	{"uid":"K9-DL-01","uname":"Dalmation","upwd":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
	{"uid":"RP-SN-01","uname":"Rattlesnake","upwd":12.00,"status":"P","listprice":38.50,"attr1":"Venomless","itemid":"EST-11"},
	{"uid":"RP-SN-01","uname":"Rattlesnake","upwd":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
	{"uid":"RP-LI-02","uname":"Iguana","upwd":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
	{"uid":"FL-DSH-01","uname":"Manx","upwd":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
	{"uid":"FL-DSH-01","uname":"Manx","upwd":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
	{"productlid":"FL-DLH-02","uname":"Persian","upwd":12.00,"status":"P","listprice":23.50,"attr1":"Adult Female","itemid":"EST-16"},
	{"uid":"FL-DLH-02","uname":"Persian","upwd":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
	{"uid":"AV-CB-01","uname":"Amazon Parrot","upwd":92.00,"status":"P","listprice":63.50,"attr1":"Adult Male","itemid":"EST-18"}
]}

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>Insert title here</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/easyui5/themes/default/easyui.css">   
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/easyui5/themes/icon.css">   
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/easyui5/jquery.min.js"></script>   
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/easyui5/jquery.easyui.min.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/userManger.js"></script> 




</head>
<body>
<!-- 展示数据 -->
<table id="dg"></table> 
<input type="text" id="ctx" 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,closed:true,buttons:'#bb'">   
      <form id="ff" method="post"> 
      <input type="hidden" id="a">
      <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>
	<!--左下反方按钮-->
   <!--  <div id="tb">
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true"/a>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-help',plain:true"/a>
	</div> -->
</body>
</html>

js页面

$(function(){
	$('#dg').datagrid({
	 url:$("#ctx").val()+'/userAction.action?methodName=list',  
	 fitColumns:true,//把行填充满
	 fit:true,//列填充
	 pagination:true,//是分页
	    columns:[[ //field的值是与数据库保持一致
	        {field:'uid',title:'代码',width:100},    
	        {field:'uname',title:'名称',width:100},    
	        {field:'upwd',title:'价格',width:100,align:'right'}    
	    ]],
	    toolbar: [{
	    	//修改增删改的图标
			iconCls: 'icon-edit',
			handler: function(){
				var row=$('#dg').datagrid('getSelected');
				if(row){
				//调用edit的方法
					$("#a").val("edit");
					//数据回显
					$('#ff').form('load',row);
					$('#dd').dialog('open');
					
				}
			
			}
		},'-',{
			iconCls: 'icon-add',
			handler: function(){
			//清楚表单内容
				$('#ff').form('clear');
				//调用增加方法
				$("#a").val("add");
				$('#dd').dialog('open');
			}
		},'-',{
			iconCls: 'icon-remove',
			handler: function(){
			//无论选中几个 只选中表单的第一行
			var row=$('#dg').datagrid('getSelected');
			if(row){
			$.ajax({
			//绝对路径 加上我要调用的路径 再加上我选中的第一行的id值
				url:$("#ctx").val()+'/userAction.action?methodName=del&&SerialNo='+row.SerialNo,  
				 success: function(param){ 
				    	$('#dg').datagrid('reload'); 
				 }    
			});
			}
			}
		}]

	});  

})

//连接dao方法的函数
function ok(){
	$('#ff').form('submit', {   
	//绝对路径加上我们要要调用方法的路径 加上jsp隐藏域传的值 
	    url:$("#ctx").val()+'/userAction.action?methodName='+$("#a").val(),    
	    success: function(param){ 
	    	$('#dg').datagrid('reload'); 
	    	$('#ff').form('clear');
	    	$('#dd').dialog('close');
	    }    
	});  
}


写好了页面接下来我们就来写dao方法
JsonBaseDao

public class UserDao extends JsonBaseDao {
	/**
	 * 登录查询用户表  登录
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	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=sql+" and uid = "+uid;
		}
		if(StringUtils.isNotBlank(upwd)) {
			sql=sql+" and upwd = "+upwd;
		}
		
		return super.executeQuery(sql, pageBean);
	}
	
	
	/**
	 * 通过中间表  查询登录所对应的权限
	 * @param paMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> listMenu(String uid,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_usermenu where true";
		if(StringUtils.isNotBlank(uid)) {
			sql=sql+" and uid = "+uid;
		}
		
		return super.executeQuery(sql, pageBean);
	}
	
	
	/**
	 * 修改
	 * @param paMap
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 */
	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 add(Map<String, String[]>paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		String sql="insert into t_easyui_user_version2 (uid,uname,upwd) value(?,?,?)";
		return super.executeUpdate(sql, new String[] {"uid","uname","upwd"}, paMap);
	}

	/**
	 * 删除
	 * @param paMap
	 * @return
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public int del(Map<String, String[]>paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		String sql="delete from t_easyui_user_version2 where SerialNo=?";
		return super.executeUpdate(sql, new String[] {"SerialNo"}, paMap);
	}
}

接下来我们再写一个处理器降低耦合度
ActionSupport

public class UserAction extends ActionSupport {
	private UserDao userDao=new UserDao();
	//传两个参数并且动态调用
	public String login(HttpServletRequest req,HttpServletResponse resp) {
		try {
			List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), null);
			if(list!=null&&list.size()>0) {
				List<Map<String, Object>> listMenu = this.userDao.listMenu(req.getParameter("uid"), null);
				StringBuffer sb=new StringBuffer();
				for (Map<String, Object> map : listMenu) {
					sb.append(","+map.get("menuId"));
				}
				//,001,002
				req.setAttribute("menuHid", sb.substring(1));
			}else {
				return "login";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "index";
		
	}
	
	
	
	/**
	 * datagrid所需数据后台程序员开发完毕
	 * 从数据库查找数据
	 * @param req
	 * @param resp
	 * @return
	 */
	public String list(HttpServletRequest req,HttpServletResponse resp) {
		try {
			PageBean pageBean =new PageBean();
			//pageBean 的初始化
			pageBean.setRequest(req);
			List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), pageBean);
			ObjectMapper om=new ObjectMapper();
			//把数据库的值转化成json可以识别的
			Map<String, Object> map=new HashMap<>();
			map.put("total", pageBean.getTotal());
			map.put("rows", list);
			ResponseUtil.write(resp, om.writeValueAsString(map));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	
	//form组件所需数据后台程序员处理完毕
	/**从数据库修改数据
	 * 调用ok
	 * @param req
	 * @param resp
	 * @return
	 */
	public String edit(HttpServletRequest req,HttpServletResponse resp) {
		try {
			int edit=this.userDao.edit(req.getParameterMap());
			ObjectMapper om=new ObjectMapper();
			//param相当于
			ResponseUtil.write(resp, om.writeValueAsString(edit));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//相当于Aja不需要页面的跳转
		return null;
		
	}
	
	
	
	
	/**增加
	 * 调用ok
	 * @param req
	 * @param resp
	 * @return
	 */
	public String add(HttpServletRequest req,HttpServletResponse resp) {
		try {
			int add=this.userDao.add(req.getParameterMap());
			ObjectMapper om=new ObjectMapper();
			//param相当于
			ResponseUtil.write(resp, om.writeValueAsString(add));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//相当于Aja不需要页面的跳转
		return null;
		
	}

	
	public String del(HttpServletRequest req,HttpServletResponse resp) {
		try {
			int del=this.userDao.del(req.getParameterMap());
			ObjectMapper om=new ObjectMapper();
			//param相当于
			ResponseUtil.write(resp, om.writeValueAsString(del));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//相当于Aja不需要页面的跳转
		return null;
	}
}

页面展示如下:
在这里插入图片描述
//把花花改为花花宇宙第一美
在这里插入图片描述
接下来我们再增加一个宇宙第二美
通过对比图发现我们增加成功
在这里插入图片描述
我们再来删除宇宙第一美
删除成功页面上没有花花宇宙第一美啦
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值