0

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>

1 Answers1

1

Update: OP solved it by himself :

This post helped Primefaces 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.

  • Check if ViewTour.xhtml is in the same folder as tours.xhtml. If not you have to specify the correct path.
  • Check if your servlet mapping is correctly configured for the .jsf files. Open your web.xml located in configuration files and check if servlet mapping looks like this:

<servlet-mapping>

    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
 </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 this return "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 tourController bean. 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 clicked commandLink and 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 @ViewScope or load data in @PostConstruct.

Also to check for the other relevant issues see this post by JSF guru BalusC.

Community
  • 1
  • 1
Fallup
  • 2,417
  • 19
  • 30
  • Thanks for replying Fallup. ViewTour.xhtml is in the same folder as tours.xhtml and i used ?faces-redirect=true today, and my servlet mapping is like, Faces Servletfaces/* What do you think the problem is? It still acts as it did. –  Jan 13 '13 at 10:13
  • @user993072 Don't use `faces/*` in URL mapping it is the old obsolete way from the JSF 1.x era. Use it like I did in the post above. Does some other navigation work for you ? I mean do you have any other .xhtml navigating to another which works ? Try to remove all of the content from your tours.xhtml - leave just form with `commandlink` and hardcoded `action="ViewTour?faces-redirect=true"` and see if it works. – Fallup Jan 13 '13 at 10:27
  • Yes I have other navigation that works with /faces/* however I changed my servlet mapping and when removing everything from tours.xhtml it worked. What can be the problem? –  Jan 13 '13 at 12:08
  • @user993072 See my updated answer for possible problem. Also check the suggested link. Your problem is mostly the incorrect bean scope. – Fallup Jan 13 '13 at 12:20
  • I had used session scope, I changed it to view scope but still have the same problem. I looked at the post you had provided. It suggested the same solution. –  Jan 13 '13 at 13:10
  • @user993072 Have you tried everything suggested in the post I've linked ? If the scope is not the problem then try to add `` to your page to check for errors. Also don't you have nested forms ? – Fallup Jan 13 '13 at 13:15
  • thanks very much for taking your precious time to answer my questions, I have tried all the solutions provided and does not show any messages because it does not give any errors. I am sorry. –  Jan 13 '13 at 13:47
  • @user993072 You are welcome, I'm sorry I couldn't help. Maybe someone other will know. If you will find the solution let us know. – Fallup Jan 13 '13 at 14:11
  • 1
    Thanks @Fallup again. This post helped: [http://stackoverflow.com/questions/11985305/primefaces-datagrid-commandlink-is-not-working]. tags should be inside when all are inside a tag. –  Jan 13 '13 at 14:26
  • @user993072 Beat me to it ;) – Fallup Jan 13 '13 at 14:33