Question about Google Web Toolkit (GWT) and pre-processing of dynamic data in HTML page
I'm new to GWT and also fairly new to doing Java web development, using Spring MVC, and am wondering if anyone can help clarify this for me as I have been searching extensively but am still not fully clear on this. When previously doing PHP / Javascript development I would often use PHP to dynamically populate something within the javascript (such as a javascript variable) on page load. It seems to me that this is generally not done with GWT and that instead the GWT app is simply deployed as is to a user's browser and then the GWT client side app makes RPC calls to retrieve any necessary data... Am I right that generally dynamic data isn't put into the GWT page on page load?
I would appreciate any clarification on this as I have already spent a lot of time looking and I don't want to keep trying to do some开发者_Python百科thing (the dynamic data in the GWT code on page load) if its not possible or generally not a good idea. Thanks
GWT apps just output HTML, so you can do anything you like to the page before or while the GWT app is running.
For example your GWT app declare some native JS code like this:
private native doSomething() /*-{
doSomethingJS();
}-*/;
Where doSomething() is called by your GWT app at runtime and it in turn invokes a JS function called doSomethingJS() which could do anything (manipulate the DOM, popup a dialog, validate a field, whatever).
Your HTML file for the GWT app could define this function or pull it in externally. You could even generate your HTML file on the fly using PHP or JSP if you wished.
For example, I could use a JSP file to generate my app's landing page, to define doSomethingJS(), embed some expression, or CSS hack or whatever, e.g.
<%@ include file="taglibs.jsp" %>
<%@ include file="nocache.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>My Application</title>
<script type="text/javascript">
function doSomethingJS() {
window.alert("Hello ${some.element.name}!");
}
</script>
<script type="text/javascript"
src="com.MyApplication.nocache.js"></script>
</head>
<!--[if lt IE 7 ]> <body class="ie6" id="ie6hack"> <![endif]-->
</div>
</body>
</html>
The above also means you can avoid roundtrips if you want. For example, say my GWT app needs to know the user's email address but we don't want to take a hit asking the server at startup.
We could define a native method:
private native String getEmailAddress() /*- { return emailAddress; } -*/;
And our JSP could contain something such as
<script>
var emailAddress = "${user.emailAddr}";
</script>
So we save a round trip because the data is embedded right there in the HTML and available to the app.
GWT translates Java code to JavaScript code, and also provides Java components to make asynchronous communication between the client (JavaScript) and the server (Java) easy.
You can use both the methods you mention to provide data to the client. If the data is known when the page is generated on the server, and is not going to change once the page is loaded on the client, you can simply insert it in the generated page using whatever technique (JSP, FreeMarker, Velocity...). No GWT is involved, but you can insert the data as JavaScript variables nonetheless if you need to manipulate it with JavaScript.
If the data can change dynamically even after the page is loaded on the client, then you can obtain it asynchronously (AJAX) using GWT's features.
So the method you choose depends on the way you use the data once the page is loaded on the client.
The "pure" way of using GWT is to keep the HTML and JS files static. Advantages:
- Can be served by any web server (no need for a servlet container, PHP server or anything like that).
- Allows to re-use a locally cached version of these files (".nocache." files need a quick revalidation, but you still don't have to download them again).
However, sometimes it makes sense to put a little bit of data in the HTML/JS files dynamically, so it's available directly when you load the page, before making any AJAX requests. You decide.
Having the GWT module cooperate with your server code in this manner is very common. GWT has a built-in Dictionary class that's a simple implementation of this scheme. More complex systems that use GWT RPC will use the RPC
utility class to embed more complicated payloads into the page that are reconstituted via a SerializationStreamFactory
. You could also do the same with AutoBeans.
精彩评论