How to implement the image redirect with parameters? - jsf

This is the way how a commandButton realizes the redirect with parameters.
<h:commandButton value="Check" action="#{GetInfoBean.getInfo}">
<f:param name="ID" value="4"></f:param>
</h:commandButton>
When this commandButton get clicked, it will pass the parameter named ID with value 4. The managed bean GetInfoBean's method getInfo() will return the new url.
But now, my question is how can I implement this redirect process using image or graphicImage since it doesn't have the action attribute?

Wrap the image using a <h:commandLink>:
<h:commandLink value="Check" action="#{GetInfoBean.getInfo}">
<f:param name="ID" value="4"></f:param>
<h:graphicImage value="/your/image.png" />
</h:commandLink>

Related

Implementing a return navigation in JSF

I am looking for a JSF coding practice that will allow one page to link to another that, when it completes, will return to the original page. So I have a page viewDoc.xhtml that has this:
<f:metadata>
<f:viewParam name="id" value="#{viewDoc.id}" />
</f:metadata>
( bunch of stuff to show the document indicated by "id" )
<p:button value="Edit Name/Title"
outcome="editnametitle">
<f:param name="id" value="#{param.id}" />
</p:button>
Then I have a page editnametitle.xhtml that has this:
<h:form>
(input fields and stuff)
<p:commandButton value="Save Changes"
action="#{editNameTitle.doSave()}"
/>
<p:commandButton value="Cancel"
action="#{editNameTitle.doCancel()}"
immediate="true"
/>
</h:form>
So how do I implement the backing bean methods doSave() and doCancel() such that when they finish they navigate back to viewDoc.xhtml with the id parameter of the document included?
I haven't found any guidance for a solution and I haven't been happy with any approach I have thought of so far. I have thought of something like adding to the p:button the return path like:
<p:button value="Edit Name/Title"
outcome="editnametitle">
<f:param name="id" value="#{param.id}" />
<f:param name="returnoutcome" value="viewDoc" />
</p:button>
Is this the right approach? Or is there some JSF facility that does this that I missed?
Yes, that's basically it if those requests are idempotent.
One possible improvement is that you can dynamically obtain the current view ID as below:
<f:param name="returnoutcome" value="#{view.viewId}" />
This allows abstracting away it in a reusable custom tag.
Personally I'd use parameter name "from" too as that's more short.

How to call Java method from h:outputlink and pass value

I have this output `h:outputLink' to open a new page and pass a value:
<h:outputLink id="link" value="newpage.jsf" style="text-decoration:none; color:white;">
<f:param name="id" value="#{item.value}" />
</h:outputLink>
I need to add Java method which is called after I click the link. I saw that h:outputLink don't have action listener which I can use for calling Java method. I tested to use `h:commandButton' but the table logic is changed and it's not working properly. How I can solve this problem?
Try using 'h:commandLink' instead of `h:outputLink':
<h:commandLink id="lnkHidden" style="text-decoration:none; color:white;" actionListener="#{bean.pageRedirect}">
<f:setPropertyActionListener target="#{bean.sessionValue}" value="#{item.value}" />
</h:commandLink>

Pass parameter to f:ajax inside c:foreach loop

I am trying to pass a parameter to an ajax event, this is an example of what I mean:
<h:panelGrid id="pictures" columns="3">
<c:forEach items="#{createProduct.pictures}" var="picture">
<h:graphicImage value="#{picture.source}" />
<h:inputText value="#{picture.alt}" />
<p:commandButton value="Remove">
<f:ajax execute="#form"
listener="#{createProduct.removePicture(picture)}"
render="#form" />
</p:commandButton>
</c:forEach>
</h:panelGrid>
I know this code won't work because you cannot pass a parameter through the ajax listener. Though this is merely an example of what I am trying to accomplish. I have tried using <f:param id="picture" value="#{picture}" /> allong with the ajax tag, though this results in a duplicate component ID form:picture since it is in a loop.
I need this parameter inside the method in order to determine which picture to remove.
How can I solve this with ajax?
I am using a #ViewScoped CDI-bean.
First, I suggest replacing c:forEach with ui:repeat tag. Second, as you are using Primefaces, you should use all its strength. So, commandButton can be more easily written:
<p:commandButton value="Remove" process="#form" update="#form" action="#{createProduct.removePicture(picture)}"/>
and that's it. Don't complicate.
By the way, primefaces commandButton is sends AJAX request by default, you don't need (and you must not use) f:ajax tag.

How to parameterize an a4j:commandLink properly?

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&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&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?
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" />
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>

why composite compoment does not invoke in the <h:datatable/>

I have a compoment which has a method-signature attribute. It can be activated, but if I put it in a <h:datatable> <h:column/> and trigger this component, it does not work.
When I just refresh this page again or when I put it in another place it can invoked successfully. I would be grateful if somebody can tell me why!
this is my code
<h:column>
<f:facet name="header">op:</f:facet>
<h:commandLink value="alter" action="#{userSession.alterAction}"
rendered="#{userSession.user.power.powerID == 1}">
<f:param name="beanId" value="#{book.bookID}" />
<f:param name="class" value="#{BookBean}" />
</h:commandLink>
<h:commandLink action="#{userSession.detailAction}" value="detail"
rendered="#{userSession.user != null}">
<f:param name="beanId" value="#{book.bookID}" />
<f:param name="class" value="#{BookBean}" />
</h:commandLink>
<h:commandLink action="#{bookAction.bookDelAction}"
onclick="return confirm('are you sure?')" value="delete"
rendered="#{userSession.user.power.powerID == 1}">
<f:param name="beanId" value="#{book.bookID}" />
</h:commandLink>
</h:column>
this manageredBean #{bookAction} is requestScope when i click one of this operation,just like delete ,it doesnot work at all. but if i put the 'delete' commandlink out of <h:datatabel/> .it can invoke backing method successfully. it is so upset!
who can tell me whether <h:datatable/> can Shielding the .i found if i put these code in a <h:form/> .it can invoke too! cound you can tell me the reason!
You need to preserve exactly the same data model (i.e. the one which you have referenced by the value attribute of the <h:dataTable>) in during the request of the form submit as it was during the request of displaying the initial form. The symptoms indicate that you're using a request scoped bean and that the loading of the data model is based on some request parameter which is missing during the form submit and/or the loading is not being done during bean's (post)construction.
Putting the bean in the view scope and/or rearranging the data loading logic should fix it.

Resources