tags:

views:

1572

answers:

2

Hi, I'm looking for a soultion of the following problem: _The is a list of links with different types of cars. _The user can click on each car in the list and a ajax request should be sent. _The response of the ajax request should be dependent on the id (of each car) and displayed in an panelGroup.

So what I need is a possibility to call a method on the backing-bean. Additionally, this method should be called with a car id as its parameter.

My code so far looks like:

...
function showDetails(detailsFor){
  jsf.ajax.request(this, event, {render: 'form1:carDetails'});
}
...
<ul>
    <ui:repeat value="#{carTree.getCars)}" var="car">
            <h:outputScript name="jsf.js" library="javax.faces" target="head" />
            <li onclick="showDetails(#{car.id});">#{car.name}</li>
    </ui:repeat>
</ul>
...
<h:panelGroup id="carDetails" layout="block" style="float:left;">
    // need the details of each 'selected /clicked' car here
</h:panelGroup>
...

And the method in the backing bean should look like:

public class CarTree {
    ...
    public String getCarDetails(int carid){
        return "The car details for the car id "+carid+" are......";
    }
    ...
}

I've no idea how to call a method by using the new JSF 2.0 AJAX functionality. Please, help me...

A: 

Hi, use f:setPropertyActionListener to pass a object from JSF page to your backend. This tag is especially useful when you are using repeatable components like datatable

No need to use raw javascript, you can use . Plus instead of worrying about Car id and all, just send it completely to backing bean.

here is a sample example:

The Car class:

public class Car {

    int id;
    String brand;
    String color;

    public Car(int id, String brand, String color) {
        this.id = id;
        this.brand = brand;
        this.color = color;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }
}

The CarTree class:

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "CarTree")
@RequestScoped
public class CarTree {

    List<Car> carList;
    Car selectedCar;

    public Car getSelectedCar() {
        return selectedCar;
    }

    public void setSelectedCar(Car selectedCar) {
        this.selectedCar = selectedCar;
    }

    public List<Car> getCars() {
        return carList;
    }

    public void setCars(List<Car> carList) {
        this.carList = carList;
    }

    public CarTree() {

        carList = new ArrayList<Car>();
        carList.add(new Car(1, "jaguar", "grey"));
        carList.add(new Car(2, "ferari", "red"));
        carList.add(new Car(3, "camri", "steel"));
    }
}

The JSF page:

<?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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"&gt;
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body id="mainBody">

        <h:form id="carForm">
            <h:dataTable value="#{CarTree.cars}" var="car">
                <h:column>
                    <h:outputText value="#{car.id}"/>
                </h:column>

                <h:column>
                    <h:outputText value="#{car.brand}"/>
                </h:column>

                <h:column>
                    <h:outputText value="#{car.color}"/>
                </h:column>

                <h:column>
                    <h:commandButton value="Show Car Detail" >
                        <f:setPropertyActionListener target="#{CarTree.selectedCar}" value="#{car}"/>
                        <f:ajax render=":carForm:carDetails" />
                    </h:commandButton>
                </h:column>

            </h:dataTable>
            <h:panelGroup id="carDetails" layout="block" style="float:left;">
                <h:outputText value="#{CarTree.selectedCar.id}" /> 
                <h:outputText value="#{CarTree.selectedCar.brand}" />
                <h:outputText value="#{CarTree.selectedCar.color}" />

            </h:panelGroup>
        </h:form>
    </h:body>
</html>

Hope this helps.

nash
A: 

I haven't tested myself, but I'd suggest you try something like this (assuming your class CarTree is @Named and, therefore, can be referred to inside the JSF page using the name carTree):

<ul>
    <ui:repeat value="#{carTree.getCars)}" var="car">
            <li><h:commandLink action="#{carTree.getCarDetails(car.id)}" value="#{car.name}">
                <f:ajax render="carDetails" />
            </h:commandLink></li>
    </ui:repeat>
</ul>
...
<h:panelGroup id="carDetails" layout="block" style="float:left;">
    // need the details of each 'selected /clicked' car here
</h:panelGroup>

I think the contents of the action property in the <h:commandLink /> can also be coded as the listener property in the <f:ajax /> tag. Don't know if there's any difference...

If you don't want to use <h:commandLink /> you could replace it with <h:outputText /> and add the property event="click" to the <f:ajax /> tag. I think that would work as well. In this case, the method call would have to be in the listener property of the <f:ajax /> tag.

Vítor Souza