问题
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>
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");
}
回答1:
rendered="#{loginBean.admin('dataread'}"
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()
.
All with all, this should do:
rendered="#{loginBean.isAdmin('dataread')}"
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')}"
回答2:
Correct syntax:
rendered="#{loginBean.isAdmin('dataread')}">
instead of
rendered="#{loginBean.admin('dataread'}"
Make sure your EL supports such feature:
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
</dependency>
Look for more information containing tutorials on passing parameters in JSF:
How to pass parameters in method expression – JSF 2.0
4 ways to pass parameter from JSF page to backing bean
来源:https://stackoverflow.com/questions/10726462/how-to-pass-a-parameter-into-rendered-attribute