Load resources (javascript) in a web application framework (wicket) correctly?

I am using Apache Wicket as my webapp framework and I have a following structure: enter image description here

What I need:

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
    ...
}

So the first question would be: What path instead of "PATH" should I use, when my resource Rules.js is in the resource folder or any other package folder?

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) 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...

Answers:

Answer

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

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);
    }

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);
        }

    }

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();
            }
        });
    }

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

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

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>

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

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.