![](/img/trans.png)
[英]JSF (and PrimeFaces) How to pass parameter to a method in ManagedBean
[英]Primefaces - how to pass parameter between 2 ui:define
我需要在两个窗体之间传递参数,这两个窗体都位于单独的ui:define中。
我有一个网站,其中的左边部分是树形表,中间部分是表格。 我想单击左侧的节点,并将其ID传递到中心部分,稍后将使用它。
目的是使用户能够向左侧树表添加新类别。 因此,我想到了一个想法,如果用户单击树表上的“ +”号,则会显示一个窗体,该窗体直到那时一直处于隐藏状态。
这是布局的主要部分。
<p:layoutUnit position="west" size="600" header="Categories" resizable="true" closable="false" collapsible="true">
<h:form>
<ui:insert name="westContent">West default content</ui:insert>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="center">
<h:form>
<ui:insert name="centerContent">Center default content</ui:insert>
</h:form>
</p:layoutUnit>
这是树表所在的左侧内容。 我想将document.id传递给位于centerContent的另一个表单。
<ui:define name="westContent">
<h:form id="form">
<p:treeTable value="#{documentsController.root}" var="document"
selection="#{documentsController.selectedNodes}" selectionMode="single">
<p:column style="width:300px">
<f:facet name="header">
Name
</f:facet>
<h:outputText value="#{document.name}" />
</p:column>
<p:column style="width:20px">
<f:facet name="header">
Add Category
</f:facet>
<p:commandButton value="+" styleClass="ui-priority-primary"
actionListener="#{editorBean.print}"
onclick="addCategory.show();">
</p:commandButton>
</p:column>
</p:treeTable>
</h:form>
</ui:define>
这是我希望将document.id传递到的中心内容。
<ui:define name="centerContent">
<h:form id="addCategoryForm">
<p:panel id="addCategory" widgetVar="addCategory" header="New Category" style="margin-bottom:10px;" closable="true" visible="false">
<p:messages id="messages" />
<h:panelGrid columns="3">
<h:outputLabel for="name" value="Name: *" />
<p:inputText id="name" value="#{editorBean.name}" required="true" label="Name">
<f:validateLength minimum="2" />
</p:inputText>
<p:message for="name" />
<h:outputLabel for="description" value="Description: *" />
<p:inputText id="description" value="#{editorBean.description}" required="true" label="Description"/>
<p:message for="description" />
</h:panelGrid>
<h:panelGrid columns="1">
<h:outputLabel value="Html" />
<pe:ckEditor id="editor" value="#{editorBean.html}" interfaceColor="#cccccc" />
<p:commandButton id="submitButton" value="Submit" update="addCategoryForm"
icon="ui-icon-disk" actionListener="#{editorBean.print}" />
</h:panelGrid>
</p:panel>
</h:form>
</ui:define>
这是我正在使用的托管bean的结构。
@ManagedBean
@Scope("view")
public class EditorBean {
private String name;
private String description;
private String html;
private boolean isCategory;
private int id;
}
我习惯使用jsp和构造函数中处理事情的旧样式,因此这对我来说很混乱。 我愿意接受其他任何解决方案。
我以为可以将树表中的ID填充到EditorBean中,然后在其余部分填充其余的ID,但似乎不起作用。
感谢您的回覆
只需在bean中具有一个属性,该属性就可以通过左侧窗体中的命令按钮保留当前选定的文档,并以您看到的任何方式从右侧窗体中对其进行访问。
例如,向您的bean添加一个属性:
private Document selected;//getter + setter
使用命令按钮的操作方法(EL 2.2及更高版本)或使用<f:setPropertyActionListener>
(EL低于2.2)对其进行预设。 还要注意, onclick
是钩住AJAX回调事件的错误位置,请改用oncomplete
:
<p:commandButton value="+" styleClass="ui-priority-primary"
actionListener="#{editorBean.print}"
action="#{editorBean.action(document)}"
oncomplete="addCategory.show();">
</p:commandButton>
与
public void action(Document document) {
selected = document;
//other business logic
}
要么:
<p:commandButton value="+" styleClass="ui-priority-primary"
actionListener="#{editorBean.print}"
oncomplete="addCategory.show();">
<f:setPropertyActionListener target="#{editorBean.selected}" value="#{document}" />
</p:commandButton>
这样,当前单击的项目将在您的bean中可用。 如果需要,可以使用操作方法对其进行分解。 它可以由其他表单引用,并且其他表单的ID可以包含在AJAX更新中,以便这些更改可以反映在其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.