EasyUi高级布局(三)
程序员文章站
2024-02-04 11:09:16
...
今天我就说一说咋们的一个前后端分离,在很多的一些企业里面我们所做的项目都是前后端分离的,也就是说我们前面台展示的界面和我们的数据操控不是同一个人写的,而是两个部门。当然在一些小型的企业里一个人既要干前端的是也要干后台的事。今天我们就来学习一一些简单的前台和后台。
1.前台
我们的前台界面是用easyUI进行开发的,虽然界面是丑了点,但是并不影响我们的开发。蛤蟆虽小五脏俱全。哈哈哈 easyUI 的开发我们还是需要了解 easyUI的apl的这里我提供了apl,仅供参考
http://www.92ui.net/
1.1,数据表格
这里我们展示了前台界面的数据表单,里面所展示的数据是来自json的
<%@ 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/userManage.js"></script>
</head>
<body>
<!-- 表单 -->
<form id="ff" method="post">
<input type="hidden" id='ctx' value='${pageContext.request.contextPath }'>
<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" />
</div>
<div>
<label for="upwd">upwd:</label>
<input class="easyui-validatebox" type="text" name="upwd" />
</div>
</form>
</div>
</body>
</html>
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"}
]}
我们前面有引入js代码,下面就是我们的js代码
$(function(){
$('#dg').datagrid({
url:$('#ctx').val()+'/userAction.action?methodName=list', //我们的绝对路径
fitColumns:true,//填充列
fit:true,//填充行
pagination:true,//分页
columns:[[ //field 与数据库的列名保持一致 , title头名
{field:'uid',title:'代码',width:100},
{field:'uname',title:'名称',width:100},
{field:'upwd',title:'价格',width:100,align:'right'}
]],
});
})
界面展示
1.2,弹出框
下面的一些代码数据我就直接加入jsp界面了
<div id="dd" class="easyui-dialog" title="My Dialog" style="width:400px;height:200px;"
data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true,buttons:'#bb'">
</div>
1.3,表单
表单我直接放在了弹出框里面
<!--弹出框-->
<div id="dd" class="easyui-dialog" title="My Dialog" 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" 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" />
</div>
<div>
<label for="upwd">upwd:</label>
<input class="easyui-validatebox" type="text" name="upwd" />
</div>
</form>
</div>
2.后台
增删查改
dao方法这里就展示一个增加的到方法了,因为其他的到方法与这个差不多
我们没有增加他的主键,因为主键是自动递增的,里面的数据都是从jsp界面传过来到
dao层
public int add(Map<String, String[]> paMap) throws NoSuchFieldException, 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);
}
子控制器
public String add(HttpServletRequest req, HttpServletResponse resp) {
try {
int edit = this.userDao.add(req.getParameterMap());//调用增加方法
ObjectMapper om = new ObjectMapper();//
ResponseUtil.write(resp, om.writeValueAsString(edit));//转json格式
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
接下来就是我们的核心js代码了
$(function(){
$('#dg').datagrid({
url:$('#ctx').val()+'/userAction.action?methodName=list',
fitColumns:true,//填充列
fit:true,//填充行
pagination:true,//分页
columns:[[ //field 与数据库的列名保持一致 , title头名
{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');//返回第一个被选中的行或如果没有选中的行则返回null。
if(row){
$('#ff').form('load',row);//给表单赋值
$('#dd').dialog('open');//打开弹出框
$("#str").val("edit");//给ID是str的input赋值,这里赋的值要与子控制器里的方法保持一致
}else{
alert("亲您还没有选中行哟!!");
}
}
},'-',{
iconCls: 'icon-add',
handler: function(){//新增
$('#ff').form('clear');//清除表单数据。
$('#dd').dialog('open');
$("#str").val("add");//赋值
}
},'-',{
iconCls: 'icon-remove',
handler: function(){//删除
var row = $("#dg").datagrid('getSelected');//返回第一个被选中的行或如果没有选中的行则返回null。
if(row){
$.ajax({//使用ajax提交
url:$('#ctx').val()+'/userAction.action?methodName=del$$SerialNo='row.SerialNo,//提交地址,传了两个参数,根据SerialNo主键进行删除
success: function(param){
$('#dg').datagrid('reload');
}
})
}
}
},'-',{//无实际意义
iconCls: 'icon-xxx',
handler: function(){alert('帮助按钮')}
}]
});
})
function ok(){//这里是我们的修改和增加提交表单所用
$('#ff').form('submit',{
url:$('#ctx').val()+'/userAction.action?methodName='+$('#str').val(),
success: function(param){
$('#dd').dialog('close');//提交成功,关闭弹框
$('#dg').datagrid('reload');//重新载入当前页面数据
$('#ff').form('clear');//清除表单数据。
}
});
}
接下来就是成果展示啦
成功了哟!!!!
如果这个小东西对你有帮助的话,就麻烦您动以下小手点亮以下吧!!!谢谢支持
上一篇: Linux 下安装MySQL 启动错误