There are two kinds of data which are needed to pass from server to client when page is loaded.
One is environment parameters and one for logged in Priority Club Rewards (PCR) user.
Environment parameters are the collection of variables from server settings and user browser info. Language id, country id, date format, cookieDomain name are the example of them.
PCR object contains the information such as login status(anonymous
,implicitly, explicitly), number of points etc for the user who accesses our web site.
Both data are passed through JSP variable and set to Javascript variables, then client will read those JavaScript variables. Those variables can be view with View Page Source with the broswer. Followings are the code example.
Environment parameters:For env object, JSON is applied to it in the consideration that the value of env will be used in CSS.
In server side of Spring interceptor, convert env object named appMappingElem to Json, then save to request.
JSONObject jsonEnv = JSONObject.fromObject( appMappingElem );
request.setAttribute( ENV_JSON, jsonEnv );
In JSP header Javascript section var envJson = '${envJson}';
In GW T client Java code: public static native String getEnvJson() /*-{
return $wnd.envJson;
}-*/;
public static UrlMappingElem buildMappingElem() {
final String envJson = getEnvJson();
final JSONObject jsonObj = (JSONObject)JSONParser.parse(envJson);
final UrlMappingElem urlMappingElem = new UrlMappingElem();
if (jsonObj.get("brand") != null) {
urlMappingElem.setBrand( getStringValue(jsonObj.get("brand"))) ;
}
if (jsonObj.get("languageId") != null) {
urlMappingElem.setLanguageId( getStringValue(jsonObj.get("languageId"))) ;
}
}
PCR object:RPC utility class is used to impose PCR profile object to Javascript variable through JSP. Other than JSON, the PCR profile object is constructed directly from serialized data.
In server side:String serizlizedString = RPC.encodeResponseForSuccess( PCRProfileService.class.getDeclaredMethod("getPCRProfileBean" ), profileBean);
request.setAttribute(PCR_PROFILE_SESSION_SERIALIZED_KEY, serizlizedString );
In JSP:
var pcrSerializProfile = '${pcrSerializProfile}';
In GWT client side:SerializationStreamFactory factory = (SerializationStreamFactory)GWT.create( PCRProfileService.class );
SerializationStreamReader reader;
reader = factory.createStreamReader( serializedOnServer );
PCRProfileBean pcrProfileBean = (PCRProfileBean)reader.readObject();
Change History:
Author: Michael Wang, initial draft