Skip to content

Using the AMD Optimizer via JSP's

rbackhouse edited this page Mar 10, 2012 · 3 revisions

Using the AMD Optimizer via JSP's

The following gist provides an example JSP that demonstrates how to use the Optimizer. It shows a simple example that creates a Calendar widget on the page via a declarative div tag reference.You can place this JSP in the root of the Web Application directory.

https://gist.github.com/1307484

Here is a gist for the Calendar AMD module that loads the calendar widget. It can placed in the lib/amdtest directory

https://gist.github.com/1307486

<%@ page import="org.dojotoolkit.optimizer.JSOptimizer" %>
<%@ page import="org.dojotoolkit.optimizer.servlet.JSURLGenerator" %>
<%@ page import="org.dojotoolkit.json.JSONParser" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.io.StringReader" %>
<%@ page import="java.io.IOException" %>

The above top 2 lines show the 2 java classes that are required to use the Optimizer. The JSOptimizer class is used to obtain the optimization data for modules that are to be loaded via the page. The JSURLGenerator class is used to generate the URL's for the page to use. There is one instance of the JSOptimizer class per Web Application while an instance of JSURLGenerator should be created per page load.

<%
    JSOptimizer jsOptimizer = (JSOptimizer)pageContext.getServletContext().getAttribute("org.dojotoolkit.optimizer.JSOptimizer");
    if (jsOptimizer == null) {
        throw new JspException("A JSOptimizer  has not been loaded into the servlet context");
    }
    JSURLGenerator urlGenerator = new JSURLGenerator(jsOptimizer, request.getLocale(), request.getContextPath());
    String configString = "{'packages': ["+
                          "{'name': 'dojo', 'location': 'dojo', 'main':'main'},"+
                          "{'name': 'dijit', 'location': 'dijit', 'main':'main'},"+
                          "{'name': 'dojox', 'location': 'dojox', 'main':'main'}"+
                          "]}";
    Map<String, Object> cfg = null;        
    try {        
        cfg = (Map<String, Object>)JSONParser.parse(new StringReader(configString));
    } catch (IOException e) {
        throw new JspException(e);
    }
%>
<script type="text/javascript" src="<%=urlGenerator.generateURL("amdtest/Calendar")%>"/></script>   
<script type="text/javascript">
    var dojoConfig = {
        locale : "<%=request.getLocale().toString().toLowerCase().replace('_', '-')%>",
        has:{
            "dojo-1x-base":1
        }
    };
    zazl({
        paths: {
            amdtest: "lib/amdtest"
        },
        packages: [
            {
                name: 'dojo',
                location: 'lib/dojo',
                main:'main'
            },
            {
                name: 'dijit',
                location: 'lib/dijit',
                main:'main'
            },
            {
                name: 'dojox',
                location: 'lib/dojox',
                main:'main'
            }
        ]
    }, 
    ["amdtest/Calendar"], 
    function(calendar) {
        console.log("done");
    });
</script>

The code above demonstrates obtaining the JSOptimizer singleton and creating and instance of JSURLGenerator. It also shows how the JSURLGenerator instance is used to create the script tag URL. It can be used multiple times within a page to generate multiple script tags if it is required to reuse optimization data across different pages. The js code afterwards shows how to call the AMD loader to instantiate the code for the page.

Clone this wiki locally