Question

I am quite new in developing website with JSF and PrimeFaces, I spent a lots of hours to research about my problem before post here. Thanks to everyone who spent time to read my question.

Well, I'm using this in my menu template page:

<h:link value="Manage Examination" outcome="/backend/examination/index" />
...
<h:link  value="List Examinations..." outcome="/WEB-INF/include/backend/examination/List.xhtml" />
<h:link value="Add Examination..." outcome="/WEB-INF/include/backend/examination/Create.xhtml" />

My WEB-INF folder has this structure like below:

WEB-INF
\--include
\\-----backend
\\------'entity name'
\\\-------'create,read,update,delete.xhtml'

Outside of WEB-INF is my root web page folder, I have backend folder here, and its structure:

webpages
\--WEB-INF
\--backend
\\---'entity name'
\\\---index.xhtml

Inside each html I put these code:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                template="/WEB-INF/include/templates/backend.xhtml">

    <ui:define name="title">
        <h:outputText value="#{appBundle.ExaminationTitle}"/>
    </ui:define>

    <ui:define name="body">
        <ui:include src="/WEB-INF/include/backend/examination/List.xhtml"/>
    </ui:define>

</ui:composition>

And my question is:

  1. I didn't configure anything in faces-config.xml so could JSF know if I click to the link ?
  2. How could I keep the url in address bar always be /backend/examination/index even I click to the 'List Examination' or 'Add Examination' link with the outcome to /WEB-INF/include/backend/examination/* ?

NNToan

Was it helpful?

Solution

You are misusing <h:link>.

The value of the 'outcome' attribute should be bound to a path whose root is the main faces context path. The JSF framework will render your h:link with an <a/> tag, so keeping the same address is out of question.

<h:link value="Page 1" outcome="page1.xhtml" />

will be in fact translate into:

<a href="/faces/page1.xhtml">Page 1</a>

If you want to perform an action without user redirection you should use a commandLink or a commandButton, returning a String (also take care not to include the faces-redirect parameter in that string).

For instance:

<h:commandLink value="Click here" action="#{YourBean.myAction}"/>

In your backing bean:

public String myAction()
{
  // do your stuff here
  return ""; // in general return the view you want to be redirected on, "" means "here"
}

OTHER TIPS

1.You don't need to. h:link is rendered as an HTML element. So clicking the link issues simple HTTP GET request to the specified URL, no JSF postback, no action listener and no dynamic navigation, and so nothing to do with the faces-config.xml in this case.

2.You can't. Since this is an HTTP GET request, browser will no way display the target URL. To preserve the URL you have to make a JSF postback and make dynamic navigation with an action listener method that returns an outcome that is used by the navigation handler. JSF always makes a postback to the same page and so URL is preserved although a new view is rendered.

<h:form>

   <!--When you click the button you navigate to the display Examinations view -->
   <!--but URL in the browser address bar does not change--> 
   <h:commandButton value="Navigate" action="#{bean.displayExaminationsList()}"/>
</h:form>

@ManagedBean
public class Bean {

  public String displayExaminationsList() {

    //The view that maps to this viewID shall be defined in the faces-config.xml
    return "examinationsListViewID";
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top