Ruby
Ruby

Reputation: 893

Setting background image dynamically in wicket application

In my wicket application there are pages for users depending upon their role and on different criteria. In my database I am storing the path of image to be used as a background for that user. Every user has a unique page. I know I can add read image if I do something like this :

<img wicket:id="img">

and corresponding to this I am writing the code which will get image for me .

But how can I set the image as body background dynamically .I am pretty much new to wicket .Can anybody have a clue how to do that ?

Upvotes: 3

Views: 1045

Answers (1)

Robert Niestroj
Robert Niestroj

Reputation: 16131

In your page you can do it with some header contribution:

   @Override
   public void renderHead(IHeaderResponse response) {
      super.renderHead(response);
      response.render(CssHeaderItem.forCSS("body{ background-image: url('" + getBackgroundBodyImagePath() + "');};", "uniqueBodyBackground"));
   }

Or you could assign a wicket id to your <body> element and add an AttributeModifier like this:

   @Override
   protected void onInitialize() {
      super.onInitialize();
      bodyElement.add(AttributeModifier.replace("style", "background-image: url(" + getBackgroundBodyImagePath() + \"');"));
   } 

Upvotes: 4

Related Questions