Khaled ElAbed
Khaled ElAbed

Reputation: 21

How to pass a parameter into rendered attribute?

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");                
          }

Upvotes: 2

Views: 7807

Answers (2)

d1e
d1e

Reputation: 6442

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:

Upvotes: 1

BalusC
BalusC

Reputation: 1108632

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')}"

Upvotes: 5

Related Questions