代码之家  ›  专栏  ›  技术社区  ›  TrackerSB

在不更改页面的情况下调用托管bean函数[重复]

  •  -2
  • TrackerSB  · 技术社区  · 9 年前

    <h:commandButton value="Click Me!" action="whatever" /> 。单击此按钮会使浏览器“向前移动”,以便我可以单击后退按钮并停留在xhtml页面上。这意味着当我点击commandButton十次时,我可以通过按下浏览器的后退按钮返回十次。 我该怎么做才能使按钮不改变页面?我希望能够点击这样一个按钮,在某些情况下更改页面,在一些情况下停留在当前页面,而不必在浏览器的历史记录中进行任何操作。

    1 回复  |  直到 9 年前
        1
  •  2
  •   Patrick    9 年前

    使用AJAX可能是解决问题的一种方法。

    如果在表单元素中使用命令按钮,单击会将表单数据连同post请求发送到服务器。

    <h:form>
        <h:inputText id="inputId" value ="#{testController.message}"></h:inputText>
        <h:commandButton value="submit" action="#{testController.reverse}" />
    </h:form>
    

    如果在命令按钮中使用AJAX元素,则只会将execute属性指定的数据发送到服务器。响应仅包含render属性指定的html文件的一部分。此部分将插入现有的html文件中。使用AJAX不会刷新整个页面,因此不会影响浏览器历史记录。

    <h:form>
        <h:inputText id="inputId" value ="#{testController.message}"></h:inputText>
        <h:commandButton value="inputId" action="#{testController.reverse}" >
            <f:ajax execute="inputId" render="outputId" />
        </h:commandButton>
    </h:form>
    
    <h:outputText id="outputId" value="#{testController.output}"/>
    
    推荐文章