Ali
Ali

Reputation: 267077

How to serve static resources (images, css, etc) using Struts 2?

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

Upvotes: 0

Views: 4234

Answers (2)

lpratlong
lpratlong

Reputation: 1461

(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).

Upvotes: 0

Lee Meador
Lee Meador

Reputation: 12985

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.

Upvotes: 1

Related Questions