简体   繁体   English

如何在开发和生产检票口添加不同的Javascript

[英]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: 我有一个wicket应用程序,我在其中添加了标记html中的javascript文件:

<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. 我的javascript文件没有放在我的.java或.html文件旁边,它们位于服务器的不同位置,可以在之前的脚本声明中看到。

My question is: Is it possible to add these javascript files depending on the application mode? 我的问题是:是否可以根据应用程序模式添加这些javascript文件? IE if the application is in development mode, load one javascript file, if it is in production load this other one. IE如果应用程序处于开发模式,加载一个javascript文件,如果它在生产中加载另一个。

Thanks! 谢谢!

PS: the idea is to load "min" version on production but the extended files on development so debugging becomes posible PS:想法是在生产中加载“min”版本,但在开发时加载扩展文件,以便调试变得可行

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 注意 :在这里观察不同的答案我重新声明:问题是找不到wicket应用程序处于开发或部署模式时,我知道,但是关于如何更改html标记或添加不同的JavaScript资源

extendig the answer of @rotsch you can do it in wicket 1.5 with : 延伸@rotsch的答案你可以在wicket 1.5中做到:

@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 . 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/ 您可以使用pack:tag压缩所有资源: 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. 在web.xml / .properties文件中,您可以根据生产模式指定是否打包。

I set a property in a properties file with I add to the path when starting the VM. 我在properties file设置了一个properties file ,我在启动VM时添加了路径。

Then I do a if else similar to the PHP answer. 然后我做了一个类似于PHP答案的if。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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