简体   繁体   English

使用JSF 2.0 Ajax响应从bean函数访问返回值?

[英]Accessing return value from bean function using JSF 2.0 Ajax response?

I'm builiding a scheduling application using JSF 2.0 where users can add items to a calendar then edit those objects. 我正在构建一个使用JSF 2.0的调度应用程序,用户可以在其中向日历添加项目,然后编辑这些对象。

I'm using AJAX quite extensively to keep the page from refreshing. 我正在广泛使用AJAX,以防止刷新页面。

The problem I am having is to get a return value from the function that is called using AJAX. 我遇到的问题是从使用AJAX调用的函数获取返回值。

<!-- ... form to fill -->
<h:commandButton value="Insert">
   <f:ajax execute="formField1 formField2..." 
    listener="#{myBean.insert()}" onevent="processSave"/>
</h:commandButton>

This successfully calls my JavaScript function processSave() . 这成功调用了我的JavaScript函数processSave()

myBean.insert() returns the id of the newly inserted row in the database myBean.insert()返回数据库中新插入的行的id

public String insert() {
    //... inserting data into database
    id = {id from database is obtained here}
    System.out.println("I have the ID here : " id);  //this works
    return id;
}

I've been trying to get this from the response object within JavaScript in the processSave() function. 我一直在尝试从processSave()函数中的JavaScript中的响应对象获取此信息。

processSave(data) {
    if(data.status == "begin") {
        // ation done here
    } else if(data.status == "complete") {
        // more actions done here
    } else if(data.status == "success") {
        //I am trying to get the ID here
        //I've tried looking into data.responseXML, but to no avail.
    }
}

Is what I am trying possible using the current technologies? 使用当前技术,我正在尝试的可能吗?

I think it would be possible to have a field in the page updated using the render component of the AJAX call, then use Javascript to go get the value. 我认为可以使用AJAX调用的render组件在页面中更新字段,然后使用Javascript获取值。 But I'm thinking this wouldn't be as clean? 但是我想这样还不干净吗?

Thank you! 谢谢!

It is possible. 有可能的。 And more easily done with primefaces . 使用primefaces更容易完成。

Follow this example, you may find something useful there. 遵循示例,您可能会在这里找到有用的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM