admin管理员组文章数量:1410737
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 paring 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 pared 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 parison method for this?
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 paring 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 pared 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 parison method for this?
Share asked Aug 24, 2017 at 7:08 Akshay PethaniAkshay Pethani 2,6002 gold badges32 silver badges44 bronze badges 8-
3
Use
'${isAdmin}' === 'true'
. – alexmac Commented Aug 24, 2017 at 7:11 - @alexmac Thank you. Your solution is correct. – Akshay Pethani 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. – user5734311 Commented Aug 24, 2017 at 7:14 - @ChrisG but without wrapping it in single quote gives pile time error – Akshay Pethani 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. – Suresh Atta Commented Aug 24, 2017 at 7:18
4 Answers
Reset to default 3As 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') { ... }
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 mented, 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 ??");
}
Why not just use the value without quotes?
if (${isAdmin}) {
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>
本文标签: javaComparison of JavaScript Boolean variables in IF statementsStack Overflow
版权声明:本文标题:java - Comparison of JavaScript Boolean variables in IF statements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744929978a2632837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论