[英]Handles data response from JSF Ajax in Javascript
I have the follow Managed Bean class: 我有以下Managed Bean类:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSayWelcome(){
//check if null?
if("".equals(name) || name ==null){
return "";
}else{
return "Ajax message : Welcome " + name;
}
}
}
And this the JSF file: 这是JSF文件:
<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h3>JSF 2.0 + Ajax Hello World Example</h3>
<h:form>
<h:inputText id="name" value="#{helloBean.name}"></h:inputText>
<h:commandButton value="Welcome Me">
<f:ajax execute="name" onevent="example" render="output" />
</h:commandButton>
<h2><h:outputText id="output" value="#{helloBean.sayWelcome}" /></h2>
</h:form>
</h:body>
And this javascript function that I call when ajax response has been completed that receive a data parameter: 而且,当ajax响应完成后,我会调用此javascript函数来接收数据参数:
function ejemplo(data){
$('#tablaEjemplo').append('<table><thead><tr><th>User</th></tr></thead>'+
'<tbody><tr><td>'+data.name+'</td></tr></tbody> </table>');
}
My pretensions are when the ajax response is success, print a table with the username of the manage bean that I inserted in the input text but I don't know how to access the parameter (the data.name obviously doesn't work) for print, I don't know really if f:ajax is returning something or I have to set up in some place (I guess in the manage bean...) something like format JSON data to retrieve and can access in my function, no?. 我的偏好是当ajax响应成功时,使用在输入文本中插入的管理bean的用户名打印一张表,但是我不知道如何访问该参数(data.name显然不起作用)打印,我真的不知道f:ajax是否正在返回某些东西,或者我是否必须在某个地方进行设置(我猜是在manage bean中...),例如格式化JSON数据以获取并可以在我的函数中访问,否? Any ideas?
有任何想法吗?
If you want to show the name, why not only adding it to the ajax rendered part like this : 如果要显示名称,为什么不仅要将其添加到ajax呈现的部分中,如下所示:
<h:commandButton value="Welcome Me">
<f:ajax render="output" />
</h:commandButton>
<h2><h:outputText id="output" value="#{helloBean.sayWelcome} #{helloBean.name}" /></h2>
If the name is null it will only not show at all. 如果名称为null,则根本不会显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.