0

I'm new to Struts 2, and I'm simply looking for a way to serve static resources (e.g images, javascript files, css files, etc). E.g every request starting with /assets should try to load the static resource being referred to.

1) Where do I need to store my images/js files, etc for this to work?

2) And how do I need to configure Struts so that it will load those files?

The project will be run as a .WAR on Tomcat 7, the static resources will be included as part of the WAR

Ali
  • 261,656
  • 265
  • 575
  • 769
  • 1
    I don't do Struts, but mapping a MVC front controller on `/*` is a bad idea. You don't want to have it to completely override the servletcontainer's default servlet. Rather replace it by `*.html` or `*.do` or so. If you've really a hard head in this, read on http://stackoverflow.com/questions/870150/how-to-access-static-resources-when-using-default-servlet/3593513#3593513 and http://stackoverflow.com/questions/4140448/servlet-mapping-vs/4140659#4140659 – BalusC May 08 '13 at 19:42
  • @BalusC Thanks, that was it. Changing the urls to `.do` fixed the issue. – Ali May 08 '13 at 19:55
  • 1
    @BalusC The front in Struts 2 is a filter, FWIW. – Dave Newton May 08 '13 at 21:07

2 Answers2

1

Usually you have your web server serve those directly from the file system. That is, Struts2 isn't doing it itself.

If you supply which one you are using, maybe we can help configure it.

For example, if you are using HTTPD (Apache) web server in front of your Tomcat7, there will be a folder from which HTTPD serves up files. Perhaps /var/www/html is that location for some system. Then you would just put your static content in /var/www/html/assets.

Edit

If the static resources are to be in the war file, you just build the war and put those files in the root of the war file or in some otherwise-unused folder structure beginning in the root of the war.

For example, if you want to reference an image at http://mysite.com/assets/my.png then you put it my.png in the folder /assets in the root of the war file.

From any HTML page in the site, you would refer to that image file this way:

<img src="/assets/m.png" .../>

This uses the default path on the same web site.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Lee Meador
  • 12,829
  • 2
  • 36
  • 42
  • How are you deploying? war file? – Lee Meador May 08 '13 at 18:48
  • Thanks for the edit. The thing is, I don't know which URL the war would be running on. It could be on mysite.com, or mysite.com/appName , etc. How can I refer to the assets so that they will be loaded no matter which URL the WAR runs on? – Ali May 08 '13 at 18:58
  • All right, this method is not working. I need to package the resources in the same WAR as the rest of the code, I can't put the resources seperately in /var/www/html or any other directory. I tested this method just now, but when I try to display an image, using `` I get the `There is no Action mapped for action name abc.jpg.` error page – Ali May 08 '13 at 19:07
  • I did put the assets directory in the root of the WAR alongwith the META-INF and WEB-INF directories. I did this by putting the assets directory in `src/main/webapp` directory, when I build it from Netbeans, the `assets` directory is put in the root of the war – Ali May 08 '13 at 19:11
  • Does it work if you put the full URL `src=http://localhost:8080/AppName/assets/abc.jpg` ? – Lee Meador May 08 '13 at 19:12
  • No, I get the 'No action mapped' page for it at `http://localhost:8084/AppName/assets/abc.jpg` – Ali May 08 '13 at 19:13
  • Wait. If its saying 'no action' that means it is trying to map it to a Struts action. Perhaps the mapping in web.xml is such that it thinks /assets maps to actions. – Lee Meador May 08 '13 at 19:13
  • Yes, that's probably the case, in web.xml its mapping `/*` to struts. – Ali May 08 '13 at 19:18
  • No. You can put a leading path on it. As in `/action/*`. Then you can have some servlets using other paths. – Lee Meador May 08 '13 at 21:02
  • 1
    @ClickUpvote Or provide an exclude pattern to the dispatch filter. – Dave Newton May 08 '13 at 21:05
0

(later answer) Sorry but, because a Java server is a middleware, you should not delegate resources management to Apache. And you should not put your resources in the root of your WAR. This is not a standard architecture. Typically, a Java web project should look like (I use Maven -because it's great-, so I indicate it too) :

src
    -- main
          -- java                <= you put your java sources here
          -- resources           <= you put your configuration or i18n properties files here
          -- webapp              <= here is the "web application" code. The front-end...
                   -- img        <= your images
                   -- js         <= your JS
                   -- css        <= your css
                   -- jsp        <= your JSP
   -- test
          -- etc...
pom.xml

All you need now is to configure your Struts2 and your web.xml (follow tutorial, very simples).

lpratlong
  • 1,421
  • 9
  • 17