Ali
Ali

Reputation: 267077

How to link to images / css files on an external website within Struts 2 JSP results?

I'm working on a struts 2 app. The problem is, all of my static assets (images / CSS, etc.) are hosted on an external URL separate from the rest of my app. I want to be able to store the url to the static assets website as a configuration setting somewhere, and use it in all of my .jsp views, such as:

<img src="{{external}}/images/something.png" />

That should be translated into:

<img src="http://some.site.amazon.com/images/something.png" />

Any ideas how this can be accomplished? If there's no out of the box way to do this in Struts 2, can I create a way myself e.g with a custom jsp tag?

Upvotes: 1

Views: 561

Answers (2)

smajlo
smajlo

Reputation: 972

The easiest way to do this is create jsp file which is included everywhere with:

<s:set var="host">http://some.site.amazon.com</s:set>

and then

<img src="<s:property value="#host"/>/images/something.png" />

More solid way is get this hostname from external resource, db for example and pass it by struts to the form (the same way you providing data to show for user)

Upvotes: 1

KyelJmD
KyelJmD

Reputation: 4732

I don't know if you have configured your struts2 through XML. however if you did you can place this somewhere your ServletContext declaration

<init-param>
  <param-name>website_image</param-name>
  <param-value>http://some.site.amazon.com/images/something.png</param-value>
</init-param>

and in your JSP add this

<img src = "${applicationScope['website_image']}"  />

Upvotes: 1

Related Questions