简体   繁体   中英

How to add different Javascript in development and production wicket

I have a wicket application in which I have added the javascript files within the markup html:

<script src="script/jquery.min.js" type="text/javascript"></script>

My javascript files are not placed beside my .java or .html files, they are in different location in the server as can be seen on previous script declaration.

My question is: Is it possible to add these javascript files depending on the application mode? IE if the application is in development mode, load one javascript file, if it is in production load this other one.

Thanks!

PS: the idea is to load "min" version on production but the extended files on development so debugging becomes posible

NOTE : Watching different answers here I re-state: the problem is not finding when the wicket app is in development or deployment mode, I know that, but is about how to change html markup or adding different JavaScript resources

extendig the answer of @rotsch you can do it in wicket 1.5 with :

@Override
public void renderHead(IHeaderResponse response) {
    if(DEVELOPMENT)
        response.renderString("<script type=\"text/javascript\" src=\"url1\"></script>");
    else
            response.renderString("<script type=\"text/javascript\" src=\"url2\"></script>");

}

https://cwiki.apache.org/WICKET/migration-to-wicket-15.html#MigrationtoWicket1.5-RemovedHeaderContributorandfriends .

You can find out in which mode you are with the following code:

RuntimeConfigurationType.DEPLOYMENT.equals(getApplication().getConfigurationType())

or

RuntimeConfigurationType.DEVELOPMENT.equals(getApplication().getConfigurationType())

I use this directory layout:

resources
|---JQueryResource.java
|---jquery-1.6.4.js
|---jquery-1.6.4.min.js

With this class:

public class JQueryResource {
    /**
     * Must be called in a RequestCycle.
     * 
     * @return Url for the jQuery library.
     */
    public static String getURL() {
        if (Application.get().usesDevelopmentConfig()) {
            Url url =
                    RequestCycle.get().mapUrlFor(
                            new PackageResourceReference(JQueryResource.class, "jquery-1.6.4.js"),
                            null);
            return url.toString();
        } else {
            Url url =
                    RequestCycle.get().mapUrlFor(
                            new PackageResourceReference(JQueryResource.class,
                                    "jquery-1.6.4.min.js"), null);
            return url.toString();
        }
    }
}

This is how I add the resource to my page.

@Override
public void renderHead(IHeaderResponse a_response) {
    a_response.renderJavaScriptReference(JQueryResource.getURL());
}

You could use pack:tag to compress all your resources: http://sourceforge.net/projects/packtag/

In your web.xml/.properties file you can specify whether to pack it or not depending on your production mode.

I set a property in a properties file with I add to the path when starting the VM.

Then I do a if else similar to the PHP answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM