Java – how do I use remote files?
I have a server dedicated to static content, so I don't want to use the resource directory to store JavaScript files, but I don't want to stop using the < H: outputscript / > tag
How to make this tag generate a link to the static server where the file is located instead of res_ NOT_ FOUND. I don't even need JSF to check if the file exists
I tried: < H: outputscript name = "#{requestbean. Staticurl} / JavaScript. JS" / >
To generate: < script type = "text / JavaScript" SRC=“ http://static.server.com/javascript.js ”>< / script>
But it generates: < script type = "text / JavaScript" SRC = "res_not_found" > < / script >
What can I do?
Solution: Daniel gave me a good solution!
I have downloaded the source code of omnifaces and sent it to org omnifaces. resourcehandler. CDNResourceHandle. The createresource (string resourcename, string libraryname) method is modified to:
public Resource createResource(String resourceName,String libraryName) { final Resource resource = wrapped.createResource(resourceName,libraryName); if (cdnResources == null) { return resource; } String resourceId = ((libraryName != null) ? libraryName + ":" : "") + resourceName; String path = cdnResources.get(resourceId); if(path == null){ if(libraryName != null){ resourceId = libraryName + ":%"; path = cdnResources.get(resourceId); if(path == null){ return resource; } path += "/"+resourceName; } else return resource; } final String requestPath = path; return new ResourceWrapper() { @Override public String getRequestPath() { return requestPath; } @Override public Resource getWrapped() { return resource; } }; }
With this change, I can add it to my web XML file
<context-param> <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name> <param-value> somelib2:%=http://cdn.example.com/somelib2,js/script1.js=http://cdn.example.com/js/script1.js,somelib:js/script2.js=http://cdn.example.com/somelib/js/script2.js,otherlib:style.css=http://cdn.example.com/otherlib/style.css,images/logo.png=http://cdn.example.com/logo.png </param-value> </context-param>
Note somelib2:% = http: / / CDN example. COM / somelib2, which points any resources in the somelib2 library to http://cdn.example.com/somelib2 Relative path in, for example:
< h:outputScript name =“js / myjs.js”library =“somelib2”/>
Will output:
< script type =“text / javascript”src =“ http://cdn.example.com/somelib2/js/myjs.js ”>< / script>
This applies to < H: outputscript / > < H: outputstylesheet / > < H: GraphicImage / >, anything that uses resources;
Solution
You can't
Just because < H: outputscript / > can only read the form local resource folder in your network application
All you can do is use omnifaces cdnresourcehandler, which is Javadoc
It allows you to use remote files
Here's some code
To make it work, you need to run it in faces - config Register this handler in XML as follows:
<application> <resource-handler>org.omnifaces.resourcehandler.CDNResourceHandler</resource-handler> </application> <context-param> <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name> <param-value> js/script1.js=http://cdn.example.com/js/script1.js,images/logo.png=http://cdn.example.com/logo.png </param-value> </context-param>
Through the above configuration, the following resources can be:
<h:outputScript name="js/script1.js" /> <h:outputScript library="somelib" name="js/script2.js" /> <h:outputStylesheet library="otherlib" name="style.css" /> <h:graphicImage name="images/logo.png" />