【问题标题】:JSF primefaces update dialog not passing value to beanJSF primefaces更新对话框未将值传递给bean
【发布时间】:2013-02-23 14:31:24
【问题描述】:

我遇到以下问题:

我可以毫无问题地添加角色,但是当我需要删除或编辑它时,由于 roleController.selectedRolenull 而无法正常工作。 起初,我将托管 bean 用作 requestScope,但我认为 null 可能来自那里,因为每个请求都会重新创建 bean。 所以我改为 ViewScoped 修复了添加按钮再次工作,但我仍然在编辑和删除时遇到同样的问题。

发生的情况如下:我选择一行并单击按钮编辑。这将正确显示包含角色信息的对话框。但是当我单击编辑时,我得到空值。 我看过几个例子,似乎我没有做错任何事。但我可能会遗漏一些非常基本的东西:/

任何见解将不胜感激!

至于豆子,我有以下几点:

@ManagedBean
@RequestScoped
....
private Roles selectedRole = new Roles();
(I have the normal setter and getter)
    public void edit() {
        Logger.getGlobal().log(Level.INFO, "====> EDIT ROLE" + selectedRole.getRole());
    }

页面如下,省略了ui:define和header的东西。

<h:form id="contentView">
    <p:dataTable id="lstRoles" var="r" value="#{roleController.rolesList}" selectionMode="single" 
                 selection="#{roleController.selectedRole}" rowKey="#{r.role}" paginator="true"
                 paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {CurrentPageReport}  {RowsPerPageDropdown}"
                 rowsPerPageTemplate="10,15,50" rows="10">
        <p:column headerText="Role" sortBy="#{r.role}">
            <p:outputLabel value="#{r.role}"></p:outputLabel>
        </p:column>
        <p:column headerText="Description">
            <h:outputLabel value="#{r.description}"></h:outputLabel>
        </p:column>                
        <f:facet name="footer">
            <p:commandButton value="New" icon="ui-icon-star" oncomplete="newRoleDialog.show()"></p:commandButton>
            <p:commandButton value="Edit" icon="ui-icon-check" oncomplete="editRoleDialog.show()" update=":editRoleForm:editRolePanel"></p:commandButton>
            <p:commandButton value="Delete" icon="ui-icon-trash"></p:commandButton>
        </f:facet>
    </p:dataTable>
    <p:blockUI block="lstRoles" trigger="lstRoles">
        LOADING 
    </p:blockUI>
</h:form>

<!-- Edit User -->
<p:dialog header="Edit User" widgetVar="editRoleDialog" resizable="false">
    <h:form id="editRoleForm">
        <p:panelGrid id="editRolePanel" columns="2">
            <h:outputText value="Role: "></h:outputText>
            <h:outputText value="#{roleController.selectedRole.role}"></h:outputText>

            <h:outputText value="Description: "></h:outputText>
            <p:inputText value="#{roleController.selectedRole.description}" required="true"></p:inputText>

            <f:facet name="footer">
                <p:commandButton value="Confirm" update=":contentView:lstRoles :growl" oncomplete="handleSubmitRequest(xhr, status, args, 'editRoleDialog','editRoleForm');" actionListener="#{roleController.edit()}"></p:commandButton>
                <p:commandButton type="reset" value="reset"></p:commandButton>
            </f:facet>
        </p:panelGrid>
    </h:form>
</p:dialog>

编辑:我正在使用 Glassfish 3.1 和 primefaces 3.5

编辑 2: 所以,看来我不能使用输出标签。如果我更改为输入,那么我会在 managedbean 中获得所需的值(我猜这是因为它调用了 setter,尽管我假设它在选择行时已经被处理)。但我不想编辑第一个字段,因为这是 PK 键,它也用作表中的 FK。但至少知道我知道发生了什么,或多或少。

【问题讨论】:

    标签: jsf primefaces


    【解决方案1】:

    你需要将从数据表中选择的角色设置为 managedbean 的 selectedrole 属性,试试这个:

    <p:commandButton value="Edit" icon="ui-icon-check" oncomplete="editRoleDialog.show()" update=":editRoleForm:editRolePanel">
        <f:setPropertyActionListener target="#{roleController.selectedRole}" value="#{r}"/>
    </p:commandButton>
    

    您很可能也需要将 bean 设为 ViewScoped。

    编辑:我不知道数据表的选择功能,澄清你是否正在使用你不需要上面的代码。

    【讨论】:

    • 谢谢,但这也不起作用:/如果我将 bean 更改为 viewscoped all stop working,我想我需要添加额外的 如果我保留为 RequestScope 并且该属性始终指向第一个记录,并且 bean 仍然为空。我会尝试将所有内容都转换为 ViewScope 并查看结果!!!谢谢
    • 仔细查看代码。 OP 正在使用&lt;p:dataTable selection&gt;。在编辑对话框中打开数据工作正常。 OP 在提交编辑对话框时抱怨它不再可用。
    【解决方案2】:

    在输入文本中尝试 valueChangeListener 属性。您通常很难在 @SessionScope 中拥有实体的一部分。如果你使用它肯定会工作

    <inputText value="#{bean.value}"/>
    

    但如果你使用可能无法工作

    <inputText value="#{bean.entity.value}"/>
    

    。使用 valueChangeListener 并创建这样的方法可以强制为您的实体保存价值。

    public void saveValue(ValueChangeEvent event) {
            Integer newValue = (Integer) event.getNewValue();//u can use getOldValue to get value before
            entity.setValue(newValue);
        }
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多