在没有刷新页面

时间:2016-07-19 08:17:46

标签: jsf datatable

我在同一页面中输入了输入字段和数据表,这样当用户添加新数据时,应该将数据添加到数据表而不刷新页面。

为实现这一点,我尝试了以下代码

hospials.xhtml:

<h:form>
        <h:outputLabel>Name:</h:outputLabel>
        <h:inputText value="#{hospitalsBean.ename}"/>
        <h:outputLabel>Address:</h:outputLabel>
        <h:inputText value="#{hospitalsBean.hospital.address}"></h:inputText>
        <h:commandButton value="A">
            <f:ajax event="click" render="table" listener="#{hospitalsBean.command()}"></f:ajax>
        </h:commandButton>
    </h:form>
    <h:dataTable id="table" value="#{hospitalsBean.hospitals}" var="hospital">
        <h:column>
            <f:facet name="header">Name</f:facet>
            #{hospital.name.english}
        </h:column>

        <h:column>
            <f:facet name="header">Address</f:facet>
                #{hospital.address}
        </h:column>
    </h:dataTable>

HospitalsBean.java:

private Hospital hospital = new Hospital();
private ArrayList<Hospital> hospitals = new ArrayList<>();
private Part file; // +getter+setter

public ArrayList<Hospital> getHospitals() {
    return MongoCrud.getHospitals();
}

public void setHospitals(ArrayList<Hospital> hospitals) {
    this.hospitals = hospitals;
}

public Hospital getHospital() {
    return hospital;
}

public void setHospital(Hospital hospital) {
    this.hospital = hospital;
}

public void command() throws IOException {

    MongoService.insertData("Hospital", Finals.gson.toJson(hospital));
}

但是当我向mongodb插入数据时,医院变量没有填写姓名和地址

1 个答案:

答案 0 :(得分:1)

您缺少execute=""属性,以便在服务器端通过ajax 执行组件

该属性的默认值为 @this ,后者仅执行嵌入标记的组件。

所以而不是

<f:ajax event="click" 
        render="table" 
        listener="#{hospitalsBean.command()}" />

<f:ajax event="click" 
        render="table" 
        execute="@form"
        listener="#{hospitalsBean.command()}" />

或提供可修改的价值持有人 ID

    <h:inputText value="#{hospitalsBean.ename}"
                    id="ename"/>
    <h:inputText value="#{hospitalsBean.hospital.address}" 
                    id="address"/>
    <h:commandButton value="A">
            <f:ajax event="click" 
                    render="table" 
                    execute="address ename"
                    listener="#{hospitalsBean.command()}" />
    </h:commandButton>