Coding Ajax
Coding Ajax
{
stringValue : "A string value",
intArray: [10, 20],
map: {
Zaphod : "Just this guy, you know",
Arthur : "Monkey-boy"
},
record: "start a named record"
}
package actions;
import java.util.Map;
import java.util.HashMap;
import com.googlecode.jsonplugin.annotations.JSON;
import org.texturemedia.smarturls.ParentPackage;
import org.texturemedia.smarturls.Result;
@ParentPackage("json-default")
@Result(name="success", type="json", location="")
public class ExampleOutputAction {
// ...
How did we code a UI using
static data?
Entry list
Entry form
List shares data with form
Ajax on Struts
What are Ajax widgets?
What are we coding?
Is there an Ajax architecture?
How can we switch to server-side data?
What about error-handling?
Is there an Ajax architecture?
<script src="my.js" type="text/javascript">
</script>
MY.Contacts = function(){
MY.Contacts.superclass.constructor.call(this);
};
YAHOO.lang.extend(MY.Contacts,YAHOO.yazaar.flev-base);
What's a FLEV widget?
Common business workflow is
Find / List / Edit / View
Or "FLEV"
// more entries
];
key and text are shared attributes
sortable, resizable are List attribute
formClassName, formtitle are Edit
attributes (validation)
MY.Contact.prototype.oResponseSchema = {
fields:["id", "last_name", "first_name",
"extension", "username", "hired", "hours",
"editor"]
};
MY.Contact.prototype.LOCAL_DATA = {
result : [{id: 'c5b6bbb1-66d6-49cb-9db6-
743af6627828', last_name: 'Beeblebrox ',
first_name: 'Zaphod ', extension: '555-123-
4565', username: 'zaphie ', hired:
'04/01/1978', hours: -1, editor: '1'},
// ...
];
my.oEvent.createEvent("contactLoad")
my.oEvent.onContactLoadReturn = function(oData)
{
my.info("Firing contactLoad Event");
my.oEvent.fireEvent("contactLoad", oData);
};
<script type="text/javascript">
var onContentReady = function() {
_Contact = new MY.Contact();
my.oEvent.subscribe("contactLoad",_Contact.load,
_Contact);
my.oEvent.onContactLoadReturn(_Contact.LOCAL_DATA);
};
YAHOO.util.Event.onContentReady("elBody",
onContentReady);
</script>
Ajax on Struts
What are Ajax widgets?
What are we coding?
Is there an Ajax architecture?
How can we switch to server-side data?
What about error-handling?
How can we switch over to
server-side data?
Create an example Action that returns
example data
Create a database Action that returns
persistent data
public class ContactLoadAction {
private List contactLoad = new ArrayList();
@JSON(name="result")
public List getContactLoad() {
contactLoad.add(new Contact(
"c5b6bbb1-66d6-49cb-9db6-743af6627828",
"Beeblebrox",
"Zaphod",
"555-123-4565",
"zaphie",
"04/01/1978",
"-1",
"1"
));
// ...
return contacts;
}
public class Contact {
JavaScript data:
MY.Contact.prototype.LOCAL_DATA =
{result:[{id: 'c5b6bbb1-66d6-49cb-9db6-
743af6627828', last_name: 'Beeblebrox ', first_name:
'Zaphod ' ...
http://developer.yahoo.com/yui/connection/
<script>
var transaction = YAHOO.util.Connect.asyncRequest(
'POST', sUrl, callback, null);
</script>
var callback =
{
success: function(o) {/*success handler code*/},
failure: function(o) {/*failure handler code*/},
argument: [argument1, argument2, argument3]
}
var callback = {
success : function(o) {
my.oEvent.onContactLoadReturn(o.responseText);
}
};
{"result":[
{"editor":"1","extension":"555-123-
4565","first_name":"Zaphod ", ...
]}
MY.Contact.prototype.LOCAL_DATA =
{result:[
// my.oEvents.onContactLoadReturn(_Self.LOCAL_DATA);
YAHOO.util.Connect.asyncRequest('GET', "contact-load.do",
callback, null);
var callback = {
success : function(o) {
var payload = eval("(" + o.responseText + ")");
my.oEvent.onContactLoadReturn(payload);
}
};
http://json.org/json.js
What about JavaScript Hijacking?
/* {"result":[{"editor":"1","extension":"555-123-4565",
...
}]} */
<action name="contact-load"
class="actions.ContactLoadAction">
<result />
</action>
</package>
Ajax on Struts
What are Ajax widgets?
What are we coding?
Is there an Ajax architecture?
How can we switch to server-side data?
What about error-handling?
What about error handling?
var callback = {
success : function(o) {
var payload = eval("(" + o.responseText + ")");
my.oEvent.onContactLoadReturn(payload);
}
};
YAHOO.util.Connect.asyncRequest('POST', "contact-
load.do", callback, null);
What about error handling?
var callback =
{
success: function(o) {/*success handler code*/},
failure: function(o) {/*failure handler code*/},
argument: [argument1, argument2, argument3]
}
How does JSON-RPC handle errors?
{
"version" : "1.1",
"error" : {
"name" : "JSONRPCError",
"code" : 123,
"message" : "An error occurred parsing the request object.",
"error" : {
"name" : "JSONError",
"message" : "Bad array",
"at" : 42,
"text" : "{\"id\":1,\"method\":\"sum\",\"params\":[1,2,3,4,5}"}
}
}
How does Struts handle exceptions?
<global-results>
<result name="error"/>
</global-results>
<global-exception-mappings>
<exception-mapping
exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
How does Struts handle exceptions?
<h2>An unexpected error has occurred</h2>
<p>Please report this error to your system
administrator or appropriate technical support
personnel. Thank you for your cooperation.</p>
<hr/>
<h3>Error Message</h3>
<p>
<s:property value="%{exception.message}"/>
</p>
<hr/>
<h3>Technical Details</h3>
<p>
<s:property value="%{exceptionStack}"/>
</p>
How does Struts handle exceptions?
public String getExceptionMessage() {
ActionContext context = ActionContext.getContext();
Object value = context.getValueStack().
findValue("exception.message");
if (value==null) return null;
return value.toString();
}
getExceptionMessage :” “Whoops!”
getExceptionStack: “java.lang.exception
Whoops! at actions.ContactLoadAction
...
How do widgets handle errors?
<!-- ... -->
</div>
<div id="elError" class="error"></div>
<div