Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve static files (JavaScript) with Struts 2

I want to put some JavaScript files in one of my packages and make Struts serve them like /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?

like image 466
alamar Avatar asked Oct 28 '10 18:10

alamar


1 Answers

Struts2 can serve static content out of the box. By default static content is being served by DefaultStaticContentLoader an implementation of StaticContentLoader. It automatically searches the following packages:

  • org.apache.struts2.static
  • template
  • static
  • org.apache.struts2.interceptor.debugging

You can add additional packages to be searched in filter init parameter named "packages".

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
    <init-param>
        <param-name>packages</param-name>
        <param-value>some.package another.one</param-value>
    </init-param>
</filter>

You can add more than one package, use comma or space or tab or new line as separator.

BTW you can control whether static content is being cached by a browser or not with this constant:

struts.serve.static.browserCache
like image 117
Aleksandr M Avatar answered Oct 15 '22 08:10

Aleksandr M