Ternary operators in JSP

Every now and then I come across things I haven’t used before but should have been using.  The ternary operator in JSP is one of them.

It makes it so much easier to write out some text based on a conditional without having to put it in a request attribute explicitly or do an if/then conditional with a possible set operation using the JSTL core library.

Example:

${booleanTest ? 'output if true' : 'output if false'}

This came in really handy for adding an additional css class to a div based on a condition in the JSP in this particular case.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s