1

我遇到以下问题:

我可以毫无问题地添加角色,但是当我需要删除或编辑它时,由于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>

编辑:我正在使用带有 primefaces 3.5 的 Glassfish 3.1

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

4

2 回答 2

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。

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

于 2013-02-23T14:55:05.930 回答
-1

在 inputtext 中尝试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);
    }

祝你好运!

于 2013-03-18T08:39:34.020 回答