自定义FailProcessor类
package com.my.mvc.processor;
import org.nutz.json.JsonFormat;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionInfo;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.impl.processor.ViewProcessor;
import com.my.BizRuntimeException;
/**
*
* @author wjw
*
*/
public class FailProcessor extends ViewProcessor {
private static final Log log = Logs.get();
//紧凑格式输出字符串
private static final JsonFormat format =new JsonFormat();
@Override
public void init(NutConfig config, ActionInfo ai) throws Throwable {
view = evalView(config, ai, ai.getFailView());
}
public void process(ActionContext ac) throws Throwable {
if(ac.getError() instanceof BizRuntimeException){
BizRuntimeExceptionJson json =new BizRuntimeExceptionJson();
json.setFlag(false);
json.setMessage(ac.getError().getMessage());
Mvcs.write(ac.getResponse(), json, format);
return;
} else if(log.isWarnEnabled()) {
String uri = Mvcs.getRequestPath(ac.getRequest());
log.warn(String.format("Error@%s :", uri), ac.getError());
}
super.process(ac);
}
/**
* 返回的json对象
* @author wjw
*
*/
public class BizRuntimeExceptionJson {
private boolean flag;
private String message;
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
}