12

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?

Aleksandr M
  • 23,647
  • 12
  • 63
  • 129
alamar
  • 17,588
  • 2
  • 60
  • 89
  • 2
    Have you seen this link: http://struts.apache.org/development/2.x/docs/static-content.html? – Aleksandr M Dec 04 '13 at 18:44
  • @AleksandrM nice, didn't know that. You're a good candidate for the bounty if you care to write a full answer. – alamar Dec 05 '13 at 09:56
  • 2
    why not to use some web-server for this? say Apache or nigix which is much better and efficient way to serve static contents – Umesh Awasthi Dec 08 '13 at 10:47
  • @UmeshAwasthi not if you want to serve content from inside .jar files, handy for internal admin pages. – alamar Dec 12 '13 at 12:27

2 Answers2

8

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
Aleksandr M
  • 23,647
  • 12
  • 63
  • 129
  • How did they manage to have packages named static? It's a reserved word :-/ – alamar Dec 05 '13 at 12:54
  • @alamar: Well, it is for static resources after all. So there is no source files in it. – Aleksandr M Dec 05 '13 at 13:00
  • 1
    @alamar It's not the bare word `static` (nor does it refer to a method or field name). There are other exceptions for "reserved words", for example you could have `String staticWord = "Hello";`. – Elliott Frisch Dec 08 '13 at 11:35
3

One way is to extend the entire template & change the templateDir as you already suggested. Unless one needs to implement a custom template, this is highly over-kill.

But the best way IMHO is to forget /struts/js/foo.js & use any other URL to load the js.

Few Samples :

JSP

WebPages
    |-->myjs.js         (a normal js file)
    |-->mydynamicjs.jsp (a .jsp file containing ONLY javascript code)
    |-->WEB-INF-->xyz.js (another .js file but accessed only through action)

Struts

<action name="myacctionjs">
    <result>/WEB-INF/xyz.js</result>
</action>
coding_idiot
  • 12,860
  • 9
  • 56
  • 111