将变量从 bean 传递到 primefaces 页面
Pass variable from bean to primefaces page
这里是另一个 Primefaces 新手。
我想将数据从 bean 传递到 XHTML 页面以便在 javascript 中使用。让我澄清一下,primefaces XHTML 页面没有组件,因此像“#{bean.property}”这样的普通绑定不在我的要求中。
有没有办法在页面加载时将bean变量传递给页面,然后将其用作javascript中的参数。
如您所见,我尝试使用 RequestContext 传递 "selectedProject",但无法使其正常工作。
我的场景代码:
豆子:
@Named(value = "ganttBean")
@SessionScoped
public class GanttBean implements Serializable {
@EJB
ProjectService projectService;
private Long selectedProjectId;
private Project selectedProject = null;
public GanttBean() {
}
public void init(){
//get the actual project by ID.
this.selectedProject = projectService.findById(selectedProjectId);
//package all the tasks of the selected project in a JSON object
jasonifyProject(selectedProject);
RequestContext.getCurrentInstance().addCallbackParam("selectedProject", selectedProject);
//RequestContext.getCurrentInstance().execute("ggg()");
}
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">
<ui:composition template="../../WEB-INF/templates/master/default.xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:em="http://java.sun.com/jsf/composite/ezcomp"
xmlns="http://www.w3.org/1999/xhtml">
<ui:define name="content">
<f:metadata>
<f:viewParam name="projectId" value="# {ganttBean.selectedProjectId}"/>
<f:event type="preRenderView" listener="#{ganttBean.init()}"/>
</f:metadata>
<h1 class="title ui-widget-header ui-corner-all">
Project Gantt View
</h1>
<h:outputScript library="jsGantt" name="jquery.1.8.js"/>
<h:outputScript library="jsGantt" name="jquery-ui.min.js"/>
<h:outputStylesheet library="jsGantt" name="jsgantt.css"/>
<h:outputScript library="jsGantt" name="jsgantt.js" target="head"/>
<div style="position:relative" class="gantt" id="GanttChartDIV">
The gantt chart container
</div>
<script type="text/javascript">
var ggg = function(datafrombean){
....do something with datafrombean....
};
//ionstantiate a gantt chart.
var g = new JSGantt.GanttChart('g',document.getElementById('GanttChartDIV'), 'day');
//add tasks.
g.AddTaskItem(new JSGantt.TaskItem(3, 'Define Chart API', '2/10/2008','25/10/2008','000ff2', 'http://help.com', 0, 'Brian', 0, 1, 0, 1));
g.AddTaskItem(new JSGantt.TaskItem(12, 'Chart Object', '2/10/2008', '25/10/2008', 'ff00ff', 'http://www.yahoo.com', 0, 'Shlomy', 100, 0,3, 1));
//draw the chart
g.Draw();
g.DrawDependencies();
</script>
<h:form id="test">
<h:inputHidden id="hiddenInput"
value="hidden"/>
<p:dialog id="editTaskDialog" widgetVar="editTaskDialog" header="Edit Task">
</p:dialog>
</h:form>
</ui:define>
</ui:composition>
嗯....让我们稍微考虑一下它是如何工作的。
Javascript 运行在客户端,纯前端。
JSF 在服务器端运行并将其组件呈现给 HTML,
因此它具有读取服务器端属性、您的 bean,然后在客户端呈现属性的优势。
因此,需要服务器端和前端之间的桥梁,JSF 已经做到了。
您声称您的页面没有组件,因此普通绑定不能满足您的要求,但是...不可见组件怎么办?
JSF:
<h:form id="formTest">
<h:inputText style="display:none" id="idComp1" value="#{ganttBean.property}"></h:inputText>
</h:form>
JS:
<script type="text/javascript">
function(){
var datafrombean = document.getElementById('formTest:idComp1').value
};
</script>
我不确定你要调用多少次以及何时调用该函数,但你可能必须使用 ajax 再次渲染不可见组件,然后再次调用 JS 函数。
此外,我确定还有其他简单的选项,这只是一种方法
您可以简单地使用 EL in javascript:
<script type="text/javascript">
function(){
var datafrombean = #{bean.data};
};
</script>
创建页面时调用 bean 上的 getData()
。
这里是另一个 Primefaces 新手。
我想将数据从 bean 传递到 XHTML 页面以便在 javascript 中使用。让我澄清一下,primefaces XHTML 页面没有组件,因此像“#{bean.property}”这样的普通绑定不在我的要求中。
有没有办法在页面加载时将bean变量传递给页面,然后将其用作javascript中的参数。
如您所见,我尝试使用 RequestContext 传递 "selectedProject",但无法使其正常工作。 我的场景代码:
豆子:
@Named(value = "ganttBean")
@SessionScoped
public class GanttBean implements Serializable {
@EJB
ProjectService projectService;
private Long selectedProjectId;
private Project selectedProject = null;
public GanttBean() {
}
public void init(){
//get the actual project by ID.
this.selectedProject = projectService.findById(selectedProjectId);
//package all the tasks of the selected project in a JSON object
jasonifyProject(selectedProject);
RequestContext.getCurrentInstance().addCallbackParam("selectedProject", selectedProject);
//RequestContext.getCurrentInstance().execute("ggg()");
}
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">
<ui:composition template="../../WEB-INF/templates/master/default.xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:em="http://java.sun.com/jsf/composite/ezcomp"
xmlns="http://www.w3.org/1999/xhtml">
<ui:define name="content">
<f:metadata>
<f:viewParam name="projectId" value="# {ganttBean.selectedProjectId}"/>
<f:event type="preRenderView" listener="#{ganttBean.init()}"/>
</f:metadata>
<h1 class="title ui-widget-header ui-corner-all">
Project Gantt View
</h1>
<h:outputScript library="jsGantt" name="jquery.1.8.js"/>
<h:outputScript library="jsGantt" name="jquery-ui.min.js"/>
<h:outputStylesheet library="jsGantt" name="jsgantt.css"/>
<h:outputScript library="jsGantt" name="jsgantt.js" target="head"/>
<div style="position:relative" class="gantt" id="GanttChartDIV">
The gantt chart container
</div>
<script type="text/javascript">
var ggg = function(datafrombean){
....do something with datafrombean....
};
//ionstantiate a gantt chart.
var g = new JSGantt.GanttChart('g',document.getElementById('GanttChartDIV'), 'day');
//add tasks.
g.AddTaskItem(new JSGantt.TaskItem(3, 'Define Chart API', '2/10/2008','25/10/2008','000ff2', 'http://help.com', 0, 'Brian', 0, 1, 0, 1));
g.AddTaskItem(new JSGantt.TaskItem(12, 'Chart Object', '2/10/2008', '25/10/2008', 'ff00ff', 'http://www.yahoo.com', 0, 'Shlomy', 100, 0,3, 1));
//draw the chart
g.Draw();
g.DrawDependencies();
</script>
<h:form id="test">
<h:inputHidden id="hiddenInput"
value="hidden"/>
<p:dialog id="editTaskDialog" widgetVar="editTaskDialog" header="Edit Task">
</p:dialog>
</h:form>
</ui:define>
</ui:composition>
嗯....让我们稍微考虑一下它是如何工作的。
Javascript 运行在客户端,纯前端。 JSF 在服务器端运行并将其组件呈现给 HTML, 因此它具有读取服务器端属性、您的 bean,然后在客户端呈现属性的优势。
因此,需要服务器端和前端之间的桥梁,JSF 已经做到了。
您声称您的页面没有组件,因此普通绑定不能满足您的要求,但是...不可见组件怎么办?
JSF:
<h:form id="formTest">
<h:inputText style="display:none" id="idComp1" value="#{ganttBean.property}"></h:inputText>
</h:form>
JS:
<script type="text/javascript">
function(){
var datafrombean = document.getElementById('formTest:idComp1').value
};
</script>
我不确定你要调用多少次以及何时调用该函数,但你可能必须使用 ajax 再次渲染不可见组件,然后再次调用 JS 函数。
此外,我确定还有其他简单的选项,这只是一种方法
您可以简单地使用 EL in javascript:
<script type="text/javascript">
function(){
var datafrombean = #{bean.data};
};
</script>
创建页面时调用 bean 上的 getData()
。