0

I have the following problem: I need to redirect from a "list page" to a details page, but I need the id from the list. "record" in this example is the var attribute of a rich:dataTable. First of all I thought about this:

<a4j:commandLink id="detailsLink" value="show details" execute="@this" action="/customerDetails?faces-redirect=true&amp;cusid=#{record.id}" />

But this is invalid syntax, so I tried something like this:

<a4j:commandLink id="detailsLink" value="show details" execute="@this" action="/customerDetails?faces-redirect=true">
<f:attribute name="cusid" value="#{record.id}"/>
</a4j:commandLink>

(I even tried f:param) On the target page I tried to receive the value with...

<f:metadata>
<f:viewParam required="false" name="cusid" value="#{customerBean.editCustomer}"/>
</f:metadata>

Basically f:metadata works, because when I try it with the following hard coded parameter, I receive its value:

<a4j:commandLink id="detailsLink" value="show details" execute="@this" action="/customerDetails?faces-redirect=true&amp;cusid=120" />

I found a solution, but I'm not sure if this is the right way:

In customerBean I make the following:

public String editCustomer(long customerId)
{

    edit(customerId);
    return "/customerDetails?faces-redirect=true";
}

But I don't think that this is the usual way to send and receive parameters with Rich Faces. Is there maybe a better solution?

Bevor
  • 8,396
  • 15
  • 77
  • 141

2 Answers2

1

The <a4j:commandLink> sends an ajax POST request while you need a normal GET request. Use <h:link> instead.

<h:link value="show details" outcome="/customerDetails?cusid=120" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Basically this seems to work, even with an EL expression as parameter, but there is some weird behavior. On customersDetail I try to get the param with the f:metadata attribute as described above. When it redirects to the details page the first time while clicking on that link, the metadata property method will not be executed. When I switch back to the list page and try it again, it suddenly works. I don't know what's wrong with that. – Bevor Mar 06 '12 at 13:59
  • Sounds like a cache matter. Clean browser cache and restart server. – BalusC Mar 06 '12 at 14:05
  • I think this is related to another problem. But anyway, this is out of the scope of my question. – Bevor Mar 06 '12 at 14:31
  • To be sure, you have not added `faces-redirect=true`, right? This is unnecessary on plain vanilla GET requests. By the way, if the parameters are strings containing user-controlled data instead of integers, you should be using `` instead so that it will be XML-escaped (to prevent potential XSS attacks). See also http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ProcessingGETRequestParameters – BalusC Mar 06 '12 at 14:32
0

Here's the best way to do it. The example provided passes 2 parameters. You can have more than one. Just use the assignTo attribute and the value attribute. Hope this answers your question.

<a4j:commandLink action="#{myBackingBean.myAction}">
    <a4j:param name="jobIdParam" value="#{job.jobNumber}"
        assignTo="#{myBackingBean.jobId}" />
    <a4j:param name="isDisplayedParam" value="true"
        assignTo="#{myBackingBean.displayed}"/>
</a4j:commandLink>
gallea01
  • 82
  • 1
  • 8