为什么这个Ajax调用不起作用?

为什么这个Ajax调用不起作用?,ajax,jsf,Ajax,Jsf,尝试使用ajax请求呈现标签而不刷新整个页面。到目前为止,每次我点击命令按钮,整个页面仍会刷新,我似乎无法找出我做错了什么 按标题搜索 <h:commandButton class="addButton" id="checkStatusButton" value ="Check Status" action = "#{itemWeb.findItem}"> <f

尝试使用ajax请求呈现标签而不刷新整个页面。到目前为止,每次我点击命令按钮,整个页面仍会刷新,我似乎无法找出我做错了什么

按标题搜索

<h:commandButton class="addButton" 
                 id="checkStatusButton" 
                 value ="Check Status" 
                 action = "#{itemWeb.findItem}">
    <f:ajax execute="checkStatusForm" 
            event="click" 
            render="statusLabel"/>
</h:commandButton>

<h:outputText id="statusLabel" 
              value="#{itemWeb.foundItem.status}">
</h:outputText>


执行
属性中,尝试仅处理所需字段而不是整个表单:

<h:commandButton class="addButton" id="checkStatusButton" value ="Check Status" action = "#{itemWeb.findItem}">
    <f:ajax execute="searchCheckInput" event="click" render="statusLabel"/>
</h:commandButton>

您需要使用
event=“action”
而不是
event=“click”
event=“action”
即嵌套在
h:commandButton
组件中时侦听的默认事件

下面是一个例子

xhtml

领域

package com.wittakarn.model;

import java.io.Serializable;

/**
 *
 * @author Wittakarn
 */
public class Item implements Serializable{
    private String status;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

页面会刷新,因为您没有使用JSF标准标记(h:head,h:body)。 因此,您应该将index.xhtml更改为下面的示例

<!-- 
Index.html setup
Holds the form for input data
-->
<!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"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <link href='http://fonts.googleapis.com/css?family=Roboto' 
              rel='stylesheet' type='text/css'></link>
        <link href='resources/css/main.css' 
              rel='stylesheet' type='text/css'></link>
        <title>
            <ui:insert name="title"
                       Library Management System
        </ui:insert>
    </title>
    <!-- 
   Quick style setup..
    -->
</h:head>

<h:body>
    <h:form id="checkStatusForm">
        <div class="group">
            <h:inputText id="searchCheckInput" 
                         value="#{itemWeb.searchString}">
            </h:inputText>
            <label>Search By Title..</label>
            <span class="highlight"></span>
            <span class="bar"></span>
        </div>
        <h:commandButton class="addButton" 
                         id="checkStatusButton" 
                         value ="Check Status" 
                         action ="#{itemWeb.findItem}">
            <f:ajax execute="checkStatusForm" 
                    event="action" 
                    render="statusLabel"/>
        </h:commandButton>
        <h:outputText id="statusLabel" 
                      value ="#{itemWeb.foundItem.status}">
        </h:outputText>
    </h:form>
</h:body>

</html>


嗯,谢谢你的帮助。尝试了一下,但不幸的是似乎仍然刷新了整个页面。将“render”属性
@all
。啊,仍然没有运气。不知道我哪里出错了。我认为这只是一个语法问题。为什么不删除
event=“click”
部分,让jsf使用默认事件
h:commandButton
?显示更多代码,您可能有嵌套表单或其他问题,还可以尝试将
execute=“checkStatusForm”
替换为
execute=“@form”
@Daniel sorry没有意识到公开回购需要它,但这一定是因为它是大学的github。找到了解决方案,谢谢您的帮助。@Daniel解决方案就是我接受的。“页面会刷新,因为您没有使用JSF标准标记(h:head,h:body)。因此,您应该将index.xhtml更改为下面的示例。”谢谢您的帮助,但仍然不适用于我。尝试使用操作(来源:),但整个页面仍会刷新。想知道它是否与其他层有关?该链接不是public repo@Timeflies,它需要登录。@Timeflies您的链接已断开。啊,由于某种原因从未上载过。这个能行!非常感谢你的帮助。我将在今天晚些时候进行这些更改并尝试!
package com.wittakarn.model;

import java.io.Serializable;

/**
 *
 * @author Wittakarn
 */
public class Item implements Serializable{
    private String status;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
<!-- 
Index.html setup
Holds the form for input data
-->
<!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"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <link href='http://fonts.googleapis.com/css?family=Roboto' 
              rel='stylesheet' type='text/css'></link>
        <link href='resources/css/main.css' 
              rel='stylesheet' type='text/css'></link>
        <title>
            <ui:insert name="title"
                       Library Management System
        </ui:insert>
    </title>
    <!-- 
   Quick style setup..
    -->
</h:head>

<h:body>
    <h:form id="checkStatusForm">
        <div class="group">
            <h:inputText id="searchCheckInput" 
                         value="#{itemWeb.searchString}">
            </h:inputText>
            <label>Search By Title..</label>
            <span class="highlight"></span>
            <span class="bar"></span>
        </div>
        <h:commandButton class="addButton" 
                         id="checkStatusButton" 
                         value ="Check Status" 
                         action ="#{itemWeb.findItem}">
            <f:ajax execute="checkStatusForm" 
                    event="action" 
                    render="statusLabel"/>
        </h:commandButton>
        <h:outputText id="statusLabel" 
                      value ="#{itemWeb.foundItem.status}">
        </h:outputText>
    </h:form>
</h:body>

</html>