I have a jsf page called tours, and a tourController, in tours I use a primefaces datagrid to show my tour items, and I use a commandLink to add a view link to each item. The problem is when I click the link, instead of going to viewTour page it shows the same page of tours.xhtml. And after a while it says view expired, so it seems that it goes somewhere. Here is my code:
TourController.java viewTour method:
public String viewTour(){
current = (Tour) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem()+getItems().getRowIndex();
return "ViewTour";
}
tours.xhtml datagrid:
<h:form id="list">
<p:dataGrid var="item" value="#{tourController.items}" columns="4" rows="20" paginator="true">
<p:panel>
<p:panelGrid style="width: 180px; height: 200px">
<f:facet name="header">
<p:row><p:column colspan="2"><h:outputText value="#{item.destination}"/></p:column></p:row>
</f:facet>
<p:row>
<p:column colspan="2" style="text-align: center">
<h:commandLink id="viewTour" action="#{tourController.viewTour()}" style="color: darkred; font-weight: bold">
<h:outputText value="#{item.description}"/>
</h:commandLink>
</p:column>
</p:row>
<p:row>
<p:column rowspan="2" style="vertical-align: middle">
<h:outputText value="#{item.company}"/><br/>
<h:outputText value="#{item.phone}"/><br/>
<a href="mailto:#{item.email}"><h:outputText value="#{item.email}"/></a><br/>
<a href="#{item.websiteurl}"><h:outputText value="#{item.websiteurl}"/></a>
</p:column>
</p:row>
<p:row>
<p:column><p:graphicImage value="/resources/images/tours/#{item.imgurl}.jpg"/></p:column>
</p:row>
</p:panelGrid>
</p:panel>
</p:dataGrid>
</h:form>
ViewTour.xhtml:
<h:form>
<h:panelGrid columns="2">
<h:outputText value="#{bundle.ViewTourLabel_imgurl}"/>
<h:outputText value="#{tourController.selected.imgurl}"/>
<h:outputText value="#{bundle.ViewTourLabel_source}"/>
<h:outputText value="#{tourController.selected.source}" title="Source"/>
<h:outputText value="#{bundle.ViewTourLabel_destination}"/>
<h:outputText value="#{tourController.selected.destination}" title="Destination"/>
<h:outputText value="#{bundle.ViewTourLabel_daysno}"/>
<h:outputText value="#{tourController.selected.daysno}" title="Duration"/>
<h:outputText value="#{bundle.ViewTourLabel_price}"/>
<h:outputText value="#{tourController.selected.price}" title="Price"/>
<h:outputText value="#{bundle.ViewTourLabel_description}"/>
<h:outputText value="#{tourController.selected.description}" title="Description"/>
<h:outputText value="#{bundle.ViewTourLabel_date}"/>
<h:outputText value="#{tourController.selected.date}" title="Date of Travel:">
<f:convertDateTime pattern="MM/dd/yyyy" />
</h:outputText>
<h:outputText value="To book this trip contact:"/>
<h:outputText value="#{bundle.ViewTourLabel_phone}"/>
<h:outputText value="#{tourController.selected.phone}" title="Phone:"/>
<h:outputText value="#{bundle.ViewTourLabel_email}"/>
<h:outputText value="#{tourController.selected.email}" title="Email:"/>
<h:outputText value="#{bundle.ViewTourLabel_purpose}"/>
<h:outputText value="#{tourController.selected.purpose}" title="Special Purpose Tour:"/>
<h:outputText value="#{bundle.ViewTourLabel_company}"/>
<h:outputText value="#{tourController.selected.company}" title="Arranged by:"/>
<h:outputText value="#{bundle.ViewTourLabel_websiteurl}"/>
<h:outputText value="#{tourController.selected.websiteurl}" title="Website:"/>
<h:outputText value="#{bundle.ViewTourLabel_rating}"/>
<h:outputText value="#{tourController.selected.rating}"/>
</h:panelGrid>
</h:form>
Update:
OP solved it by himself :
This post helpedPrimefaces DataGrid – CommandLink is not working.<p:panel> tags should be inside <p:column> when all are inside a <p:datagrid> tag.So the things I would check if I were you :
I suppose
viewTour()get correctly called and executed.<servlet-mapping>There can be many other problems but these are IMO the most frequent..
Also NOTE:
If you need to update the URL adress in the browser then you need to use redirect.
To redirect to another page you have to use
?faces-redirect=true. Append it to your return string like thisreturn "ViewTour?faces-redirect=true";. Without this you are using only forward mechanism which is happening completely on the server side so your URL won’t get updated in the browser.Edit
The reason why it is not navigating in your case may be the bean scope of your
tourControllerbean. Since controller beans are in the most cases@RequestScope-d (I assume it is your case as well) the problem is that your<h:commandLink>is placed inside iterating component –<p:dataGrid>. JSF will try to find clickedcommandLinkand submited values during the apply request values phase – it will reiterate over the component.Beacause of that you need to ensure that exactly the same value of the component has been given during this phase (as when it was clicked).
Solution: Put your bean to
@ViewScopeor load data in@PostConstruct.Also to check for the other relevant issues see this post by JSF guru BalusC.