0

I am trying to set align on a div like this :

<div align="${shoudSetAlign ? myObject.memberVariable'someString' : ''}" </div>

My requirement is to set align only if shoudSetAlign's value is true and don't set any value at all if shoudSetAlign's value is false. If I implement above code, what's happening is when shoudSetAlign's value is false, it's setting empty value to shoudSetAlign which I don't want. The reason for this kind of requirement is that on client side, we check if align is set or not. If it's set, we leave it as it is. But if it's not set, some value is set on client side behind some logic. Also I am facing error while appending string to myObject.memberVariable like this :

"${shoudSetAlign ? myObject.memberVariable'someString' : ''}"

Please advise me as solution for this.

Manu Arora
  • 47
  • 1
  • 2
  • 5
  • Can you post your complete jsp page ? – Avijit Barua Nov 16 '18 at 17:20
  • Possible duplicate of [Conditionally set an attribute on an element with JSP Documents (JSPX)](https://stackoverflow.com/questions/157005/conditionally-set-an-attribute-on-an-element-with-jsp-documents-jspx) – Vadzim Apr 05 '19 at 18:59

1 Answers1

1

Add tag library

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

and try this code

<c:if test="${shoudSetAlign}">  
   <div align="${ myObject.memberVariable}" ></div>  
</c:if> 
<c:if test="${!shoudSetAlign}">  
   <div ></div>  
</c:if> 

and if yo want append string means add <c:set var="myVar" value="someString" /> access using <div align="${ myObject.memberVariable}${ myVar}" ></div>

Ajish
  • 81
  • 3