25

Can you do an if-then-else statement inside a JSP expression?

EDIT : Specifically I was looking for a JSP solution, not a JSTL solution. But some JSTL solutions below are highly rated and are very welcome. Just please do not vote me down for a duplicate question because one has already been asked about JSTL.

Xonatron
  • 15,622
  • 30
  • 70
  • 84

4 Answers4

37

In JSP EL 2.0, you can do that using the ternary operator. For instance:

<option value="1" ${param.number == 1 ? 'selected' : ''}>First option</option>

What it does is it checks JSP's param's number variable. If it's 1, then selected is substituted. otherwise, nothing.

Ivan Zarea
  • 2,174
  • 15
  • 13
  • I like this. But I'm not sure what it is. It's not a JSP Expression. Can I access variables from my JSP Scriptlet in here? (I'm trying right now and failing.) – Xonatron Jan 25 '12 at 18:44
  • The `param` contains all the variables from the scriptlet. `param.number` will just access the value of the `number`. – Ivan Zarea Jan 25 '12 at 18:55
  • I have a `boolean even` in my JSP Scriplet, but `param.even` does not seem to access it. `even` switches from `true` to `false` each row, and when i use `${param.even == true ? 'even' : 'odd'}` or `${param.even ? 'even' : 'odd'}` all I get is `odd` for the output over and over. – Xonatron Jan 25 '12 at 19:03
23

If you are using JSTL you can do choose-when-otherwise.

<c:choose>
  <c:when test="condition"></c:when>
  <c:when test="condition2"></c:when>
  <c:otherwise></c:otherwise>
</c:choose>

For more information on JSTL try here.

Michael
  • 2,460
  • 3
  • 27
  • 47
  • Thank you for this, but I was looking for an JSP solution not a JSTL solution. I'm ultimately not sure if it matters or not for my project, but I am yet unfamiliar with JSTL. – Xonatron Jan 25 '12 at 18:47
  • 1
    JSTL adds a lot of functionality... if you are doing your UI with JSPs then it probably would be worth looking at. It's pretty simple to get started with. – Michael Jan 25 '12 at 18:49
  • Thanks for this. I may have to look into it. It appears to handle escaping web-based strings easily too. Feels like it's designed very well for web apps. Like I said, I have never used it, so this is just what it appears like. I'm actually a bit confused between JSP Scriptlets and JSP Expressions and JSTL. As I use them all I expect to get a grip of the purpose and place of each. A choice between one and another so far appears to be for elegance. Which is the purpose of my question actually. – Xonatron Jan 25 '12 at 19:05
  • 1
    I think for the most part it is considered to be good practice to avoid putting Java code directly inside the JSPs. Keeping all of that in the Spring Controllers then only doing the minimum logic using something like JSTL in a JSP (or another view technology entirely). – Michael Jan 25 '12 at 19:16
  • 1
    This is the whole idea of the Model-View-Controller paradigm. Good luck with your coding! :) – Michael Jan 25 '12 at 19:16
  • Thank you again. This is exactly what I'm building a Spring MVC web app, and I can already see this makes sense. The functionality I needed here was only to get odd rows (to color them differently), so I wanted the solution to be elegant. – Xonatron Jan 25 '12 at 19:17
  • No worries! Glad to share my knowledge (limited as it may be). – Michael Jan 25 '12 at 20:23
14

You can wrap your html code with jsp tags like this:

<% if (condition) { %>
<div>Condition is true!</div>
<% } else { %>
<div>Condition is false</div>
<% } %>
Kevin
  • 149
  • 2
  • 1
    I like this too. This is actually my current solution! But it's using JSP Scriptlets, not JSP Expressions. I just wanted the most elegant solution. – Xonatron Jan 25 '12 at 18:46
1

there is a different way I use because Eclipse keep telling me that it can't resolve a variable which is coming from the servlet while executing which is below (PS: Am new to JSP):

${if(var != null)"text to print"}
${if(var != null)var}  to print a variable 
${if(var != null)"text to print"}

the result will be like

text to print <var value here> text to print

Saad Bouteraa
  • 119
  • 1
  • 3