Search code examples
jsfrichfacesmyfaces

open detail view with request parameter


I created a simple master/detail using myfaces and richfaces. By clicking a h:commandLink in the rich:dataTable the user is able to open the detail view and edit the entity.

Now I want to create a URL that allows the user to open the detail view directly. Normally I would to this by creating an URL like /detail.jsp?id=12 - how can I achieve that with JSF?


Solution

  • You can construct the URL using parameters on a link control:

        <h:outputLink value="reqscope.faces">
            <f:param name="id" value="#{row.id}" />
            <h:outputText value="link" />
        </h:outputLink>
    

    On the target page, you can read the id from the parameter map, either directly using the expression language or via a managed bean.

    View:

    <f:view>
        id= <h:outputText value="#{param['id']}" />
        <br />
        id= <h:outputText value="#{lookupBean.id}" />
    </f:view>
    

    Bean:

    public class LookupBean implements Serializable {
    
        public String getId() {
            FacesContext context = FacesContext.getCurrentInstance();
            ExternalContext extContext = context.getExternalContext();
            Map<String, String> params = extContext.getRequestParameterMap();
            return params.get("id");
        }
    
    }
    

    faces-config.xml declaration:

    <managed-bean>
        <managed-bean-name>lookupBean</managed-bean-name>
        <managed-bean-class>reqscope.LookupBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>