0

On the datable I click on commandLink and that link navigates to destination page. I need to get the parameter ('id' in this case) on that destination page.

It works fine with link but not with commandLink. I get the parameter as null. And I must solve this with commandLink.

source page

<p:dataTable value="#{orderBean.orders}" var="order" id="orderDTable" rowKey="#{order.id}">

    <p:column id="editButtonsId" >
        <p:commandLink value="Edit" action="orderInsert" >
            <f:param name="id" value="#{order.id}"/>
        </p:commandLink>

        <!-- works fine with this link -->
        <p:link value="Edit" outcome="orderInsert" >
            <f:param name="id" value="#{order.id}" />
        </p:link>
    </p:column> 

</p:dataTable>

destination page

<ui:composition template="templates/layout.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:components="http://java.sun.com/jsf/composite/components"
                xmlns:fn="http://java.sun.com/jsp/jstl/functions"
                xmlns:p="http://primefaces.org/ui">
<f:metadata>
        <f:viewParam name="id" value="#{orderInsertBean.id}" />
        <f:event listener="#{orderInsertBean.init}" type="preRenderView" />
</f:metadata>

</ui:composition>

@Component("orderInsertBean")
@ManagedBean
@RequestScoped
public class OrderInsertBean implements Serializable {

    private String id;

    public void init() {
        // the parameter id is null here when the page is navigated by the commandLink
        FacesMessage msg = new FacesMessage(id, null);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
}
Korki Korkig
  • 2,736
  • 9
  • 34
  • 51

1 Answers1

-1

See these posts first

With commandLink you could either annotate the id with @ManagedPropertyas shown here or use the request parameters map:

public void init() {
    FacesContext context = FacesContext.getCurrentInstance();  
    Map requestParams = context.getExternalContext().getRequestParameterMap();  
    id = (String) requestParams.get("id");
    // the parameter id is null here when the page is navigated by the commandLink
    FacesMessage msg = new FacesMessage(id, null);
    FacesContext.getCurrentInstance().addMessage(null, msg);
}
Community
  • 1
  • 1
Seitaridis
  • 4,459
  • 9
  • 53
  • 85
  • sorry it's still null. – Korki Korkig Sep 09 '14 at 10:06
  • What `@Component` annotation do you use? Maybe there is another thing that doesn't work in you code. Try the code without the annotation. I have tested my answer in a small project only with JSF/PrimeFaces and the value is not null. My advice is to remove all the code unrelated to your problem. If the code works than you can move on to figure out what else goes wrong. – Seitaridis Sep 11 '14 at 16:47