我试图为登录的用户类型显示不同的消息,但出现解析异常
<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/