截图
1、JAVA代码
package org.chris.module; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.chris.model.DutyModel; import org.chris.service.DutyService; import org.chris.utils.StringUtils; import org.nutz.ioc.Ioc; import org.nutz.ioc.loader.annotation.Inject; import org.nutz.ioc.loader.annotation.IocBean; import org.nutz.mvc.annotation.At; import org.nutz.mvc.annotation.Ok; @IocBean() public class DutyModule { @Inject private DutyService dutyService; @At("/duty/save") @Ok("json") // 返回形式是jsp public void save(HttpServletRequest request) { try { String id = request.getParameter("id"); String name = request.getParameter("name"); String orderNum = request.getParameter("orderNum"); DutyModel model = new DutyModel(StringUtils.toInt(id),name,StringUtils.toInt(orderNum)); this.dutyService.save(model); } catch (RuntimeException e) { e.printStackTrace(); } } @At("/duty/delete") @Ok("json") // 返回形式是jsp public void delete(HttpServletRequest request) { try { String[] ids = request.getParameter("ids").split(","); this.dutyService.delete(ids); } catch (RuntimeException e) { e.printStackTrace(); } } /** * 加载菜单 * @author Chris Suk * @date 2014-4-22 下午10:13:57 * @param request * @param ioc */ @At("/duty/init") @Ok("json") // 返回形式是jsp public Map<String, Object> init(HttpServletRequest request, Ioc ioc) { Map<String, Object> map = new HashMap<String, Object>(); try { List<DutyModel> list = this.dutyService.init(); map.put("total", String.valueOf(list.size())); map.put("rows", list); } catch (Exception e) { e.printStackTrace(); } return map; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ include file="../../inc/include.jsp" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>职务维护</title> <link rel="stylesheet" type="text/css" href="../../plugins/jquery.easyui.1.3.6/themes/default/easyui.css"> <link rel="stylesheet" type="text/css" href="../../plugins/jquery.easyui.1.3.6/themes/icon.css"> <script type="text/javascript" src="${thisPath}/plugins/jquery.easyui.1.3.6/jquery.min.js"></script> <script type="text/javascript" src="${thisPath}/plugins/jquery.easyui.1.3.6/jquery.easyui.min.js"></script> <script type="text/javascript" src="${thisPath}/js/Common.js"></script> <script> $(function(){ $('#tt').datagrid({pagination: true}); }); </script> </head> <body> <table id="tt" width="100%" height="auto" data-options=" iconCls:'icon-edit', toolbar: '#tb', singleSelect:true, idField:'id', fitColumns:true, url:'${thisPath}/duty/init.do', onClickRow: onClickRow " title="职务维护"> <thead> <tr> <th data-options="field:'name',width:100,editor:'text'">职务名称</th> <th data-options="field:'orderNum',width:100,editor:'numberbox'">职务序号</th> </tr> </thead> </table> <div id="tb" style="height:auto"> <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()">添加</a> <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="remove()">删除</a> <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()">保存</a> </div> <script type="text/javascript"> var editIndex = undefined; function onClickRow(index){ if (editIndex != index){ endEditing(); $('#tt').datagrid('selectRow', index).datagrid('beginEdit', index); editIndex = index; } } function endEditing(){ var rows = $('#tt').datagrid('getRows'); for ( var i = 0; i < rows.length; i++) { $('#tt').datagrid('endEdit', i); } } function append(){ endEditing(); $('#tt').datagrid('appendRow',{status:'P'}); editIndex = $('#tt').datagrid('getRows').length-1; $('#tt').datagrid('selectRow', editIndex).datagrid('beginEdit', editIndex); } function remove(){ $.messager.confirm('操作提示', '您确定要删除选中记录吗?', function(r){ if (r){ var rows = $('#tt').datagrid('getSelected'); var ids = rows.id; $.post('${thisPath}/duty/delete.do',{ids:ids},function(){ $.msg('删除成功'); }); $('#tt').datagrid('reload'); } }); } function accept(){ endEditing(); var obj = $('#tt').datagrid('getChanges')[0]; $.post('${thisPath}/duty/save.do',obj,function(){ $.msg('保存成功'); }); $('#tt').datagrid('reload'); } </script> </body> </html>
3、Service接口
package org.chris.service; import java.util.List; import org.chris.model.DutyModel; public interface DutyService { /** * 添加 * @author Chris Suk * @date 2014-4-24 下午08:41:51 * @param model */ public void save(DutyModel model); /** * 删除 * @author Chris Suk * @date 2014-4-24 下午08:41:51 * @param ids */ public void delete(String...ids); /** * 加载 * @author Chris Suk * @date 2014-4-24 下午08:43:10 * @return */ public List<DutyModel> init(); }
package org.chris.service.impl; import java.util.List; import org.chris.model.DutyModel; import org.chris.service.DutyService; import org.nutz.dao.Cnd; import org.nutz.ioc.loader.annotation.IocBean; import org.nutz.service.NameEntityService; @IocBean(name="dutyService", fields={"dao"}) public class DutyServiceImpl extends NameEntityService<DutyModel> implements DutyService { public void delete(String... ids) { this.dao().clear(DutyModel.class, Cnd.where("id", "in", ids)); } public List<DutyModel> init() { return this.dao().query(DutyModel.class, null); } public void save(DutyModel model) { DutyModel query = this.dao().fetch(DutyModel.class, model.getId()); if(null!=query){ this.dao().update(model); }else{ this.dao().insert(model); } } }