2017-02-08 09:31
@杜福忠
ps:
@Before(Tx.class)
public void trans_demo() {// 这是 Controller 的一个 Action
//.... 这里面不能 try { } catch (){} 给吃掉, 如果业务需要,那就catch 捕获到以后需要再向上抛 throw,
}
比如写API的时候,就算异常了也要给调用者返回错误信息,不能是500吧,
我一般这样写:
// 这是 Controller 的一个 Action
//在前面加多个拦截器 参考手册第4.2 Interceptor 章节
@Before({renderJsonTxInterceptor.class, Tx.class})
public void trans_demo() {
//...
}
//这是 拦截器 renderJsonTxInterceptor的代码:
public class renderJsonTxInterceptor implements Interceptor {
@Override
public void intercept(Invocation inv) {
try {
//这里 测试就使用 System.out.println 了 生成是不能用的,会被打死
System.out.println("renderJsonTxInterceptor 进入");
inv.invoke();
System.out.println("renderJsonTxInterceptor 没有检查到异常");
} catch (Exception e) {
e.printStackTrace();
System.out.println("renderJsonTxInterceptor 捕获到异常,并放入错误码");
inv.getController().renderJson("{\"ret\":0}");//错误码自己定义,这里就不写了
}
}
}
2017-02-08 09:16
请打开手册第 : 5.5 声明式事务 章节
注意:MySql 数据库表必须设置为 InnoDB 引擎时才支持事务,MyISAM 并不支持事务。
最简单的方式:
在 Controller 的 Action 上面加@Before(Tx.class) ,
(ps:不能在其他类直接加 @Before(Tx.class), 如果需要使用, 具体使用方式参考 手册第4.6 Duang 、Enhancer 章节
如手册例:
@Before(Tx.class)
public void trans_demo() {// 这是 Controller 的一个 Action
//....
}
其他使用方式细读手册 :)
拦截器的配置方法见 Interceptor 有关章节 章节
2017-02-06 10:47
源码中这样写到:
//2.0 这样的__________________________________________
public FileRender(File file) {
this.file = file;
}
public FileRender(String fileName) {
fileName = fileName.startsWith("/") ? webRootPath + fileName : fileDownloadPath + fileName;
this.file = new File(fileName);
}
//3.0 这样的__________________________________________
public FileRender(File file) {
if (file == null) {
throw new IllegalArgumentException("file can not be null.");
}
this.file = file;
}
public FileRender(String fileName) {
if (StrKit.isBlank(fileName)) {
throw new IllegalArgumentException("fileName can not be blank.");
}
String fullFileName;
fileName = fileName.trim();
if (fileName.startsWith("/") || fileName.startsWith("\\")) {
if (baseDownloadPath.equals("/")) {
fullFileName = fileName;
} else {
fullFileName = baseDownloadPath + fileName;
}
} else {
fullFileName = baseDownloadPath + File.separator + fileName;
}
this.file = new File(fullFileName);
}
2017-02-06 10:34
@杜福忠 ps: 不要put, 只remove就可以了, 源码中已经put了,
/**
* Find model by cache.
* @see #find(String, Object...)
* @param cacheName the cache name
* @param key the key used to get data from cache
* @return the list of Model
*/
public List findByCache(String cacheName, Object key, String sql, Object... paras) {
ICache cache = getConfig().getCache();
List result = cache.get(cacheName, key);
if (result == null) { // 这里
result = find(sql, paras);
cache.put(cacheName, key, result); // 这里
}
return result;
}