Integrating Prototype Ajax engine with Struts Application
You can achieve Ajax effect in any struts application (Struts1.x ) with prototype in a very simple way.
In normal Struts application we generally use DispatchAction to make our web app and life easy. In general struts controller gets a request and delegates it to corresponding action/dispatchAction subclasses. Depending on the method name parameter our target method gets invoked and after processing of some business/domain objects finally forwards a mapping(configured in struts-config.xml) towards the view tier and a JSP gets rendered in our thin client as a response.
Now when we think of Ajax in this kind of application model, we can consider two types of implementation of Ajax: Partial Page Rendering and Partial Response update.
Problem: A form based application has too many fields to be filled in by the user but majority of the fields or group of fields are optional and it takes time to render those fields and thus the entire page.
JSP Solution: Break the optional fields and mandatory fields in two separate groups. And Break the JSP page in two and use JSP:INCLUDE to save some jsp rendering time. You can save approximately 5-7% of your loading time.
Ajax Solution Have a different method in your DispatchAction for example renderOptionFields(…….)
And return a different forward mapping and in turn return a different JSP but a partial one which render only the optional fields.
And in that partial JSP page you just only include your necessary TLDs and render all the struts html tags with name attribute mentioned.
And now in your main JSP have a button called option fields having a javascript function call to render the option fields.
Including prototype.js is obvious and have the code as follows:
<input type="button" value="Optional Fields" onClick="renderOptionalFields()"/> <div id="optionalFieldsContainer"></div>
<script>function renderOptionalFields(){ var url ="/appcontext/yourDispatch.action?method= renderOptionFields"; var params =""; var updatePage = new Ajax.Updater(''optionalFieldsContainer'',url, { method:''get'', parameters:params, onFailure:reportError } );}function reportError(request) { alert("Error Updaing Page");}</script>