2

In my Spring MVC project I am getting the one Boolean variable as a model attribute from the controller in my JSP view. based on the Boolean variable's value I have to execute the Js code. So I am comparing the value in the if statement. but it is not giving me the correct output:

The code is something like this:

In spring controller the Boolean variable is:

boolean isAdmin;

which is recieved by the JSP view and I can access it as follows:

$(isAdmin)

In my JSP view I am doing as follows:

<script>
  alert('${isAdmin}')  // which gives the correct value .i.e : true / false

  if('${isAdmin}'){

   // some code
  }

</script>

So in the above if statement if the value of the isAdmin is false than it should not be execute if block but it does executed even the value is true.

I also compared the value of the Boolean variable as follow:

if('${isAdmin}' == true )
if('${isAdmin}' === true) 

But these both are also not working.

So correct me where I am doing the wrong? and what is the correct comparison method for this?

8
  • 3
    Use '${isAdmin}' === 'true'. Commented Aug 24, 2017 at 7:11
  • @alexmac Thank you. Your solution is correct. Commented Aug 24, 2017 at 7:14
  • 1
    The answer is in your question. You're wrapping it in single quotes, which means it's not true but 'true', which is text. Commented Aug 24, 2017 at 7:14
  • @ChrisG but without wrapping it in single quote gives compile time error Commented Aug 24, 2017 at 7:17
  • 2
    @ChrisG Yes, the reason for the single quote is that EL expressions should be wrapped by quotes. Commented Aug 24, 2017 at 7:18

4 Answers 4

3

As far as I understand your scenario, you have a boolean variable isAdmin in the server side (JSP). You want to inject it in a javascript code, but it is not working, because you are creating a string literal instead of a boolean one:

if ('true') {
  console.log('This always executes');
}

if('false') {
  console.log('This always executes, too');
}

This happens because any non empty string is considered a "truthy" value by Javascript.Remove the quotes and it will work. But for the sake of cleanliness, I would move the Javascript code to its own file and I will create a hidden field in the HTML code to store the isAdmin value:

<input id="isAdminFlag" type="hidden" value="${isAdmin}" />


if ($('isAdminFlag').val() == 'true') { ... }
Sign up to request clarification or add additional context in comments.

2 Comments

Anyway in the end you checking against 'true'. Why not directly doing it and having extra input field ?
It's one of way injecting server side generated value in JS code, Second way is using <![CDATA[ ]]>. It works with Theamleaf, I didn't tested it in JSP
2

Since the resulted value is a String and you are expecting a boolean, '${isAdmin}' should be check against a boolean String value. As already someone commented, it should be '${isAdmin}' === 'true'.

if('${isAdmin}' === 'true') {
  // TODO
}

The reason why it is always executing is Javascript truthify the if expression. For example if("false") is evalutes to true and executes

if("false"){
alert("What ??");
}

Comments

1

Why not just use the value without quotes?

if (${isAdmin}) {

3 Comments

Yes it gives the compile time error in <script> tag
@AkshayPethani, could you use <%= ${isAdmin}) %>?
@NinaScholz I tried this too. But it also not worked.
0

Your rendered js code is wrong. Rendering engine should put the value of 'isAdmin' in js code, but actually not doing. You need to add your script like this

 <script>
<![CDATA[
     var isTrueSet = ('${isAdmin}'=== 'true');

      if(isTrueSet ){
          // some code
  }
]]>
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.