If assertion in JSTL

I want to do an if-stat in JSTL .. This is what I want to do:

<c:set var="sex" value="${param.sex}"/>

 <c:if test="$(param.sex=='male')" >
     //set the sex to zero
 </c:if>
 <c:if test="$(param.sex=='female')" >
     //set the sex to one
 </c:if>

      

and then use gender in where clause like this

<sql:query dataSource="${dbcon}" var="result">
 select firstname,lastname from members where sex = ?
 <sql:param value="${sex}"></sql:param>

      

+3


source to share


3 answers


Use advanced if the condition will be easier



+1


source


You don't need to do zero / one bit unless you use the $ {sex} variable elsewhere besides your sql: query. Your problem might collapse:

<c:set var="sex" value="${param.sex}"/>

<c:if test="$(param.sex=='male')" >
    //set the sex to zero
</c:if>
<c:if test="$(param.sex=='female')" >
   //set the sex to one
</c:if>
...
<sql:query dataSource="${dbcon}" var="result">
    select firstname,lastname from members where sex = ?
    <sql:param value="${sex}">
</sql:param>

      

Before that, it's simple:

<sql:query dataSource="${dbcon}" var="result">
    select firstname,lastname from members where sex = ?
    <sql:param value="${param.sex == 'male' ? 0 : 1}">
</sql:param>

      

That you can use param.sex directly in the sql: statement, and you can use the "ternary statement" instead of using the if statement in this situation.

Google for more information on the "JSTL ternary operator" or look here (scroll down to Ternary Operators): http://davidensinger.com/2014/07/fun-with-jstl-in-jsps/




UPDATE

To see the ternary operator in action and verify that the param.sex value is the value you expect, you can simply type this:

${param.sex == 'male' ? 0 : 1}

      

On a blank line and should print either 0 or 1 on the screen.

+1


source


 select firstname, lastname from members where sex = <% = Enter variable name accepted for sex in this EL%>  

0


source







All Articles