Doesn't the code you have posted already achieve this?
You could potentially put this logic into a custom tag to make your
jsp pages easier to read.
The tag would take an attribute name, and a value for that attribute, but only render the attr="value" if a non-null value was passed.
So you could get
It just cuts down on the boiler-plate that was building up there.
You could then go further and have a <custom:mediaTag properties="${properties}"/> which could be responsible for rendering the entire div tag.