如何将参数传递给渲染的属性? [英] How to pass a parameter into rendered attribute?
问题描述
我尝试像这样将参数传递给admin方法:
I try to pass a parameter into method admin like this:
<p:toolbarGroup align="right" rendered="#{loginBean.admin('dataread'}">
<h:form>
<p:commandButton value="manage users" ajax="false"
icon="ui-icon-document" action="/admin/manageUsers.xhtml?faces-redirect=true"/>
</h:form>
</p:toolbarGroup>
我的托管Bean中的代码就是这样
the code in my managed Bean is like that
public boolean isAdmin(String role){
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
return request.isUserInRole("admin");
}
推荐答案
rendered="#{loginBean.admin('dataread'}"
您在那里缺少)
一个EL语法错误,该错误导致该值未被识别为EL表达式,因此被视为纯香草字符串,该字符串将在rendered
属性中使用,因此默认为boolean true
.另外,当指定动作表达式(例如#{bean.method()}
)而不是值表达式(例如#{bean.property}
)时,您应该指定完整的方法名称,因此要指定isAdmin()
而不是admin()
.
You've there with the missing )
an EL syntax error which causes the value not being recognized as an EL expression and is thus treated as a plain vanilla string which would in the rendered
attribute thus default to boolean true
. Also, when specifying action expressions like #{bean.method()}
instead of value expressions like #{bean.property}
, you should specify the full method name, thus isAdmin()
instead of admin()
.
所有,这应该做到:
rendered="#{loginBean.isAdmin('dataread')}"
与具体问题无关,#{request}
已经在EL作用域中提供了HttpServletRequest
,因此也不需要支持bean样板:
Unrelated to the concrete problem, the HttpServletRequest
is already available in the EL scope by #{request}
, so this should also do without the need for backing bean boilerplate:
rendered="#{request.isUserInRole('admin')}"
这篇关于如何将参数传递给渲染的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!