Demo实例讲解-action类的列表页面和编辑页面

515 阅读4分钟

测试项目

测试项目地址

码云地址 gitee.com/huhuanan/mo…

后台测试地址: test.ostudio.cc/admin admin 123456 (请勿修改密码,如不能登录请联系我,最下面有联系方式)

后台页面的Action类

Action类的规则

  1. 所有Action类都要继承m.common.action.Action,后台页面的Action类要继承manage.action.ManageAction
  2. 可访问的Action类需要配置@ActionMeta(name="demoDemo")注解,访问路径为/action/注解name值/方法名
  3. 请求Action时,Action类的属性可根据请求参数自动填充。
  4. Action类所在包要配置到mconfig.properties里。
#action包, 多个用逗号分开(,)   继承m.common.action 系统Action
action_pack=mo.test.demo.action

继承ManageAction类的功能

  1. 可使用注解实现列表页面,方法内最简单可只用写一行代码,使用注解定义列表内容,无需写sql即可完成列表页面及操作按钮。如下图
  2. 可使用注解实现编辑页面,方法内只需填充Action类的属性和固定一行代码,加上注解,无需sql即可完成编辑页面。如下图
  3. 可使用注解编写图表页面,如下图
  4. 还可以实现导出Excel

Demo的Action代码

Demo列表页面

列表页面访问路径有点特殊,由于先要根据注解展示列表页面,在请求该方法的数据内容。

列表请求路径action/demoDemo/toList?method=demoDetailDatatoListmanage.action.ManageAction里面的方法,method参数是列表方法。

该路径配到到菜单里了,打开菜单时,就展示该列表。

@ActionTableMeta(dataUrl = "action/demoDemo/demoDetailData",//查询数据的action方法,即对应方法的访问路径
		modelClass="mo.test.demo.model.DemoDetail",//查询开始的对象
		rowspanIndex=1,rowspanNum=5,//行合并开始和合并列数
		orders= {"demo.createDate desc","demo.oid desc"},//排序
	cols = { 
		//表格列定义
		//field 对象属性
		//title 列名
		//width 宽
		//align 对齐
		//numberFormat dateFormat 数字格式和日期格式
		@ActionTableColMeta(field = "demo.oid", title = "",type=TableColType.INDEX),
		@ActionTableColMeta(field = "demo.name", title = "名称", width=100),
		@ActionTableColMeta(field = "demo.total", title = "数量", width=100,align="right",numberFormat="#,##0"),
		@ActionTableColMeta(field = "demo.money", title = "金额", width=100,align="right",numberFormat="#,##0.00"),
		@ActionTableColMeta(field = "demo.createDate", title = "创建时间", width=100,dateFormat="yyyy-MM-dd"),
		//buttons 行内按钮
		@ActionTableColMeta(field = "demo.oid",title="操作",width=170,align="center",isExcel=false,buttons={
			@ButtonMeta(title="修改", event = ButtonEvent.MODAL,modalWidth=550, url = "action/demoDemo/toModifyDemo",
				params={@ParamMeta(name = "model.oid", field="demo.oid")},success=SuccessMethod.REFRESH,style=ButtonStyle.NORMAL,
				power="demo_power"
			),
			@ButtonMeta(title="添加明细", event = ButtonEvent.MODAL,modalWidth=550, url = "action/demoDemo/toModifyDemoDetail",
				params={@ParamMeta(name = "detail.demo.oid", field="demo.oid")},success=SuccessMethod.REFRESH,style=ButtonStyle.SUCCESS,
				power="demo_power"
			),
		}),
		@ActionTableColMeta(field = "name", title = "明细名称", width=100),
		@ActionTableColMeta(field = "money", title = "明细金额", width=100,align="right",numberFormat="#,##0.00",countType=TableCountType.SUM),
		@ActionTableColMeta(field = "oid",title="操作",width=100,align="center",isExcel=false,buttons={
			@ButtonMeta(title="修改", event = ButtonEvent.MODAL,modalWidth=550, url = "action/demoDemo/toModifyDemoDetail",
				params={@ParamMeta(name = "detail.oid", field="oid")},success=SuccessMethod.REFRESH,style=ButtonStyle.SUCCESS,
				power="demo_power"
			),
		})
	},
	//查询条件
	querys = {
		@QueryMeta(field = "demo.name", name = "名称", type = QueryType.TEXT, hint="请输入名称", likeMode=true)
	},
	//表格上按钮
	buttons = {
		@ButtonMeta(title="新增", event = ButtonEvent.MODAL,modalWidth=550,  url = "action/demoDemo/toAddDemo", 
			success=SuccessMethod.REFRESH,style=ButtonStyle.NORMAL,
			power="demo_power"
		),
		@ButtonMeta(title="报表",style=ButtonStyle.DEFAULT, event = ButtonEvent.MODAL,modalWidth=1000, url = "action/demoDemo/toChart?method=demoChart", 
			queryParams={
				@ParamMeta(name="params[name]",field="demo.name"),
			},power="demo_power"
		),
		@ButtonMeta(title="导出",style=ButtonStyle.DEFAULT, event = ButtonEvent.OPEN, url = "action/demoDemo/demoExcel", 
		    queryParams={
				@ParamMeta(name="params[demo.name]",field="demo.name"),
		    },power="demo_power"
		)
	}
)
public JSONMessage demoDetailData() throws Exception{
	return getListDataResult(null);
}

以上方法就是列表页面所需数据,getListDataResultmanage.action.ManageAction里面的方法,会根据方法注解生成sql并转换成JSONMessage返回给页面。

Demo编辑页面

编辑页面可直接根据定义方法访问,该Demo的访问路径是action/demoDemo/toModifyDemoDetail

编辑页面注解用的的field属性是Action类的属性,所以编辑的方法体内需要给Action的detail属性填充值。

@ActionFormMeta(title="Demo明细",
	rows={
		@FormRowMeta(fields={
			@FormFieldMeta(title="demo",field="detail.demo.oid",type=FormFieldType.SELECT,hint="请选择demo",
				querySelect= @QuerySelectMeta(modelClass = "mo.test.demo.model.DemoInfo", title = "name", value = "oid",
					titleExpression="concat(#{name},' 数量:',#{total},' 金额:',#{money})")
			),
		}),
		@FormRowMeta(fields={
			@FormFieldMeta(field = "detail.oid", type = FormFieldType.HIDDEN),
			@FormFieldMeta(title="明细名称",field="detail.name",type=FormFieldType.TEXT,span=12,hint="请输入明细名称"),
			@FormFieldMeta(title="明细金额",field="detail.money",type=FormFieldType.DOUBLE,span=12,hint="请输入明细金额"),
		})
	},
	buttons={
		@FormButtonMeta(title = "保存", url = "action/demoDemo/doSaveDetail",success=FormSuccessMethod.DONE_BACK)
	}
)
public ActionResult toModifyDemoDetail() throws Exception{
	verifyAdminOperPower("demo_power");
	if(null!=detail&&!StringUtil.isSpace(detail.getOid())) {
		detail=ModelQueryList.getModel(detail, new String[] {"*"});
	}
	return getFormResult(this,ActionFormPage.EDIT);
}

Demo图表页面

图表方式类似于列表页面,访问路径是action/demoDemo/toChart?method=demoChart

@ActionChartMeta(dataUrl="action/demoDemo/demoChart",tableHeight=400,
		modelClass="mo.test.demo.model.DemoInfo",
	series = { 
		@ChartSeries(field = "total", name = "明细数量",markPoint=true,type=ChartSeriesType.BAR),
		@ChartSeries(field = "money", name = "明细金额",index=1,type=ChartSeriesType.BAR)
	},
	xAxis=@ChartXAxis(field="name",type=ChartAxisType.CATEGORY,dataZoom=true),
	querys={
		@QueryMeta(field = "name", name = "名称", type = QueryType.TEXT, hint="请输入名称", likeMode=true)
	}
)
public JSONMessage demoChart() throws Exception {
	return getChartDataResult(null);
}

Demo导出方法

导出方法直接调用即可导出excel,excel内容可根据列表方法的注释导出。

public ActionResult demoExcel() throws Exception{
    ExcelObject eo=new ExcelObject("demo列表");
    ActionTableMeta meta=AnnotationUtil.getAnnotation4Method(ActionTableMeta.class, getActionClass(), "demoDetailData");
    List<QueryCondition> list=QueryMetaUtil.convertQuery(getParams(),meta.querys());//查询条件
    eo.addSheet(super.getExcelSheet(this.getActionClass(),"demoDetailData",list.toArray(new QueryCondition[] {}),"demo"));
    return toExportExcel(eo);
}

Demo保存方法

保存方法其实就是普通的action方法,请求处理后返回json。

public JSONMessage doSaveDetail(){
	setLogContent("保存", "保存demo明细");
	JSONMessage result=new JSONMessage();
	try {
		verifyAdminOperPower("demo_power");//鉴权
		String msg=getService(DemoService.class).saveDetail(detail);//使用service类保存
		result.push("detail.oid", detail.getOid());
		result.push("code", 0);//返回code=0代表成功
		result.push("msg", msg);
	} catch (Exception e) {
		result.push("code", 1);
		result.push("msg", e.getMessage());
		setLogError(e.getMessage());
		if(RuntimeData.getDebug()) e.printStackTrace();
	}
	return result;
}

这类普通方法也有注解,可在后台管理的开发指南菜单里查看并测试。如下图

具体实现可下载Demo部署调试

开发指南

如何使用M快速开发框架创建项目

M:Demo实例讲解-model类及操作

持续更新中.....

码云地址 gitee.com/huhuanan/mo… 欢迎大家关注,喜欢的请点 star

点击链接加入群聊【M快速开发框架】交流讨论 群号:764581300

谢谢支持