播放2:如何比较scala模板中的字符串?

nig*_*aph 7 templates scala playframework-2.0

我有一个表单对象,我需要检查字段的值是否等于某个字符串

我正在尝试这个,但它没有用

 @if(sp.pageType.equals("customreCare")) {
   //render this specific div 
  } else {
   //render this other div
  }
Run Code Online (Sandbox Code Playgroud)

但不幸的是它不起作用,这是什么语法?

bie*_*ior 10

使用==运算符比较字符串:

@defining("something") {whatToTest =>
    @if(whatToTest == "something"){ There is something } else { There is.... nothing }
}
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下(当然要确保在customreCare......等条件下没有拼写错误):

@if(sp.pageType == "customreCare") {
     //render this specific div 
} else {
     //render this other div
}
Run Code Online (Sandbox Code Playgroud)

  • 有点烦人我有@if(sp.page ....)而不是@if(...(if和the之间的空格(显然很重要!) (4认同)