Nicolas Raoul
Nicolas Raoul

Reputation: 60213

Serving static content with Struts2: Tomcat error 404

With Struts2 I can't find a way to serve a static CSS :-/ Newbie question but I could not find any answer on the Internet:

The CSS file is static/styles.css in my WAR file.

Tomcat replies with 404 when I request http://server/myapp/static/styles.css

But it works if I put styles.css at the root of the WAR and request http://server/myapp/styles.css

I tried adding the following to my struts.xml in the only package but still 404:

<action name="/static/*">
  <result>/static/{1}</result>
</action>

Or as a second package:

<package name="static" extends="struts-default" namespace="/static">
  <action name="/static/*">
    <result>/static/{1}</result>
  </action>
</package>

Upvotes: 1

Views: 2711

Answers (2)

Santeri Salminen
Santeri Salminen

Reputation: 36

This is a very old question, but since it wasn't answered and I was struggling with the same problem, I'm sure others might benefit from a solution.

Struts serves static content by default from JARs only, but not from subdirectories outside the JARs. So you need to add this to your struts.xml:

<constant name="struts.action.excludePattern" value=".*/static/.*" />

That prevents anything in the "static" folder from being handled as an action.

Upvotes: 2

Fernando Mart&#237;nez
Fernando Mart&#237;nez

Reputation: 211

You don't need to include anything (action or package). Struts2 serves static content by default.

Check if after deploying your war file, the resource style.css exists in the path you think.

Upvotes: 0

Related Questions