java - 如何在 EL 表达式的文本中使用不同的支持 bean 属性?

标签 java jsf-2 el

我试图为登录的用户类型显示不同的消息,但出现解析异常

<ui:define name="header">
        <ui:param name="userParam" value="#{bean.user.firstName} - #{bean.user.lastName}" />    
        <p:panel id="headerPanel"
            header="#{bean.adminUser ? 'Welcome Power User' userParam : bean.normalUser ? 'Welcome User' userParam :'Welcome to sytem'}"/>
    </ui:define>

这是异常(exception)

Caused by: org.apache.el.parser.ParseException: Encountered "?" at line 1, column 21.
Was expecting one of:
    "}" ...
    "." ...
    "[" ...
    ">" ...
    "gt" ...
    "<" ...
    "lt" ...
    ">=" ...
    "ge" ...
    "<=" ...
    "le" ...
    "==" ...
    "eq" ...
    "!=" ...
    "ne" ...
    "&&" ...
    "and" ...
    "||" ...
    "or" ...
    "*" ...
    "+" ...
    "-" ...
    "/" ...
    "div" ...
    "%" ...
    "mod" ...

最佳答案

从您的代码中:

#{bean.adminUser ? 'Welcome Power User' userParam : bean.normalUser ? 'Welcome User' userParam :'Welcome to sytem'}

您不能以这种方式连接 EL 中的字符串。

如果您使用 EL 2.2,则可以使用字符串的 concat()方法。

#{bean.adminUser ? 'Welcome Power User '.concat(userParam) : bean.normalUser ? 'Welcome User '.concat(userParam) : 'Welcome to sytem'}

或者,如果您仍在使用 EL 2.1,则应该由另一个 <ui:param> 预先准备完整的字符串。 (或<c:set>):

<ui:param name="welcomePowerUser" value="Welcome Power User #{userParam}" />
<ui:param name="welcomeUser" value="Welcome User #{userParam}" />
...
#{bean.adminUser ? welcomePowerUser : bean.normalUser ? welcomeUser : 'Welcome to sytem'}

关于java - 如何在 EL 表达式的文本中使用不同的支持 bean 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13280870/

相关文章:

java - JSF 2.0 和参数化 EL 表达式的问题

java - 为什么我无法通过表达式语言找到静态方法

java - 在 Fragments Android 中使用 ConstraintLayout 时的空 UI

java - Google Domain API 不断返回 401/403 响应。我究竟做错了什么?

java - JSF2 自定义 UIComponent 属性数据类型

jsf-2 - Weblogic 10.3.5 : <prefer-web-inf-classes> in weblogic. xml 不工作 VS。服务器类路径 JAR 文件

jsf - 如何为 h :dataTable/ui:repeat? 的每一行/项目设置转换器属性

java - 在当前应用程序上使用hadoop

java - 找到网格中 2 个节点之间路径的最有效方法

java - JSF 将 EL 表达式显示为纯文本而不是计算值