JqueryEasyUI datagrid通過struts請求從後台讀取資料,jqueryeasyuistruts
我就說下需要注意的事項,希望能協助到大家:1.後台返回的格式必須是JqueryEasyUI那種格式。比如:{"total":2,"rows":[{...}]"}這種形式。2.struts設定檔無須配置result,直接簡單配置就行,如下: <!-- 使用者列表--> <action name="userList" method="userList" class="com.duosen.ocs.system.action.UserAction"> </action>
3.action中方法這麼寫,直接返回success: result = userService.findPageUserList(this.getPage(), this.getLimit(), user); //{total=2, rows=[com.duosen.ocs.system.domain.User@151685f, com.duosen.ocs.system.domain.User@19b60bb]}
JSONObject object = JSONObject.fromObject(result); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("html/text;charset=UTF-8"); response.getWriter().write(object.toString());
return null;
這樣前台返回資料格式為:
前台展現:
附上使用者管理jsp代碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/common/include.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<h2>CRUD DataGrid</h2>
<p>Double click the row to begin editing.</p>
<table id="dg" title="My Users" style="width:700px;height:250px"
toolbar="#toolbar" pagination="true" idField="id"
rownumbers="true" fitColumns="true" singleSelect="true">
<thead>
<tr>
<th field="loginname" width="50" editor="{type:'validatebox',options:{required:true}}">使用者名稱</th>
<th field="password" width="50" editor="{type:'validatebox',options:{required:true}}">密碼</th>
<th field="mobile" width="50" editor="text">電話</th>
<th field="createtime" width="50" editor="{type:'validatebox',options:{validType:'email'}}">建立時間</th>
</tr>
</thead>
</table>
<div id="toolbar">
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:$('#dg').edatagrid('addRow')">New</a>
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:$('#dg').edatagrid('destroyRow')">Destroy</a>
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="javascript:$('#dg').edatagrid('saveRow')">Save</a>
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-undo" plain="true" onclick="javascript:$('#dg').edatagrid('cancelRow')">Cancel</a>
</div>
<script type="text/javascript">
$(function(){
$('#dg').edatagrid({
url: window.BIZCTX_PATH + '/system/userjson/userList.action',
saveUrl: 'save_user.php',
updateUrl: 'update_user.php',
destroyUrl: 'destroy_user.php'
});
});
</script>
</body>
</html>
include.jsp的代碼:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<%@ page import="com.duosen.ocs.constant.WebConstants" %>
<%@ page import="com.duosen.ocs.system.domain.User" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<script type="text/javascript">
window.BIZCTX_PATH = "<%=path%>";
var bizctxpath = "<%=path%>";
</script>
<link rel="stylesheet" type="text/css" href="<%=path%>/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/easyui/themes/color.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/easyui/demo/demo.css">
<script type="text/javascript" src="<%=path%>/easyui/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="<%=path%>/css/common.css" />
<script type="text/javascript" src="<%=path%>/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=path%>/resource/js/fmscookie.js"></script>
<script type="text/javascript" src="<%=path%>/easyui/jquery.edatagrid.js"></script>
以上js都是jqueryEasyUi官方下載。