简体   繁体   English

比较Grails GSP中的字符串

[英]Comparing Strings in Grails GSP

I'm stumped by this problem. 我为这个问题感到困惑。 I'm trying to display a class if a certain string is equals to the logged in username. 如果某个字符串等于登录的用户名,我试图显示一个类。 However it doesn't seem to ever evaluate to true. 但是,它似乎从未评估为真。 Here's the code in gsp 这是gsp中的代码

<g:if test="${it.from.username == sec.loggedInUserInfo(field: 'username')}">
   <div class="direct-chat-msg right">
</g:if>
<g:else>
   <div class="direct-chat-msg">
</g:else>

I also tried using this method 我也尝试过使用这种方法

<div class="direct-chat-msg ${(it.from.username == sec.loggedInUserInfo(field: 'username')) ? 'right' : ''}">

However nothing I do can get the 'right' class to show up in the div. 但是我什么也做不了,可以在div中显示“正确的”类。 Just for good measure, I printed out the values of both classes in my gsp in hidden fields. 出于良好的考虑,我在隐藏字段中的gsp中打印了两个类的值。

<input type="hidden" value="${it.from.username}"/>
<input type="hidden" value="${sec.loggedInUserInfo(field: 'username')}"/>

And the values are exactly the same 值是完全一样的

<input type="hidden" value="u***@gmail.com">
<input type="hidden" value="u***@gmail.com">

I've tried several combinations of string comparison .equals() , calling .toString() on both, also trying as String . 我尝试了几种字符串比较.equals()组合,在两者上都调用.toString() ,也尝试as String however nothing seems to be working. 但是似乎没有任何作用。 What could the issue be? 可能是什么问题?

I have tried passing the logged in user object in the Model from the controller, and just calling loggedInUser.username and it works. 我尝试从控制器传递模型中登录的用户对象,然后仅调用loggedInUser.username So my question now is, what kind of object is returned by spring security? 所以我现在的问题是,Spring Security返回哪种对象?

The result of sec.loggedInUserInfo(field: 'username')} is an HTML encoded string. sec.loggedInUserInfo(field: 'username')}是HTML编码的字符串。 Spring security calls encodeAsHTML() before returning 'username' value. Spring安全性在返回'username'值之前调用encodeAsHTML() Visually they look the same but are they equal? 视觉上它们看起来相同,但相等吗? Apparently not! 显然不是!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM