简体   繁体   中英

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 eg with a custom jsp tag?

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']}"  />

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM