标签:
效果图如下所示:
包的结构图如下(面向接口编程):
view层代码如下所示:
instanceList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/easyui/easyui.jsp"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script type="text/javascript"> var dataGridTable,materiTypeLst,ioutTypeLst; var myDialog; //--DOM加载完毕-- $(function(){ dataGridTable = $(‘#ecs_list‘); myDialog=$(‘#ecs_window‘); initDataGrid(); //--加载DATAGRID-- initDialog(); //--加载DIALOG-- initCreatorDialog();//--加载用户Dialog-- if(‘0‘==‘${param.state}‘){ $(‘#ecs_list‘).datagrid({toolbar:‘#ecs_list_toolbar‘}); }else{ $("#ecs_list_toolbar").hide(); } }); //--加载DATAGRID-- function initDataGrid(){ dataGridTable.datagrid({ iconCls:‘icon-viewlist‘, url:webContext +‘/ecs/ecs_queryInstanceList.action?state=${param.state}‘, fit:true, border:false, pagination:true, rownumbers:true, nowrap: true, autoRowHeight: true, singleSelect:true, striped: true, pageList:[10,20,50], idField:‘id‘, onHeaderContextMenu:function(e, field){ e.preventDefault(); if(!cmenu){ createColumnMenu(‘ecs_list‘); } cmenu.menu(‘show‘,{ left:e.pageX }); }, onBeforeLoad: function (value, rec,param) { var opt = $(this).datagrid("getColumnOption", "opt"); if (opt) { opt.formatter = function (value, rowData, rowIndex) { var opt0 = "<a href=‘javascript:void(0)‘ style=‘text-decoration:none;‘ onclick=‘newLine("+rowData.id+")‘>修改</a> "; var opt4 = "<a href=‘javascript:void(0)‘ style=‘text-decoration:none;‘ onclick=‘deleteEcs("+rowData.id+")‘>删除</a> "; var opt1 = "<a href=‘javascript:void(0)‘ style=‘text-decoration:none;‘ onclick=‘submitIt("+rowData.id+")‘>确认提交</a> "; var opt3 = "<a href=‘javascript:void(0)‘ style=‘text-decoration:none;‘ onclick=addManagerTabs("+rowData.id+",‘"+rowData.instanceid+"‘,‘"+rowData.instancename+"‘)>管理</a> "; if(rowData.state==0){ return opt0+opt4+opt1; }else{ if(rowData.status=="Creating"){ return ""; }else{ return opt3; } } //return opt1+opt2+opt3; } } } }).datagrid("columnMoving"); $("#ecs_search_btn").click(function(){ dataGridTable.datagrid("clearSelections").datagrid("load",{ search_instancename:$("#search_instancename").val(), vpcid:$("#vpcid").combobox(‘getValue‘), privateipaddress_ip:$("#privateipaddress_ip").val(), start_time:$(‘#start_time‘).datebox(‘getValue‘), end_time:$(‘#end_time‘).datebox(‘getValue‘) }); }); $("#ecs_clear_btn").click(function(){ $(‘#ecs_search_form‘)[0].reset(); $("#vpcid").combobox(‘clear‘), $(‘#start_time‘).datebox(‘clear‘); $(‘#end_time‘).datebox(‘clear‘); }); $(‘#start_time‘).datebox({ onSelect:function(date){ var currentDate = new Date(); if(date>currentDate){ $.messager.alert(‘提示‘, "开始时间不能大于系统当前时间。"); $(‘#start_time‘).datebox(‘clear‘); } var end_time = $("#end_time").datebox(‘getValue‘); if(""==end_time){return;} end_time = new Date(end_time); if(date>end_time){ $.messager.alert(‘提示‘, "开始时间不能大于结束时间。"); $(‘#start_time‘).datebox(‘clear‘); } } }); $(‘#end_time‘).datebox({ onSelect:function(date){ var currentDate = new Date(); if(date>currentDate){ $.messager.alert(‘提示‘, "结束时间不能大于系统当前时间。"); $(‘#end_time‘).datebox(‘clear‘); } var start_time = $("#start_time").datebox(‘getValue‘); if(""==start_time){return;} start_time = new Date(start_time); start_time = new Date(start_time.setHours(0, 0, 0, 0)); if(date<start_time){ $.messager.alert(‘提示‘, "结束时间不能小于开始时间。"); $(‘#end_time‘).datebox(‘clear‘); } } }); } //--加载DIALOG-- function initDialog(){ myDialog.dialog({ width:fixWidth(‘F‘), height:fixHeight(‘Z‘), resizable:true, maximizable:true, maximized:true, modal:true, closed:true, onOpen:function(){ //dialog原始left default_left=$(this).panel(‘options‘).left; //dialog原始top default_top=$(this).panel(‘options‘).top; }, onMove:function(left,top){ //鼠标拖动时事件 var body_width=document.body.offsetWidth;//body的宽度 var body_height=document.body.offsetHeight;//body的高度 var dd_width= $(this).panel(‘options‘).width;//dialog的宽度 var dd_height= $(this).panel(‘options‘).height;//dialog的高度 if(left<1||left>(body_width-dd_width)||top<1||top>(body_height-dd_height)){ $(this).dialog(‘move‘,{ left:default_left, top:default_top }); } }, cache: false, onClose:function(){ $(‘#ecs_add_form‘)[0].reset(); }, buttons:[{ text:‘保存‘, iconCls:‘icon-save‘, handler:function(){ $(‘#ecs_add_form‘).submit(); //dataGridTable.datagrid("clearSelections"); } }] }); } function deleteEcs(id){ $.messager.confirm(‘消息提醒‘, ‘是否确认删除?‘, function(r){ if (r){ //alert(id); Ajax(‘/ecs/ecs_deleteEcs.action‘,{ id:id },function(data){ if(data.success){ Alert(‘提示‘,‘删除成功!‘); dataGridTable.datagrid("reload"); }else{ Alert(‘提示‘,‘删除失败:‘+data.msg); } }); } }); } function addManagerTabs(id,instanceid,instancename){ parent.$(‘#wf_layout‘).layout(‘collapse‘,‘west‘); var url = "/result/pcitcAliyun/ecs/instanceManager.jsp?id="+id+"&instanceid="+instanceid+"&instancename="+instancename; parent.page("","Ecs实例管理:"+id,url); } function addManagerTab(id,instanceid){ parent.$(‘#wf_layout‘).layout(‘collapse‘,‘west‘); var url = "/result/pcitcAliyun/ecs/instanceManager.jsp?id="+id+"&instanceid="+instanceid; parent.page("","Ecs实例管理:"+id,url); } function submitIt(id){ $.messager.confirm(‘消息提醒‘, ‘是否确认提交?‘, function(r){ if (r){ //alert(id); Ajax(‘/ecs/ecs_submitInstance.action‘,{ instanceId:id },function(data){ if(data.success){ Alert(‘提示‘,‘提交成功!‘); dataGridTable.datagrid("reload"); }else{ Alert(‘提示‘,‘提交失败:<br>‘+data.msg); } }); } }); } //--新增装置侧线-- function newLine(id){ var url="createInstance.jsp"; if(id!=null&&id!=""){ url=webContext+‘/ecs/editInstance!editInstance.action?id=‘+id; } myDialog.dialog("open").dialog(‘setTitle‘, ‘创建实例‘).dialog(‘refresh‘,encodeURI(url)); } function fstate(value){ if(value==‘Running‘){ return ‘<span style="color:green;">运行中</span>‘; }else if(value==‘Starting‘){ return ‘<span style="color:green;">启动中</span>‘; }else if(value==‘Stopped‘){ return ‘<span style="color:green;">已停止</span>‘; }else if(value==‘Stopping‘){ return ‘<span style="color:green;">停止中</span>‘; }else if(value==‘Deleted‘){ return ‘<span style="color:red;">已释放</span>‘; } } function show(storage_id){ var survey_type="<%=com.sinopec.system.workflow.survey.Constants.UNITPROCESSLINE%>"; var operType="TJ"; var url=webContext+‘/result/workFlow/shHistorySLst.jsp?dataId=‘+storage_id+‘&surveyType=‘+survey_type+‘&operType=‘+operType; $updateHis.dialog("open").dialog(‘setTitle‘, ‘审核意见‘).dialog(‘refresh‘,url); } function setW(value){ if(value.length>20){ return "<div>"+value.substring(0,20)+"</div><div>"+value.substring(20)+"</div>"; }else{ return value; } } function setIdAndName(value, rowData, rowIndex){ if(rowData.state==0){ return value+"<br>"+rowData.instancename; }else{ return rowData.instanceid+"<br>"+rowData.instancename; } } function setIps(value, rowData, rowIndex){ return rowData.natipaddress+"<br>"+rowData.privateipaddress_ip; } //--加载用户Dialog-- function initCreatorDialog(){ $(‘#sinopec_window_creator‘).dialog({ title:‘选择用户‘, width:fixWidth(‘C‘), height:fixHeight(‘C‘), resizable:true, maximizable:true, modal:true, closed:true, onOpen:function(){ //dialog原始left default_left=$(this).panel(‘options‘).left; //dialog原始top default_top=$(this).panel(‘options‘).top; }, onMove:function(left,top){ //鼠标拖动时事件 var body_width=document.body.offsetWidth;//body的宽度 var body_height=document.body.offsetHeight;//body的高度 var dd_width= $(this).panel(‘options‘).width;//dialog的宽度 var dd_height= $(this).panel(‘options‘).height;//dialog的高度 if(left<1||left>(body_width-dd_width)||top<1||top>(body_height-dd_height)){ $(this).dialog(‘move‘,{ left:default_left, top:default_top }); } }, buttons:[{ text:‘确定‘, iconCls:‘icon-save‘, handler:function(){ //var creatorInfo=window.frames["creatorFrame"].getCheckedCreatorInfo(); var creatorInfo=document.getElementById("creatorFrame").contentWindow.getCheckedCreatorInfo(); //if(creatorInfo != ‘undefined‘ && creatorInfo.length != 0){ if(creatorInfo){ var creatorStr = creatorInfo.split(","); var creatorname = creatorStr[1]; $("#responsible_name").val(creatorname); $("#responsible_id").val(creatorStr[0]); $(‘#sinopec_window_creator‘).dialog("close"); } } }] }); } </script> </head> <body> <div class="easyui-layout" data-options="border:false,fit:true" collapsible="true"> <div data-options="border:true,region:‘north‘" style="height:60px;overflow-x:auto;overflow-y:hidden;padding:11px" iconCls="icon-search"> <form id=‘ecs_search_form‘ method="post"> <input type="hidden" id="state" name="state" value="${param.state}"> <table class=‘sinopec_search_panel‘> <tr> <td align="right"><nobr>VPC:</nobr></td> <td><input id="vpcid" name="vpcid" class="easyui-combobox" data-options="panelWidth:‘200‘, valueField: ‘key‘, textField: ‘value‘, url:webContext+‘/vpc/vpc_getVpcLst.action‘, onSelect: function(rec){ }"/> </td> <td align="right" ><nobr>实例名称:</nobr> </td> <td > <input type="text" id="search_instancename" name="instancename" style="width:100px"> </td> <!-- <td align="right" ><nobr>实例ID:</nobr> </td> <td > <input type="text" id="search_instanceid" name="instancename" style="width:100px"> </td> --> <c:if test="${param.state eq 2}"> <td align="right" ><nobr>私有IP地址:</nobr> </td> <td > <input type="text" id="privateipaddress_ip" name="instancename" style="width:100px"> </td> </c:if> <td align="right" ><nobr>创建时间:</nobr></td> <td><input type="text" name="start_time" id="start_time" style="width:95px;" class="easyui-datebox" data-options="editable:false"/> </td> <td align="right" ><nobr>到:</nobr></td> <td><input type="text" name="end_time" id="end_time" style="width:95px;" class="easyui-datebox" data-options="editable:false"/> </td> <td><nobr> <a id="ecs_search_btn" href="#" class="easyui-linkbutton" data-options="iconCls:‘icon-search‘">查询</a> <a id="ecs_clear_btn" href="#" class="easyui-linkbutton" data-options="iconCls:‘icon-back‘">清空</a> </nobr></td> </tr> </table> </form> </div> <div data-options="region:‘center‘,border:false" style="overflow:hidden"> <div id="ecs_list_toolbar" style="padding:5px;"> <a href="javascript:void(0);" id="addTb" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newLine();">创建实例</a> </div> <table id="ecs_list"> <thead> <tr> <!--<th data-options="checkbox:true"></th> --> <th data-options="field:‘id‘,formatter:setIdAndName, width:150">实例ID/名称</th> <th data-options="field:‘zonename‘, width:100">所在可用区</th> <c:if test="${param.state eq 2}"> <th data-options="field:‘ips‘, width:150,formatter:setIps">IP地址</th> <th data-options="field:‘status‘,formatter:fstate, width:100,align:‘center‘,sortable:true">状态</th> </c:if> <th data-options="field:‘instancetypename‘,formatter:setW, width:200">配置</th> <th data-options="field:‘create_time‘, width:100">创建时间</th> <th data-options="field:‘opt‘, width:150">操作项</th> </tr> </thead> </table> </div> </div> <div id="ecs_window" style="overflow:hidden;padding:10px;"></div> <div id="batchImport_dialog" style=""><iframe id="batchImportFrame" frameborder="0" style="width:100%;height:99%;"></iframe></div> <div id="sinopec_window_creator" style="padding:10px;"> <iframe id="creatorFrame" scrolling="auto" frameborder="0" src="" style="width:100%;height:100%;"></iframe> </div> </body> </html>
entity层代码如下所示:
AliEcsInstance.java
package com.aliyun.pcitcAliyun.ecs.model; import java.text.ParseException; import java.util.Date; import com.aliyun.util.DateUtil; import com.sinopec.common.model.Page; public class AliEcsInstance extends Page{ private int id; private String instanceid; private String instancename; private String description; private String regionid; private String regionname; private String zoneid; private String zonename; private String imageid; private String imagename; private String instancetype; private String instancetypename; private String securitygroupid; private String securityGroupIds; private String internetchargetype; private String internetmaxbandwidthin; private String internetmaxbandwidthout; private String hostname; private String password; private String systemdiskcategory; private String systemdiskdiskname; private String systemdiskdescription; private String vswitchid; private String systemcode; private String systemname; private String remark; private String creator_id; private String creator_name; private String creator_dep; private String creator_depname; private Date create_time; private String state; private String status; private Date submit_time; private String privateipaddress_ip; private String natipaddress; private String eip; private String responsible_id; private String responsible_name; private String startupCount; //不跟数据库对应字段 private Date start_time; private Date end_time; private String vpcid; private String loadbalancerid; private String vswitchname; private String vncUrl; /** * 阿里创建时间 */ private String creationTime; /** * 阿里云资源是否在本地数据库中存在 * 0:不存在;1:存在; */ Integer existInDB; public String getVpcid() { return vpcid; } public void setVpcid(String vpcid) { this.vpcid = vpcid; } public String getStartupCount() { return startupCount; } public void setStartupCount(String startupCount) { this.startupCount = startupCount; } public String getVswitchname() { return vswitchname; } public void setVswitchname(String vswitchname) { this.vswitchname = vswitchname; } public Date getStart_time() { return start_time; } public void setStart_time(Date start_time) { this.start_time = start_time; } public Date getEnd_time() { return end_time; } public void setEnd_time(Date end_time) { this.end_time = end_time; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getInstanceid() { return instanceid; } public void setInstanceid(String instanceid) { this.instanceid = instanceid; } public String getInstancename() { return instancename; } public void setInstancename(String instancename) { this.instancename = instancename; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getRegionid() { return regionid; } public void setRegionid(String regionid) { this.regionid = regionid; } public String getRegionname() { return regionname; } public String getResponsible_id() { return responsible_id; } public void setResponsible_id(String responsible_id) { this.responsible_id = responsible_id; } public String getResponsible_name() { return responsible_name; } public void setResponsible_name(String responsible_name) { this.responsible_name = responsible_name; } public void setRegionname(String regionname) { this.regionname = regionname; } public String getZoneid() { return zoneid; } public void setZoneid(String zoneid) { this.zoneid = zoneid; } public String getZonename() { return zonename; } public void setZonename(String zonename) { this.zonename = zonename; } public String getImageid() { return imageid; } public void setImageid(String imageid) { this.imageid = imageid; } public String getImagename() { return imagename; } public void setImagename(String imagename) { this.imagename = imagename; } public String getInstancetype() { return instancetype; } public void setInstancetype(String instancetype) { this.instancetype = instancetype; } public String getInstancetypename() { return instancetypename; } public void setInstancetypename(String instancetypename) { this.instancetypename = instancetypename; } public String getSecuritygroupid() { return securitygroupid; } public void setSecuritygroupid(String securitygroupid) { this.securitygroupid = securitygroupid; } public String getInternetchargetype() { return internetchargetype; } public void setInternetchargetype(String internetchargetype) { this.internetchargetype = internetchargetype; } public String getInternetmaxbandwidthin() { return internetmaxbandwidthin; } public void setInternetmaxbandwidthin(String internetmaxbandwidthin) { this.internetmaxbandwidthin = internetmaxbandwidthin; } public String getInternetmaxbandwidthout() { return internetmaxbandwidthout; } public void setInternetmaxbandwidthout(String internetmaxbandwidthout) { this.internetmaxbandwidthout = internetmaxbandwidthout; } public String getHostname() { return hostname; } public void setHostname(String hostname) { this.hostname = hostname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSystemdiskcategory() { return systemdiskcategory; } public void setSystemdiskcategory(String systemdiskcategory) { this.systemdiskcategory = systemdiskcategory; } public String getSystemdiskdiskname() { return systemdiskdiskname; } public void setSystemdiskdiskname(String systemdiskdiskname) { this.systemdiskdiskname = systemdiskdiskname; } public String getSystemdiskdescription() { return systemdiskdescription; } public void setSystemdiskdescription(String systemdiskdescription) { this.systemdiskdescription = systemdiskdescription; } public String getVswitchid() { return vswitchid; } public void setVswitchid(String vswitchid) { this.vswitchid = vswitchid; } public String getSystemcode() { return systemcode; } public void setSystemcode(String systemcode) { this.systemcode = systemcode; } public String getSystemname() { return systemname; } public void setSystemname(String systemname) { this.systemname = systemname; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getCreator_id() { return creator_id; } public void setCreator_id(String creator_id) { this.creator_id = creator_id; } public String getCreator_name() { return creator_name; } public void setCreator_name(String creator_name) { this.creator_name = creator_name; } public String getCreator_dep() { return creator_dep; } public void setCreator_dep(String creator_dep) { this.creator_dep = creator_dep; } public String getCreator_depname() { return creator_depname; } public void setCreator_depname(String creator_depname) { this.creator_depname = creator_depname; } public Date getCreate_time() { return create_time; } public void setCreate_time(Date create_time) { this.create_time = create_time; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public Date getSubmit_time() { return submit_time; } public void setSubmit_time(Date submit_time) { this.submit_time = submit_time; } public String getPrivateipaddress_ip() { return privateipaddress_ip; } public void setPrivateipaddress_ip(String privateipaddress_ip) { this.privateipaddress_ip = privateipaddress_ip; } public String getNatipaddress() { return natipaddress; } public void setNatipaddress(String natipaddress) { this.natipaddress = natipaddress; } public String getEip() { return eip; } public void setEip(String eip) { this.eip = eip; } public Integer getExistInDB() { return existInDB; } public void setExistInDB(Integer existInDB) { this.existInDB = existInDB; } public String getSecurityGroupIds() { return securityGroupIds; } public void setSecurityGroupIds(String securityGroupIds) { this.securityGroupIds = securityGroupIds; } public String getCreationTime() { String str = ""; if(creationTime != null) { String tempDateStr = creationTime.replace("T", " ").replace("Z", ""); try { str = DateUtil.getDateTime(DateUtil.getBeiJingTime(DateUtil.string2Date(tempDateStr))); } catch (ParseException e) { e.printStackTrace(); } } return str; } public void setCreationTime(String creationTime) { this.creationTime = creationTime; } public String getLoadbalancerid() { return loadbalancerid; } public void setLoadbalancerid(String loadbalancerid) { this.loadbalancerid = loadbalancerid; } public String getVncUrl() { return vncUrl; } public void setVncUrl(String vncUrl) { this.vncUrl = vncUrl; } }
action层代码如下所示:
EcsAction.java
public String queryInstanceList() { try { HttpServletRequest request = ServletActionContext.getRequest(); String search_instancename = request .getParameter("search_instancename"); String vpcid = request.getParameter("vpcid"); String privateipaddress_ip = request .getParameter("privateipaddress_ip"); String start_time = request.getParameter("start_time"); String end_time = request.getParameter("end_time"); String state = request.getParameter("state"); String loadbalancerid = request.getParameter("loadbalancerid"); if (!StringUtils.isEmpty(search_instancename)) { ecs.setInstancename(search_instancename); } if (!StringUtils.isEmpty(vpcid)) { ecs.setVpcid(vpcid); } if (!StringUtils.isEmpty(privateipaddress_ip)) { ecs.setPrivateipaddress_ip(privateipaddress_ip); } if (!StringUtils.isEmpty(start_time)) { ecs.setStart_time(DateUtil .string2Date(start_time, "yyyy-MM-dd")); } if (!StringUtils.isEmpty(end_time)) { ecs.setEnd_time(DateUtil.string2Date(end_time, "yyyy-MM-dd")); } if (!StringUtils.isEmpty(state)) { ecs.setState(state); } if (!StringUtils.isEmpty(loadbalancerid)) { ecs.setLoadbalancerid(loadbalancerid); } // 查询自己的或者责任人是自己的 ecs.setCreator_id(SessionUtilMap.getLoginUser().getUser_id()); ecs.setResponsible_id(SessionUtilMap.getLoginUser().getUser_id()); ecs.setPageNo(getPage()); ecs.setPageSize(getRows()); JsonPage<AliEcsInstance> page = ecsManager.searchInstancePage(ecs); this.setResult(page.toJson()); // 设置属性字段方便前端使用,与具体业务逻辑无关 request.getSession().setAttribute("username_ecs", SessionUtilMap.getLoginUser().getUser_name()); } catch (Exception e) { simpleJSON.put("success", false); simpleJSON.put("msg", e.getMessage()); throw new BaseRuntimeException(e.getMessage()); } return SUCCESS; }
JsonPage.java
package com.sinopec.common.model; import java.util.ArrayList; import java.util.Date; import java.util.List; import net.sf.json.JSON; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; import net.sf.json.JsonConfig; /** * 封装结果集的对象,用于转换为jQuery Easy UI的json对象 * @param <T> * create at 2012-9-12 下午01:47:47 * @author wanghe */ public class JsonPage<T> { private JsonConfig config = new JsonConfig(); /** * 记录总数 */ private int total; /** * 结果集 */ private List<T> rows = new ArrayList<T>(); public JsonPage(){ super(); config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); config.setExcludes(new String[]{"config"}); } public JsonPage(String formate){ this(); config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor(formate)); } public JsonPage(List<T> rows,int total){ this(); this.total = total; this.rows = rows; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public List<T> getRows() { return rows; } public void setRows(List<T> rows) { this.rows = rows; } public JsonConfig getConfig() { return config; } public void setConfig(JsonConfig config) { this.config = config; } public JSON toJson(){ return JSONSerializer.toJSON(this, config); } public JSONObject toJsonObject(){ return JSONObject.fromObject(this, config); } }
QueryAction.java
package com.sinopec.common.action; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import net.sf.json.JSON; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import com.sinopec.common.model.DateJsonValueProcessor; import com.sinopec.common.prossBar.manager.ProcessBarManager; import com.sinopec.common.prossBar.model.ProceBarPercent; import com.sinopec.common.util.StringUtil; public abstract class QueryAction extends ActionSupport { protected ProcessBarManager processBarManagerImp; private static final long serialVersionUID = -6604170344271862481L; /** * 返回的结果 */ private JSON result; private JsonConfig config = new JsonConfig(); /** * 简单返回结果用,在不设置result时使用 */ public JSONObject simpleJSON = new JSONObject(); /** * 页数 */ private int pageNo; /** * 记录数 */ private int pageSize; /** * 排序字段 */ private String sort; /** * 排序方式(desc?asc) */ private String order; public int getPage() { return pageNo; } /** * 分页起始结果数 * * @return * int * create at 2012-9-14 上午11:09:25 * @author wanghe */ public int getStart() { return (pageNo-1)*pageSize; } /** * 分页最大结果数 * * @return * int * create at 2012-9-14 上午11:09:35 * @author wanghe */ public int getEnd() { return pageNo*pageSize; } public void setPage(int page) { this.pageNo = page; } public int getRows() { return pageSize; } public void setRows(int rows) { this.pageSize = rows; } public String getSort() { return sort; } public void setSort(String sort) { this.sort = sort; } public String getOrder() { return order; } public void setOrder(String order) { this.order = order; } public JSON getResult() { return result==null?simpleJSON:result; } /** * 设置返回的JSON对象 * @param result * void * create at 2012-10-19 上午11:28:46 * @author wanghe */ public void setResult(JSON result) { this.result = result; } public void setResult(Object result,String formate) { config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor(formate)); this.result = JSONObject.fromObject(result, config); } /** * 设置要返回的对象,此方法会将传递进来的对象解析成JSON格式 * @param model * void * create at 2012-10-19 上午11:28:55 * @author wanghe */ public void setModel(Object model) { this.result = JSONObject.fromObject(model); } /** * 传入一个ID字符串(以,号连接的),解析后,返回ID集合 * @param array * @return * List<String> * create at 2012-10-11 下午05:16:09 * @author wanghe */ public List<String> toList(String array){ ArrayList<String> list = new ArrayList<String>(); if(array!=null){ for (String string : array.split(",")) { list.add(string); } } return list; } protected List<String> toList(String[]idarray){ List<String> idList=new ArrayList<String>(); if(idarray!=null) for(String id:idarray){ idList.add(id); } return idList; } /** * 将以,或者空格分隔的字符串解析成字符数组 * @param str 以,或者空格分隔的字符串 * @return 字符数组 * @author hanxg * @date 2014-2-13 */ protected List<String> split(String str){ if(StringUtil.isEmpty(str)) return null; String[] arr = StringUtil.format(str).split("[\\s,]+", 0); List<String> list = new ArrayList<String>(); for(String s:arr){ if(!StringUtil.format(s).equals("")) list.add(s); } return list; } /** * 开始导入校验 * @return */ public String processCheck(){ HttpServletRequest request = ServletActionContext.getRequest(); String guid=request.getParameter("guid"); Object obj=request.getSession().getAttribute(guid); if(obj!=null){ simpleJSON.put("processState",true); }else{ ProceBarPercent proceBarPercent=new ProceBarPercent(); request.getSession().setAttribute(guid,proceBarPercent); simpleJSON.put("processState",true); } return SUCCESS; } /** * 获取进程进度的方法 默认的方法 阶段性处理 * @return */ public String getDefPersent(){ HttpServletRequest request = ServletActionContext.getRequest(); String guid=request.getParameter("guid"); ProceBarPercent proceBarPercent=(ProceBarPercent)request.getSession().getAttribute(guid); while(proceBarPercent.getNextPercent()==0){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } simpleJSON.put("currentPercent",proceBarPercent.getDefPersent().getCurrentPercent()); simpleJSON.put("isHasValidatePass",proceBarPercent.isHasValidatePass()); proceBarPercent.setNextPercent(0); return SUCCESS; } /** * 获取进程进度的方法 校验 条条进行 * @return */ public String getValidatePersent(){ HttpServletRequest request = ServletActionContext.getRequest(); String guid=request.getParameter("guid"); ProceBarPercent proceBarPercent=(ProceBarPercent)request.getSession().getAttribute(guid); while(proceBarPercent.getNextPercent()==0){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } simpleJSON.put("currentPercent",proceBarPercent.getValidatePersent().getCurrentPercent()); simpleJSON.put("isHasValidatePass",proceBarPercent.isHasValidatePass()); proceBarPercent.setNextPercent(0); return SUCCESS; } /** * 获取进程进度的方法 导入 * @return */ public String getImportPersent(){ HttpServletRequest request = ServletActionContext.getRequest(); String guid=request.getParameter("guid"); ProceBarPercent proceBarPercent=(ProceBarPercent)request.getSession().getAttribute(guid); this.setResult(JSONObject.fromObject(proceBarPercent.getValidatePersent())); return SUCCESS; } public ProcessBarManager getProcessBarManager() { return processBarManagerImp; } public void setProcessBarManager(ProcessBarManager processBarManagerImp) { this.processBarManagerImp = processBarManagerImp; } }
manager层代码如下所示:
EcsManager.java
JsonPage<AliEcsInstance> searchInstancePage(AliEcsInstance ecs);
manager impl层代码如下所示:
EcsManagerImp.java
@Override public JsonPage<AliEcsInstance> searchInstancePage(AliEcsInstance ecs) { JsonPage<AliEcsInstance> page = new JsonPage<AliEcsInstance>(); page.setRows(ecsDao.findInstanceByDyConditon(ecs)); page.setTotal(ecsDao.searchInstanceTotal(ecs)); return page; }
dao层代码如下所示:
EcsDao.java
List<AliEcsInstance> findInstanceByDyConditon(AliEcsInstance ecs); int searchInstanceTotal(AliEcsInstance ecs);
dao impl层代码如下所示:
EcsDaoImp.java
@Override public List<AliEcsInstance> findInstanceByDyConditon(AliEcsInstance ecs) { return this.getSqlMapClientTemplate().queryForList( "findInstanceByDyConditon", ecs); } @Override public int searchInstanceTotal(AliEcsInstance ecs) { return (Integer) this.getSqlMapClientTemplate().queryForObject( "findInstanceTotal", ecs); }
ibatis代码如下所示:
page.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="page"> <!-- <sql id="pageStart"> <isNotNull property="start"> select * from ( select sun.* ,Rownum r from ( </isNotNull> </sql> <sql id="pageEnd"> <isNotNull property="start"> ) sun where Rownum <![CDATA[<= ]]> #end# ) super where super.r> #start# </isNotNull> </sql> --> <sql id="pageStart"> </sql> <sql id="pageEnd"> <isNotNull property="start"> limit #start#,#pageSize# </isNotNull> </sql> </sqlMap>
AliEcsInstance.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="AliEcsInstance"> <typeAlias alias="AliEcsInstance" type="com.aliyun.pcitcAliyun.ecs.model.AliEcsInstance"/> <resultMap id="result" class="AliEcsInstance"> <result property="id" column="ID"/> <result property="instanceid" column="INSTANCEID"/> <result property="instancename" column="INSTANCENAME"/> <result property="description" column="DESCRIPTION"/> <result property="regionid" column="REGIONID"/> <result property="regionname" column="REGIONNAME"/> <result property="zoneid" column="ZONEID"/> <result property="zonename" column="ZONENAME"/> <result property="imageid" column="IMAGEID"/> <result property="imagename" column="IMAGENAME"/> <result property="instancetype" column="INSTANCETYPE"/> <result property="instancetypename" column="INSTANCETYPENAME"/> <result property="securitygroupid" column="SECURITYGROUPID"/> <result property="internetchargetype" column="INTERNETCHARGETYPE"/> <result property="internetmaxbandwidthin" column="INTERNETMAXBANDWIDTHIN"/> <result property="internetmaxbandwidthout" column="INTERNETMAXBANDWIDTHOUT"/> <result property="hostname" column="HOSTNAME"/> <result property="password" column="PASSWORD"/> <result property="systemdiskcategory" column="SYSTEMDISKCATEGORY"/> <result property="systemdiskdiskname" column="SYSTEMDISKDISKNAME"/> <result property="systemdiskdescription" column="SYSTEMDISKDESCRIPTION"/> <result property="vswitchid" column="VSWITCHID"/> <result property="systemcode" column="SYSTEMCODE"/> <result property="systemname" column="SYSTEMNAME"/> <result property="remark" column="REMARK"/> <result property="creator_id" column="CREATOR_ID"/> <result property="creator_name" column="CREATOR_NAME"/> <result property="creator_dep" column="CREATOR_DEP"/> <result property="creator_depname" column="CREATOR_DEPNAME"/> <result property="create_time" column="CREATE_TIME"/> <result property="state" column="STATE"/> <result property="status" column="STATUS"/> <result property="submit_time" column="SUBMIT_TIME"/> <result property="privateipaddress_ip" column="PRIVATEIPADDRESS_IP"/> <result property="natipaddress" column="NATIPADDRESS"/> <result property="eip" column="EIP"/> <result property="responsible_name" column="responsible_name"/> <result property="responsible_id" column="responsible_id"/> <result property="startupCount" column="startupCount"/> </resultMap> <resultMap id="oneresult" extends="result" class="AliEcsInstance" > <result property="vswitchname" column="vswitchname"/> </resultMap> <sql id="InstanceDySql"> <isNotEmpty prepend="and" property="state"> state = #state# </isNotEmpty> <isNotEmpty prepend="and" property="instancename"> instancename like concat(‘%‘, #instancename#, ‘%‘) </isNotEmpty> <isNotEmpty prepend="and" property="securitygroupid"> securitygroupid = #securitygroupid# </isNotEmpty> <isNotEmpty prepend="and" property="vpcid"> vswitchid in(select vswitchid from ali_vswitch where vpcid=#vpcid#) </isNotEmpty> <isNotEmpty prepend="and" property="privateipaddress_ip"> privateipaddress_ip like concat(‘%‘, #privateipaddress_ip#, ‘%‘) </isNotEmpty> <isNotEmpty prepend="and" property="creator_id"> (creator_id = #creator_id# or responsible_id=#responsible_id#) </isNotEmpty> <isNotEmpty prepend="and" property="loadbalancerid"> id NOT IN(select ecsid as id from ali_slb_backendservers where loadbalancerid=#loadbalancerid#) </isNotEmpty> <isNotEmpty prepend="and" property="start_time"> <![CDATA[ DATE_FORMAT(submit_time,"%Y-%m-%d") >=DATE_FORMAT(#start_time#,‘%Y-%m-%d‘) ]]> </isNotEmpty> <isNotEmpty prepend="and" property="end_time"> <![CDATA[ DATE_FORMAT(submit_time,"%Y-%m-%d") <=DATE_FORMAT(#end_time#,‘%Y-%m-%d‘) ]]> </isNotEmpty> </sql> <select id="findInstanceByDyConditon" parameterClass="AliEcsInstance" resultMap="result"> <include refid="pageStart"/> select * from ali_ecs_instance where (status is null or status !=‘Deleted‘ ) <include refid="InstanceDySql"/> <include refid="pageEnd"/> </select> <select id="findInstanceTotal" parameterClass="AliEcsInstance" resultClass="int"> select count(*) from ali_ecs_instance where (status is null or status !=‘Deleted‘ ) <include refid="InstanceDySql"/> </select> </sqlMap>
标签:
原文地址:http://www.cnblogs.com/longshiyVip/p/4591150.html