日志文章

2007-06-15

《GWT开发的八大忠告》程序代码

由于篇幅所限,将于2007年6月25日见报的《GWT开发的八大忠告》一文,不能全文刊登相关程序代码,特在此发表,供读者朋友参考。 uc|+_|=K  
g/tsfox>  
代码片段1:带有增强型错误处理机制的onModuleLoad()方法的模板 ~ 6Y@V  
/** vW2:J*Qm  
* 这是GWT入口点方法的模板。 r` /wkdMmR  
*/ tg/paleA  
public void onModuleLoad() { j%m31?+bD  
DebugUtils.initDebugAndErrorHandling(); &o%4mW&S  
Window.setTitle("<Your App Title>"); @.Y [)K  
Window.addWindowCloseListener(new WindowCloseListener() { 0-1wuhGI  
/** IpK{}y]cd  
* Fired after the browser window closes or navigates to a different site. Mvm1cHz-  
* This event cannot be cancelled, and is used mainly to clean up your application wy[<{S#(N  
* state and/or save the state to the server. #1l0>OD<  
*/ ~^*|+8r  
public void onWindowClosed() { "P*s5sh  
Debug.println("App window closed."); ,Z^^;1M  
} \N+eM{ +  
T*%Y  
public String onWindowClosing() { -'AYo)  
return "Application closing..."; CMc,;mA  
} HH"MM  
}); oU #X 2  
// try/catch is necessary here because GWT.setUncaughtExceptionHandler() m 3C$5   
// will work not until onModuleLoad() has returned ivEX p7  
try { jn`I aWH%  
// create and init your startup widgets (e.g., Login Dialog) here Y1G\q=xJ%>  
createStartupWidgets(); JbLOliqtz  
} catch (RuntimeException e) { =bk bF|WL  
GWT.log("Error in 'onModuleLoad()' method", e); J~/U|;c^  
e.printStackTrace(); m$q57tqO  
} ']6EH   
} 5":9=kNO  
C8WDqF<`  
// DebugUtils class used by 'onModuleLoad()' &UA ftcB  
// /wl^7  
import asquare.gwt.debug.client.Debug; tw D Cr/a  
import asquare.gwt.debug.client.DebugConsole; o}X{Q%  
2{Z.Cs&yN  
import com.google.gwt.core.client.GWT; T <c"ie$(  
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler; ~YZ|, H!  
import com.google.gwt.user.client.Window; ?"hw?(-]  
Lc%C?]LWz  
/** }d474(g<  
* Helper class that provides static utility methods to .FBEL`q'Z!  
* support GWT-client-side error reporting. V aAT)3  
*/ kE-ym'!  
public class DebugUtils { \v2hdFKS7  
n3\:/\!:  
private DebugUtils() { nz#k(T2i  
// hide constructor because we are a utility class with static methods only. IHJLt,  
} N#~qKu  
!c|:&Z+  
public static void initDebugAndErrorHandling() { y't9E"  
Debug.enable(); 6*Hh3V'E  
DebugConsole.getInstance().disable(); // can be enabled for web mode , ]?B/w  
GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { 1Q= ,b u  
public void onUncaughtException(final Throwable tracepoint) { ^(Ss;`B^?  
performDefaultErrorHandling(tracepoint); gzXn|rS  
} (=J'h"X  
}); GVe )&=W7  
} N{Ua2%S  
G;1NQV  
public static void performDefaultErrorHandling(final Throwable caught) { d,7{p  
if (caught != null) { LyJLh^9U;  
final String stacktrace = DebugUtils.getStacktraceAsString(caught); 7:>k\6wc+W  
Debug.println(stacktrace); }BZN4  
Window.alert(stacktrace); x8e6"OxJN~  
} else { q]s2Kz  
final String message = "Error ocuured, but we have no further information QWuUW#  
about the cause"; M7P*UTX&t  
Debug.println(message); [ALrH!^Vtt  
Window.alert(message); <fWH84pG  
} q;BX8kl  
}  : #[g  
#>y,.U2<  
public static String getStacktraceAsString(final Throwable tracepoint) { 2H^w/<  
final StackTraceElement[] trace = tracepoint.getStackTrace(); :@1{xFT  
final StringBuffer sbuf = new StringBuffer(2048); bZUX6~xD9!  
sbuf.append(tracepoint.toString()); "mt[xko  
sbuf.append(": at\n"); G_jRp6r0%  
// I cut the stacktrace at depth 7 >m?w w<  
final int length = Math.min(7, trace.length); ^f|W3[L  
for (int i = 0; i <= length; i++) { mo;Jo&o  
sbuf.append(trace.toString()); qJ F4g%@~  
sbuf.append("\n"); n+kYzim&  
} br*.Hu,l  
if (trace.length > 7) { n7&a YLWu  
sbuf.append("..."); jE~7)@)hf  
} Ylr*o0@  
final String stacktrace = sbuf.toString(); q `}I[  
return stacktrace; RaaY,oC  
} 5c+G   
\&DeB5tZG  
} pJ{@s]  
+- le)adw  
./B\(2h'>  
代码片段2:为RemoteServiceServlet类打上补丁,获得更准确的错误信息。 |}C$Wowq3  
public String processCall(String payload) throws SerializationException { 8}Ty"#  
... Ww0 U#'$  
... String myServletException = null; // added by KPB q3 {{mGoq  
try { 2k@ ~~  
..... c"TK{ W  
} catch (InvocationTargetException e) { b/#USaGt7  
// Try to serialize the caught exception if the client is expecting it, 9ASyegvU  
// otherwise log the exception server-side. C!bK\*J-2]  
caught = e; *~c5`- <  
Throwable cause = e.getCause(); m=#.@B  
~fEytosJg  
// added by KPB $xJ1b%w9  
final StringWriter stringWriter = new StringWriter(1000); zA^`S[yC  
e.printStackTrace(new PrintWriter(stringWriter)); QhFJI).  
final StringBuffer sbuf = stringWriter.getBuffer(); FCyV?Qt*0  
int startIndexCause = sbuf.indexOf("Caused by:"); p5/nO6/q0  
if (startIndexCause != -1) { poUx-G)T  
myServletException = "\n" + sbuf.substring(startIndexCause) + "\n"; FW$Ay4  
} bcA;<3 <  
// end additional code m~CX`OWQ  
G } 98  
...... Y79P/K1n  
3(f9}db  
// Let subclasses see the serialized response. zrR|S,   
// T[uIX ,?  
onAfterResponseSerialized(responsePayload); ]9,#m;  
xNJaBqte  
// added by KPB 4YIUmsK  
if (myServletException != null) { miXN<@|!  
responsePayload = myServletException + responsePayload; z`%wlX=0m  
} K O\^ ?F  
// end additional code tDr=Z Q6  
A;Q)2BB#  
return responsePayload; O\Ss*)-  
} 5Gi7M#  
&9"9zqnT  
- ?I%^5  
代码片段3:增强web.xml,以便在宿主模式下访问服务器小程序init参数。 0b'inoa>  
<?xml version="1.0" encoding="UTF-8"?> [d[zd2m6  
<web-app> %1aqLFt J]  
<!-- GWTShellServlet mapping for embedded Tomccat --> Z\TD\,w:.  
<servlet> E+D\3%#Uv^  
<servlet-name>shell</servlet-name> [#90 $ v  
<servlet-class>com.google.gwt.dev.shell.GWTShellServlet</servlet-class> )XO}JlS  
</servlet> Wh 30lzIz  
<servlet-mapping> [ n.WtQ  
<servlet-name>shell</servlet-name> D"w}gDI+zr  
<url-pattern>/*</url-pattern> Oa 2fB*J  
</servlet-mapping> e3 t5_,  
gLI 6_T  
<!-- Display name --> 1 d##ftJ  
<display-name>JUnit2MR</display-name> }#/Gyy  
3 x g71_)n  
<!-Context info for embedded Tomccat to allow access to servlet's init parameters in hosted mode, too--> M]y%0`.  
<context-param> @gyB<>R:  
<param-name>host</param-name> ) P;&xGl  
<param-value>localhost</param-value> w"`j ]A^  
</context-param> +pDM2$ 2y  
<context-param> p*y;Al'  
<param-name>port</param-name> :FXwT{o6  
<param-value>3306</param-value> pO\iuV~D  
</context-param> 5`Yj 5e  
<context-param> {xhBzhXY  
<param-name>db</param-name> OO/FoJXH  
<param-value>fts_db</param-value> ~ 1*8W@-[1  
</context-param> +~"X$ #  
<context-param> S ?j 5UYE  
<param-name>cq_db</param-name> *B"~wl()(  
<param-value><![CDATA[SYM_API_7.0.0/SYM]]></param-value> /}<@AqnXw  
</context-param> %{ '  
$y-ZuzqU  
<servlet> l2u}%oy  
... your module servlet's information goes here... GYFegTBsS  
} $_D>4}#  
F(!p:c  
ZYX{]7,U@%  
代码片段4:服务器端创建PDF文件、客户端使用文件。 <5ru-  
// Client-Code (Widget) #w\ _QfpG  
String pdfLink; mNp;.&}Fa  
dataService.createSummaryPDF(GWT.isScript() ,new AsyncCallback() { W( KlQT  
public void onFailure(final Throwable caught) { M `H!kdg  
DebugUtils.performDefaultErrorHandling(caught); mn+niMla  
} YM!!XU+  
public void onSuccess(final Object result) { m\,C*-  
pdfLink = (String)result; \d1ec  
final String infoAsHtml = "..." + "<P/> + "<A href=\"" + GWT.getModuleBaseURL() + (ha=+6V ;  
pdfLink + "\" target=\"_blank\">Summary (PDF)</A>"; d{]h $x  
final HTML htmlText = new HTML(infoAsHtml); +a#1{b!h%  
... ~E7 uH  
} e}(D&u/  
}); &BQre&6K|  
hQEo\o_a  
// Corresponding Server-Code (Servlet) }wNo q3  
private static final String SUMMARY_PDF_NAME = "summary.pdf"; kR x 3i4  
public String createSummaryPDF(final boolean isScript) { s W"1ysahe  
try { FOP#b'a6  
if (isScript) { =-yUU  
FileCopy.copy(PDF_FILE_TO_COPY, getGwtServletContext().getRealPath( il)z/b$  
"/" + SUMMARY_PDF_NAME)); // FileCopy is a replacement for your real PDF creation A[2o<E.p  
} else { &a")#I)nT  
// the GWTShell servlet expects resources coming from the src.../public folder B'S SyXO?  
String destinationPathname = System.getProperty("user.dir") tU3TtZW  
+ "/src/" + getClass().getName().replace('.', '/').replace("server", "public"); KM-n 9bp  
destinationPathname = destinationPathname.substring(0, LC\Ibz4.  
destinationPathname.lastIndexOf('/')) + SUMMARY_PDF_NAME; d=$pO^f!V  
generatePdfIinPath(destinationPathname); 8'=FV<|$}:  
} RaY4AOEe  
} catch (final IOException e) { R!;1N  
e.printStackTrace(); |*OE J  
} x:Ux$Au  
return SUMMARY_PDF_NAME; Mz!ZK .  
} 8p*iM_+=  
GG$* w,W  
public final getGwtServletContext() { /P/ ]LVWa,  
final HttpServletRequest request = this.getThreadLocalRequest(); ^UXD^#qr=  
return request.getSession().getServletContext();  ON^Nw`  
} =b}J"k  
!o.7]svUQ  


类别: 无分类 |  评论(4) |  浏览(2920) |  收藏
一共有 4 条评论
abuygyld 2009-06-24 17:23 Says:
网站推广软件,博客群发软件,博客群建软件,网络营销软件 信息发布软件,论坛群发软件,网络推广软件,博客推广软件, blog推广软件,网站点评软件,博客自动访问软件. www.fafa2009.cn QQ:840010503. 最好的网站推广软件,最好的博客群发软件,最好的群发软件. 最好的博客群建软件,最好的网络营销软件,最好的网络推广软件. www.fafa2009.cn QQ:840010503. 网站推广软件,博客群建软件, 博客群发软件,网络营销软件, 信息发布软件,群发软件, 网络营销软件. 论坛群发软件,网站推广软件. 博客群建软件.博客群发软件. www.fafa2009.cn QQ:840010503. 网络推广软件.信息发布软件. 论坛群发软件.留言本群发软件. 网站推广软件. 博客推广软件.blog推广软件. 网站推广软件. www.fafa2009.cn QQ:840010503. '500')this.width='500';if(this.height>'400')this.height='400';" border="0" />abuygyld
r1mugt2u 2009-06-20 20:17 Says:
网站推广软件,博客群发软件,博客群建软件,网络营销软件 信息发布软件,论坛群发软件,网络推广软件,博客推广软件, blog推广软件,网站点评软件,博客自动访问软件. www.jdqunfa.cn QQ:840010503. 最好的网站推广软件,最好的博客群发软件,最好的群发软件. 最好的博客群建软件,最好的网络营销软件,最好的网络推广软件. www.jdqunfa.cn QQ:840010503. 网站推广软件,博客群建软件, 博客群发软件,网络营销软件, 信息发布软件,群发软件,网络营销软件. 论坛群发软件,网站推广软件. 博客群建软件.博客群发软件. www.jdqunfa.cn QQ:840010503. 网络推广软件.信息发布软件. 论坛群发软件.留言本群发软件. 网络营销软件.网站推广软件. 博客群建软件.博客群发软件. 网络推广软件.信息发布软件. 博客推广软件.blog推广软件. 网站推广软件.评论群发软件. www.jdqunfa.cn QQ:840010503. '500')this.width='500';if(this.height>'400')this.height='400';" border="0" />r1mugt2u
s 2007-06-18 08:10 Says:
已阅。 . 工作占了生命的一半,但绝不是生活的一半。 —— http://www.ccwblog.com/singlejoe/ —— shiyinger 的一句名言 . 我们从山的这边开始挖掘,你们从山的那边挖掘。 当我们中途相遇时,我们将会完成一个隧道。 而如果我们没有相遇,我们将完成两个隧道。 —— http://www.hp.com.cn/services/success/20030129_05.asp . http://blog.tranbbs.com/user/shaosx/ . http://www.cpms.com.cn . 交通部CPMS推广工作组 北京市海淀区西土城路8号,100088,中国 Tel: +86-010-6238-8353 Fax: +86-010-6207-9514
发表评论
看不清楚,换一张