浏览 2007 次
锁定老帖子 主题:打造Tapestry5中的智能的错误页面。
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-09-18
http://wiki.apache.org/tapestry/Tapestry5ExceptionPage 上面这篇文章告诉我们怎样替换掉tapestry自带的error page。 但是我们需要更灵活的方式。 OK, 下面开始写代码。 我们在AppModule class 添加以下代码段。 // handle RedirectException public static RequestExceptionHandler decorateRequestExceptionHandler( final Object delegate, final Response response, final RequestPageCache requestPageCache, final LinkFactory linkFactory, final ComponentClassResolver resolver, @Symbol(SymbolConstants.PRODUCTION_MODE) final boolean productionMode, final PageResponseRenderer renderer ) { return new RequestExceptionHandler() { public void handleRequestException(Throwable exception) throws IOException { // check if wrapped Throwable cause = exception; if (exception.getCause() instanceof RedirectException) { cause = exception.getCause(); } //Better way to check if the cause is RedirectException. Sometimes it's wrapped pretty deep.. int i = 0; while(true){ if(cause == null || cause instanceof RedirectException || i > 1000){ break; } i++; cause = cause.getCause(); } // check for redirect if (cause instanceof RedirectException) { // check for class and string RedirectException redirect = (RedirectException)cause; URL url = redirect.getUrl(); if (url != null) { response.sendRedirect(url.toString()); return ; } Link pageLink = redirect.getPageLink(); if (pageLink == null) { // handle Class (see ClassResultProcessor) String pageName = redirect.getMessage(); Class<?> pageClass = redirect.getPageClass(); if (pageClass != null) { pageName = resolver.resolvePageClassNameToPageName(pageClass.getName()); } // handle String (see StringResultProcessor) Page page = requestPageCache.get(pageName); pageLink = linkFactory.createPageLink(page, false); } // handle Link redirect if (pageLink != null) { response.sendRedirect(pageLink.toRedirectURI()); return; } } if (productionMode) { Page page = requestPageCache.get("ProductionExceptionReport"); ExceptionReporter rootComponent = (ExceptionReporter) page.getRootComponent(); rootComponent.reportException(exception); renderer.renderPageResponse(page); }else { // no redirect so pass on the exception ((RequestExceptionHandler)delegate).handleRequestException(exception); } } }; } 我们只要关注64至72行的代码。 64行以上的代码是实现Tapestry4中的RedirectException作用的。我们可以只看64至72行的代码就行了。 这段代码很简单。 如果是productionMode(我们运行时servlet container时加了-Dtapestry.production-mode 参数)的话, 我们去拿"ProductionExceptionReport"页面。 然后调用reportException方法。 最后转到ProductionExceptionReport页面。 如果不是production mode的话,我们使用tapestry默认的RequestExceptionHandler去处理这个异常。 这样tapestry就使用自带的异常页面了。 import java.util.List; import org.apache.tapestry5.Asset; import org.apache.tapestry5.annotations.Property; import org.apache.tapestry5.ioc.annotations.Inject; import org.apache.tapestry5.ioc.services.ExceptionAnalysis; import org.apache.tapestry5.ioc.services.ExceptionAnalyzer; import org.apache.tapestry5.ioc.services.ExceptionInfo; import org.apache.tapestry5.services.ExceptionReporter; public class ProductionExceptionReport implements ExceptionReporter { @Property(write=false) private Throwable exception; @Inject private ExceptionAnalyzer analyzer; @Property private List<ExceptionInfo> stack; public void reportException(Throwable exception) { this.exception = exception; ExceptionAnalysis analysis = analyzer.analyze(exception); stack = analysis.getExceptionInfos(); sendExceptionByEmail(); } private void sendExceptionByEmail() { System.out.println(".......................Send Email .............."); } @Override public Asset[] getCsses() { return new Asset[] {getIndexCssAsset()}; } } ProductionExceptionReport页面实现了tapestry5的ExceptionReporter 接口。 这里要注意的是 ExceptionReporter rootComponent = (ExceptionReporter)page.getRootComponent(); 你不能把它cast成ProductionExceptionReporter, 这是由tapestry5的classloader超成的。 tapestry5文档中有说明。 你可以在reportException方法中做些事情。 比如发邮件通知管理员。 下面是个简单的error page的模板页面。 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <t:layout home="false" errorPage="true" showAddNewSite="false" showSideBar="false" title="literal:Error" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd"> <font color="red">有错误发生,请联系管理员。</font> <br/> <a t:type="pagelink" t:page="Index">重新开始</a> </t:layout> OK。大工告成! 在你开发的环境中启动参数中加上-Dtapestry.production-mode=false 参考: http://wiki.apache.org/tapestry/Tapestry5ExceptionPage http://tapestry.apache.org/tapestry5/cookbook/exceptions.html 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |