记录一下JFinal 在controller 添加事务处理

controller添加事务;

@Before(value = { ErrorInterceptor.class, Tx.class }) 
前面是拦截器处理自定义抛异常 拦截器把错误信息显示到页面   这种事务的写法 如果代码加了try 事务是不起作用的  必须手动抛异常
public class XXController extends BaseMsgController {}
刚开始 我写成了  @Before(Tx.class)  不管怎么弄都不好使, 这种写法是添加到方法上的


自定义抛异常代码: 下面catch里面 不手动抛异常 事务是不会回滚的   如果是在方法上面加 @Before...则不用手动抛 如果是在类上面整体加的需要手动抛异常

	                       try {
					order.update();
					reVo = new ReVo(order);
				} catch (Exception e) {
					logger.info(e.getMessage());
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("msg", "网络异常请刷新重试!");
					UnifyThrowEcxp.throwExcp(e, map);
				}


异常处理类:

public class UnifyThrowEcxp {

	/**
	 * 统一抛异常- maps里面可自定义异常参数
	 */
	public static void throwExcp(Exception e, Map<String, Object> maps) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("msg", e.getMessage());
		map.put("text", e.getMessage());
		if (!maps.isEmpty()) {
			map.put("errMsg", JSONObject.toJSONString(maps));
		}
		throw new RuntimeException(JSONObject.toJSONString(map));
	}

}

拦截器错误处理: 这里只是简单的 处理了一下 直接把错误信息render到了页面

public class ErrorInterceptor implements Interceptor {

	public void intercept(Invocation ai) {
		try {
			ai.invoke();
		} catch (Exception e) {
			JSONObject jo = JSONObject.parseObject(e.getMessage());
			ai.getController().renderJson(jo.toJSONString());
		}
	}
}


运行结果:

手动抛错调用的代码:  如果不添加自定义错误信息 就直接传一个 空的map就行

                                Map<String, Object> map = new HashMap<String, Object>();
				map.put("msg", "测试测试测试!");
				UnifyThrowEcxp.throwExcp(e, map);

菜鸟发帖 大神勿喷  纯属记录一下过程  


 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值