0

css

.myStyle {
height: 10px;
background-image: url(../myImage.png);
}

jsp

<img class=myStyle src=<% imageurl != null ? imageurl: (bacground-image property from my css)%> >

Is there any way to achieve this? I don't want to hardcode the default url in my code, to allow change of default image by just changing the css property.

3
  • Can you explain why you do not use a property file instead? Commented Jan 14, 2013 at 6:03
  • I want to have a single point of change, rest of the styling properties including images can be updated by just updating the css.. this is just a peculiar case i have. Commented Jan 14, 2013 at 6:05
  • That is what property files are for in jsp. Here you will need JavaScript unless you want to read the actual CSS file in the jsp. easywayserver.com/blog/java-resourcebundle-properties-file-jsp Commented Jan 14, 2013 at 6:13

1 Answer 1

0

What you really want is a property file - for example like this http://www.easywayserver.com/blog/java-resourcebundle-properties-file-jsp/

# image default
myimage.default=../myImage.png

and

<%@ page language="java" import="java.util.*" %>
<%
ResourceBundle resource = ResourceBundle.getBundle("commonVariable");
/// commonVariable.properties file will be in WEB-INF/classess  folder

String defaultImage=resource.getString("myimage.default");
.
.
.
if (imageurl==null) imageurl=defaultImage;

%>

<img class="myStyle" src="<%= imageurl  %>" />

Alternative if you must

https://stackoverflow.com/a/2104580/295783

function setImageFromBG(img) {
  var style = img.currentStyle || window.getComputedStyle(img, false),
  img.src = style.backgroundImage.slice(4, -1);
}

<img class="myStyle" src="<%= imageurl %>" onerror="setImageFromBG(this)" />

May loop if the bgimage code fails to give a good image too

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.