在Web应用程序框架(检票口)中正确加载资源(javascript)? [英] Load resources (javascript) in a web application framework (wicket) correctly?

查看:47
本文介绍了在Web应用程序框架(检票口)中正确加载资源(javascript)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Apache Wicket用作我的webapp框架,并且具有以下结构:

I am using Apache Wicket as my webapp framework and I have a following structure:

我需要什么:

1)应该为我的自定义Java类Process.java加载并读取javascript文件Rules.js.因为在我的Process.java中,我有这样的东西:

1) The javascript file Rules.js should be loaded and readable for my Custom Java Class Process.java. Because in my Process.java I have something like this:

private String readFile(String filename) throws IOException {

    File file = new File("PATH");
    //reads the file and returns a string
    ...
}

第一个问题是:当我的资源Rules.js位于资源文件夹或任何其他包文件夹中时,我应该使用什么路径而不是"PATH"?

2)Process.java不仅读取文件,而且还使用json操纵我的Rules.js文件.因此,应将以下行添加到Rules.js文件中:

2) The Process.java not only reads the file, it also manipulates my Rules.js file for example with json. So the following line should be added to the Rules.js file:

var object = {JSON STRING};

我已经知道该怎么做了.

I know how to do this already.

3)操作Rules.js文件后,当然应该自动对其进行更新.第二个问题是:我还应该添加什么以及在应用程序中的位置,因此Rules.js是一个文件,可用于应用程序中的所有必需类,并且在会话期间始终保持最新状态.

3) After the manipulation of the Rules.js file it should automatically be updated of course. So the second question would be: What else should I add and where to my application so the Rules.js is a file that is aviable for all needed classes in my application and is always up to date during the session.

我已经尝试了很多事情,但是这里缺少我无法弄清的东西...

I tried a lot of things already, but I am missing here something that I cant figure out...

推荐答案

如果要保留 IResource ,甚至可以使用 ContextRelativeResource Web应用程序根目录上的> rules.js 文件.

You can create your own IResource or even use the ContextRelativeResource if you want to keep your rules.js file on the webapp root.

在您的 WebApplication 上,您必须将资源注册为共享资源:

At your WebApplication you have to register the resource as a shared resource:

    @Override
    public void init() {
        super.init();
        getSharedResources().add(RULES_RESOURCE_KEY, new ContextRelativeResource(RULES_CONTEXT_FILE));
    }

    public static ResourceReference getRulesResourceReference() {
        return Application.get().getSharedResources().get(Application.class, RULES_RESOURCE_KEY, null, null, null, true);
    }

这里是修改 rules.js 文件的示例方法.

Here an example method that modifies the rules.js file.

    public static void changeRules() {    
        try {
            // Get a context URL
            URL rules = WebApplication.get()
                     .getServletContext()
                     .getResource(RULES_CONTEXT_FILE);

            // Update the file
            OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(new File(rules.toURI())));
            out.write("var rules=\"" + Long.toHexString(System.currentTimeMillis()) + "\" ");
            out.close();

        } catch (Exception e) {
            throw new WicketRuntimeException(e);
        }

    }

在您的 Home 类上或需要更新的地方 rules.js :

At your HomePage class or wherever you need update the rules.js:

    public HomePage(final PageParameters parameters) {
        super(parameters);

        add(new Link("changerules") {
            @Override
            public void onClick() {
                WicketApplication.changeRules();
            }
        });
    }

在具有 Form 的页面上,添加 JavaScriptHeaderItem :

At the page that you have your Form add a JavaScriptHeaderItem:

    @Override
    public void renderHead(IHeaderResponse response) {
        response.render(
                JavaScriptHeaderItem.forReference(WicketApplication.getRulesResourceReference())
        );
    }

要测试此示例的HTML:

The HTML to run this example if you want to test it:

    <html>
        <body onload="alert(rules);">
            <a wicket:id="changerules" href="#">Change rules files</a>
        </body>
    </html>

不要忘记在您的 webapp 文件夹中创建 rules.js 的初始版本.

And don't forget to create an initial version of rules.js at your webapp folder.

我认为更新 webapp 文件夹中的文件不是一个好主意,因为容器可以在重新部署时将其替换为初始版本.因此,最好使用自己的IResource并将文件放在外部文件夹中.如果您检查此帖子需要更多详细信息.

I think that it's not a good idea to update the files in the webapp folder, because the container can replace it for the initial version at redeploy time. So it's better to use your own IResource and put the file in an external folder. Check this post if you need more details.

这篇关于在Web应用程序框架(检票口)中正确加载资源(javascript)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆