Search code examples
jsfhttp-redirectcommandlink

Redirect my page after h:commandLink action in JSF


I have two pages:

  • listMedicalJourneys.xhtml
  • listAffectedEmployees.xhtml

From the first page:

<h:form>
    <h:commandLink action="#{MedicalJourneyController.listAffectedEmployees()}" value="Manage">
        <f:setPropertyActionListener value="#{element.id}" target="#{MedicalJourneyController.medicalJourneyId}" />
    </h:commandLink>
</h:form>

The @RequestScoped bean #{MedicalJourneyController} has this method:

public String listAffectedEmployees() {
    MedicalJourney m = medicalJourneyBean.getMedicalJourneyById(medicalJourneyId);
    setMesList(new ArrayList<MedicalJourneyEmployeeService>(m.getMedicalJourneyEmployeeServices()));
    setSelectedMedicalJourney(m);
    return "listAffectedEmployees.faces?faces-redirect=true";
}

When I use the redirect to change URL, the next page doesn't show the selected value.


Solution

  • if i understand you right, you want to select an item inside one view, and display its SubItems in the next View. if this is the Case then, there are seviral ways to acheave this, here i will show you a way based on your Concept/Code above, so lets assume the following:

    1. listMedicalJourneys.xhtml the View where you select an Item
    2. listAffectedEmployees.xhtml the view Where you display SubItems of the preselected Item

    and i assume that you have for each View its own Controller/ManagedBean,

    so lets call the first one medicalJourneysManager and for the second view affectedEmployeesManager, both are requestScoped Beans

    in the medicalJourneysManager your "selection Methode" action event should only get the selected ItemId, and pass this selected id to the next Page. Next Page Controller should then load the List of SubItems. Because a requestScoped Bean is only available during this single request, and as soon as you navigate anywhere, your bean will be reinitialized, that means the loaded data is lost at this moment.

    so in your medicalJourneysManager define some ActionMethode like this:

        //JSF 2.+
        public String selectMedicalJourny(int medicalJourneyId) {
            // do what ever you want before redirect, i.e. any validations, ...etc if required
            return "listAffectedEmployees.faces?faces-redirect=true&medicalJourneyId="+medicalJourneyId;
        }
    

    this will redirect you to the next View where you display a list of AffectedEmployees

    this new View needs its Controller "affectedEmployeesManager" to load the list of affectedEmployees.

    so in the this managed Bean do something like this:

    @PostConstruct
    private void init(){
        try{
            String medicalJourneyId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("medicalJourneyId");
            // now do whatever you want with it, load its subNodes/affectedEmployeesList, ...etc
            MedicalJourney m =  medicalJourneyBean.getMedicalJourneyById(medicalJourneyId);
            setMesList(new ArrayList<MedicalJourneyEmployeeService>(m.getMedicalJourneyEmployeeServices()));
            setSelectedMedicalJourney(m);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    

    a simpler alternative to commandLink is using a direct link with that param without any action commands.

    here is a helpfull link.