秀一下我多天才做出来的Ext实例:用户管理的增、删、改、查。只是实现了,没有考虑项目框架的构建。有好方法的给提一下。
用户列页面userlist.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ include file="/include/taglibs.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><tiles:getAsString name="project_name" /></title>
</head>
<body>
<script type="text/javascript">
Ext.onReady(function(){
// 使用表单提示
Ext.QuickTips.init();
// turn on validation errors beside the field globally
Ext.form.Field.prototype.msgTarget = 'side';
var sm=new Ext.grid.CheckboxSelectionModel();
var cm=new Ext.grid.ColumnModel([
new Ext.grid.RowNumberer(),
sm,
{header:'用户名',width:120,align:'center',dataIndex:'userName'},
{header:'姓名',width:120,align:'center',dataIndex:'realName'},
{header:'角色',width:100,align:'center',dataIndex:'roleName'},
{header:'创建日期',width:150,align:'center',renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'),dataIndex:'createTime'}
]);
cm.defaultSortable=true;
var ds=new Ext.data.Store({
//proxy:new Ext.data.MemoryProxy(data),
proxy:new Ext.data.ScriptTagProxy({url:'UserListAction.do'}),
reader:new Ext.data.JsonReader(
{totalProperty:'totalProperty',
root:'user'
},
[{name:'id',mapping:'userId'},
{name:'userName'},
{name:'realName'},
{name:'roleName'},
{name:'createTime',type: 'date', dateFormat: 'Y-m-d H:i:s'}
])
});
var searchbar=new Ext.Toolbar({
id:'searchbar',
items:['用户名',
new Ext.form.TextField({
id:'userName',
width:100
}),' ',
new Ext.Toolbar.Button({
id:'searchbutton',
text:'查询',
handler:doSearch
})
]
});
function doSearch(){
ds.load(
{params:{
start:0,
limit:10,
userName:Ext.get('userName').dom.value
}});
}
var paging=new Ext.PagingToolbar({
pageSize:10,
store:ds,
displayInfo:true,
displayMsg:'显示第{0}条到{1}条记录,一共{2}条',
items:
["-"," ",
{text:'添加',
tooltip:'添加一条系统用户信息',
handler:function(){
window.location="UserListNaviAction.do?action=UserAdd";
}
},
"-"," ",
{text:'修改',
tooltip:'修改一条系统用户信息',
handler:function(){
var _record=grid.getSelectionModel().getSelected();
if(sm.getCount()==1){
window.location='UserListNaviAction.do?action='+'UserEdit'+'&userId='
+_record.get('id');
}else
Ext.Msg.alert('提示', '请选择一条记录!');
}
},
"-"," ",{text:'删除',
tooltip:'删除选择的系统用户信息',
handler:function(){
if(sm.hasSelection()){
Ext.MessageBox.confirm('提示','你确定要删除该用户信息吗?',
function(button){
if(button=='yes'){
var list=sm.getSelections();
var jsonData="";
for(var i=0;i<list.length;i++){
var id=list[i].data["id"];
if(i==0){
jsonData=jsonData+id;
}else{
jsonData=jsonData+","+id;
}
}
window.location='UserListNaviAction.do?action='+'UserDelete'+'&delIds='
+jsonData;
}
}
);
}else{
Ext.Msg.alert('提示', "请至少选择一条记录!");
}
}
}
],
emptyMsg:"查询记录为空!"
});
var grid=new Ext.grid.EditorGridPanel({
renderTo:'content',
cm:cm,
ds:ds,
sm:sm,
tbar:searchbar,
bbar:paging,
height:400,
bodyStyle:'width:100%',
stripeRows: true,
title:'用户管理'
});
ds.load({params:{start:0, limit:10}});
});
</script>
<div id="content" style="width:1100px;padding:10px;"></div>
</body>
struts Action类UserListAction.java:
package actions.common.user;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;
import org.json.JSONArray;
import domain.common.TbUser;
import service.common.UserService;
/**
* 用户列表Action.
* @author jinyy
*/
public class UserListAction extends Action {
/**
* Logger for this class.
*/
private static final Logger logger = Logger.getLogger(UserListAction.class);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK);
/**
* UserService.
*/
private UserService userService;
/**
* @param mapping struts mapping
* @param request request
* @param form action form
* @param response response
* @return action forward
* @throws Exception any exception
*/
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String userName = request.getParameter("userName");
String start = request.getParameter("start");
String limit = request.getParameter("limit");
int index=0;
int pageSize=10;
if (start != null) {
index = Integer.parseInt(start);
pageSize = Integer.parseInt(limit);
}
logger.info("list user - User Name: " + userName);
String delIds = request.getParameter("delIds");
if (delIds != null && !"".equals(delIds)) {
String[] ids = delIds.split(",");
for (String id : ids) {
userService.delete(Integer.parseInt(id));
}
}
// 调用service方法
List<TbUser> userList = userService.queryLists(userName);
Integer count = userList.size();
boolean scriptTag = false;
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("GBK");
String cb = request.getParameter("callback");
if (cb != null) {
scriptTag = true;
response.setContentType("text/javascript");
} else {
response.setContentType("application/x-json");
}
PrintWriter out = response.getWriter();
if (scriptTag) {
out.write(cb + "(");
}
JSONObject object = new JSONObject();
JSONArray js = new JSONArray();
try {
Object[] users = userList.toArray();
for (int i = index; (i < pageSize + index) && (count - 1 >= i); i++) {
JSONObject objTemp = new JSONObject();
objTemp.put("userName", ((TbUser) users[i]).getUserName());
objTemp.put("userId", ((TbUser) users[i]).getUserId());
objTemp.put("realName", ((TbUser) users[i]).getRealName());
objTemp.put("roleName", ((TbUser) users[i]).getRoleName());
if (((TbUser) users[i]).getCreateTime() != null) {
objTemp.put("createTime", sdf.format(((TbUser) users[i]).getCreateTime()));
}
js.put(objTemp);
}
object.put("user", js);
object.put("totalProperty", count);
out.print(object.toString());
if (scriptTag) {
out.write(");");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param pUserService the userService to set
*/
public void setUserService(UserService pUserService) {
userService = pUserService;
}
}