views:

22

answers:

1

Hello! I want to put some javascript files in one of my packages and make struts serve those on /struts/js/foo.js

Struts does that for files in 'template' package (that's where jquery plugin's files are located, guarded by struts.ui.templateDir option). However I want to put those files into another package; If I redefine struts.ui.templateDir then struts ceases working because it can't find its templates.

So the question is: How to tell struts to serve files in org.foo.some.package.js as /struts/js/whatever.js ?

A: 

Struts2 serves template files that way because they are packaged in a JAR. For files that are local to your web application, you should just serve them from your WAR file (e.g., src/main/webapp if you use the Maven standard directory layout). For example, you can just create a "js" directory in your web root and place your JavaScript files there. Then just reference them as "/yourContextRoot/js/foo.js".

  • What version of Struts2 are you using?
  • What is your action extension configured to be? (e.g., the value of your struts.action.extension property)

In Struts 2.0.x, the default extension was "action", so requests that don't end in "action" should pass through to other filters, servlets, or to the file system automatically. If you change that value to be empty, then Struts2 will match against all URLs and nothing will get through to the file system.

In Struts 2.1.x, the default extension was changed to "action,,", which matches both ".action" and also no extension. This should still pass requests that have an extension other than .action straight through (e.g., css, js, png, etc.)

As of version 2.2.x of Struts2, you can also include a property in the struts.properties (or as a constant in the struts.xml) file, such as:

struts.action.excludePattern=/js/.*

Struts2 will allow any matching requests to pass through.

Any of those approaches should allow you to serve static files from the root of your war file. You should not need to place static web content in your Java package structure.

Steven Benitez
The problem is, I don't have a war file. What I have is some files put as resources into my jars in corresponding packages, and I would like to serve that.
alamar
Struts does that for files in template.- packages, the question is how to make it do that for other files.
alamar