easyui高级控件02

1.easyui的crud(dialog,datagrid、form讲解)

陈旧的开发模式
美工(ui工程师:出一个项目模型)
java工程师:将原有的html转成jsp,动态展示数据
缺点:
客户需要调节前端的展示效果
解决:由美工去重新排版,重新选色。
Vs
前后端分离
美工、java工程师都是独立工作的,彼此之间在开发过程中是没有任何交际。
在开发前约定数据交互的格式。
java工程师的工作:写方法返回数据如tree_data1.json
美工:只管展示tree_data1.json

{"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"},
	{"uid":"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"}
]}

2、通用的JsonBaseDao增删改方法

package com.houyitao.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;


import com.houyitao.util.JsonBaseDao;
import com.houyitao.util.JsonUtils;
import com.houyitao.util.PageBean;
import com.houyitao.util.StringUtils;

import com.houyitao.util.JsonBaseDao;

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 SQLException
 * @throws NoSuchFieldException
 * @throws SecurityException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
	public int del(Map<String, String[]> paMap) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		String sql="delete from t_easyui_user_version2 where uid=?";
		return super.executeUpdate(sql, new String[] {"uid"},paMap);
	}
	/**
	 * 增加
	 * @param paMap
	 * @return
	 * @throws SQLException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public int add(Map<String, String[]> paMap) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		String sql="insert into t_easyui_user_version2 values(?,?,?)";
		return super.executeUpdate(sql, new String[] {"uid","uname","upwd"},paMap);
	}
	
	//用当前用户id查找menuid
	public List<Map<String,Object>> listMenu(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");
		if (StringUtils.isNotBlank(uid)) {
			sql += " and uid = "+uid;
		}
		return super.executeQuery(sql, pageBean);
		
	}
}


3、web层

package com.houyitao.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.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.houyitao.dao.UserDao;
import com.houyitao.entity.TreeNode;
import com.houyitao.util.PageBean;
import com.houyitao.util.ResponseUtil;
import com.zking.framework.ActionSupport;


public class UserAction extends ActionSupport{
	private UserDao u=new UserDao();
	
	public String login(HttpServletRequest req, HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException {
		Map<String,Object> map=null;
		try {
			try {
                map=this.u.list(req.getParameterMap(),null).get(0);
			} catch (Exception e) {
				req.setAttribute("msg","...");
				return "login";
			}
			if (map!=null && map.size() > 0) {
				StringBuilder sb=new StringBuilder();
					List<Map<String,Object>> menIdArr=this.u.listMenu(req.getParameterMap(),null);
				for (Map<String, Object> m : menIdArr) {
				sb.append(","+m.get("menuId"));
				}
				req.setAttribute("menuIds", sb.substring(1));
				return "index";
				}else {
					req.setAttribute("msg","。。。");
					return "login";
				}
		} catch (Exception e) {
			}
		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.u.list(req.getParameterMap(), pageBean);
			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组件提交方法
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception 
	 * @throws JsonProcessingException 
	 */
	public String edit(HttpServletRequest req, HttpServletResponse resp) {
		try {
			int code = this.u.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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public String add(HttpServletRequest req, HttpServletResponse resp) {
		try {
			int code = this.u.add(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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String del(HttpServletRequest req, HttpServletResponse resp) {
		try {
			int code = this.u.del(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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}



4.js页面

$(function() {
	$('#dg').datagrid({    
	    url:'../userAction.action?methodName=list',    
	    fit:true,
	    fitColumns:true,
	    pagination:true,
	    columns:[[    
	        {field:'uid',title:'id',width:100},    
	        {field:'uname',title:'用户名',width:100},    
	        {field:'upwd',title:'密碼',width:100,align:'right'}    
	    ]] ,
	    toolbar: [{
			iconCls: 'icon-edit',
			handler: function(){
				$('#dd').dialog('open');
				var row = $('#dg').datagrid('getSelected');
				if(row){
					$("#ff").form('load',row);
				}
			}
		},'-',{
			iconCls: 'icon-add',
			handler: function(){
				$('#dd').dialog('open');
				

			}
		},'-',{
			iconCls: 'icon-remove',
			handler: function(){
				
			}
		}]

	});  
	
})
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)   
	        //针对于后端返回的结果进行处理
	    }    
	});  

}

5.主界面

<%@ 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/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'">

		<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="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> -->
<div id="bb">
<a href="#" class="easyui-linkbutton" onclick="ok()">保存</a>
<a href="#" class="easyui-linkbutton">关闭</a>
</div>




</body>
</html>

7、功能完善
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值