Javascript with JSF

Javascript with JSF

本文关键字:JSF with Javascript      更新时间:2023-09-26

我正在使用Google Maps API在客户端获取用户位置。。。然后,我想将其发送到服务器端进行一些处理,并填充一个选择框(使用本地地址)。

做这件事最好的方法是什么?我更愿意按下一个按钮并使用ajax填充框。

谢谢!

下面是一个如何使用Ajax将Javascript值(用户位置)传递给bean的示例。

该位置将存储在<h:inputHidden>字段中。<f:ajax>的execute属性指定将发送到服务器进行处理的值。

<h:form id="formname">
    <h:inputHidden value="#{indexBean.value}" id="hvalue" />
    <h:commandButton value="action" onclick="getLocation();" action="#{indexBean.doSome}">
        <f:ajax execute="hvalue" render="output" />
    </h:commandButton>
    <h:outputText value="#{indexBean.output}" id="output" />
</h:form>

在发送Ajax请求之前,<h:commandButton>的onclick属性指定要执行的Javascript函数。Javascript函数应该在隐藏字段中设置值。

function getLocation() {        
    document.getElementById("formname:hvalue").value = "TEST";                       
}

<f:ajax>的呈现属性指定要在客户端更新的内容。在这种情况下是<h:outputText>,在您的情况下,它将是您的<h:selectOneMenu>