JSF 2.0 AJAX:jsf.ajax.request调用方法不仅重新渲染页面区域

前端之家收集整理的这篇文章主要介绍了JSF 2.0 AJAX:jsf.ajax.request调用方法不仅重新渲染页面区域前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找以下问题的灵魂:
_这是一个包含不同类型汽车的链接列表.
_用户可以点击列表中的每辆车,并发送ajax请求.
_ ajax请求的响应应取决于(每辆车的)id并显示在panelGroup中.

所以我需要的是在backing-bean上调用方法的可能性.此外,应使用car id作为参数调用方法.

我的代码到目前为止看起来像:

...
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>
...

并且backing bean中的方法应该如下所示:

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

我不知道如何使用新的JSF 2.0 AJAX功能调用方法.请帮帮我…

使用f:setPropertyActionListener将对象从JSF页面传递到后端.当您使用可重复的组件(如datatable)时,此标记特别有用

无需使用原始JavaScript,您可以使用< f:ajax />.另外,不要担心车载ID和所有,只需将它完全发送到支持bean.

这是一个示例示例:

汽车类:

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;
    }
}

CarTree类:

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"));
    }
}

JSF页面

<?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">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <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>

希望这可以帮助.

原文链接:https://www.f2er.com/ajax/160037.html

猜你在找的Ajax相关文章