0

我有一个用户表。添加、查看和删除工作。但我有问题要编辑。

我的列表页面:

    <h:form id="form">
        <p:dataTable styleClass="table" value="#{userMB.allAdmins}" var="admin" paginator="true" rows="15" rowKey="#{admin.id}" selection="#{userMB.user}" selectionMode="single">
            <f:facet name="header">
                Lista administratorów
            </f:facet>

            <p:column headerText="#{msg.firstName}">
                <h:outputText value="#{admin.firstName}" />
            </p:column>

            <p:column headerText="#{msg.lastName}">
                <h:outputText value="#{admin.lastName}" />
            </p:column>

            <p:column headerText="#{msg.personalId}">
                <h:outputText value="#{admin.personalId}" />
            </p:column>

            <f:facet name="footer">
                <p:commandButton id="viewButton" value="#{msg.info}" icon="ui-icon-search"  
                                 update=":form:display" oncomplete="userDialog.show()"/>
                <p:commandButton action="#{userMB.createStart()}" value="#{msg.add}" icon="ui-icon-plus" />
                <p:commandButton action="#{userMB.editStart()}" value="#{msg.edit}" >
                    <f:setPropertyActionListener target="#{userMB.user}" value="#{userMB.user}" />
                </p:commandButton>
                <p:commandButton action="#{userMB.deleteUser()}" value="#{msg.delete}" icon="ui-icon-close"/>
            </f:facet>
        </p:dataTable>

        <p:dialog id="dialog" header="Administrator" widgetVar="userDialog" resizable="false"
                  width="300" showEffect="clip" hideEffect="explode">

            <h:panelGrid id="display" columns="2" cellpadding="4">

                <f:facet name="header">
                    <p:graphicImage value="./../../images/person4.png" width="150" height="150"/>
                </f:facet>

                <h:outputText value="#{msg.firstName}" />
                <h:outputText value="#{userMB.user.firstName}" />

                <h:outputText value="#{msg.lastName}" />
                <h:outputText value="#{userMB.user.lastName}" />

                <h:outputText value="#{msg.personalId}" />
                <h:outputText value="#{userMB.user.personalId}" />

            </h:panelGrid>
        </p:dialog>

    </h:form>

我想在下一页发送选定的用户:我使用 aciontListener:

                <p:commandButton action="#{userMB.editStart()}" value="#{msg.edit}" >
                    <f:setPropertyActionListener target="#{userMB.user}" value="#{userMB.user}" />
                </p:commandButton>

我要发送用户的编辑页面:

        <h:form>
            <div id="userPanel">
                <h:inputHidden value="#{userMB.user}" />
                <p:panel id="panelUser" header="Edytuj administratora" >
                    <div id="panelImage">
                        <img src="./../../images/person4.png" alt="person" width="150px" height="130px"/>
                    </div>
                    <h:panelGrid columns="3">
                        <p:outputLabel for="firstName" value="#{msg.firstName}"></p:outputLabel>
                        <p:inputText id="firstName" value="#{userMB.user.firstName}" label="#{msg.firstName}" required="true">
                            <f:validator validatorId="nameValidator" />  
                            <p:ajax update="msgFristName" event="keyup" />  
                        </p:inputText>  
                        <p:message for="firstName" id="msgFristName"/> 

                        <p:outputLabel for="lastName" value="#{msg.lastName}"></p:outputLabel>
                        <p:inputText id="lastName" value="#{userMB.user.lastName}" label="#{msg.lastName}" required="true">
                            <f:validator validatorId="nameValidator" />   
                            <p:ajax update="msgLastName" event="keyup" /> 
                        </p:inputText>  
                        <p:message for="lastName" id="msgLastName"/> 

                        <p:outputLabel for="personalId" value="#{msg.personalId}"></p:outputLabel>
                        <p:inputText id="personalId" value="#{userMB.user.personalId}" label="#{msg.personalId}" required="true">
                            <f:validator validatorId="personalIdValidator" />   
                            <p:ajax update="msgPersonalId" event="keyup" /> 
                        </p:inputText>  
                        <p:message for="personalId" id="msgPersonalId"/>

                        <p:outputLabel for="password" value="#{msg.password}"></p:outputLabel>

                </p:panel>
            </div>
        </h:form>

我补充说:<h:inputHidden value="#{userMB.user}" />但我没有看到数据用户,只有空字段。我如何发送这个人?我在其他项目中使用过一次这种方法,没有primefaces有点不同并且它有效。为什么现在不起作用?

方法编辑开始:

public String editStart() {
    return "editStart";
}

面孔配置:

<navigation-case>
    <from-outcome>editStart</from-outcome>
    <to-view-id>/protected/admin/adminEdit.xhtml</to-view-id>
    <redirect/>
</navigation-case>

当我在 adminEdit 一边时,我编辑了池并执行了 editUser 方法:

public String editUser() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map requestParameterMap = (Map) context.getExternalContext().getRequestParameterMap();
    try {
        String userRole = requestParameterMap.get("userRole").toString();
        String active = requestParameterMap.get("active").toString();
        Boolean act = Boolean.parseBoolean(active);
        user.setRole(userRole);
        user.setActive(act);
        userDao.update(user);
    } catch (EJBException e) {
        sendErrorMessageToUser("Edit error");
        return null;
    }
    sendInfoMessageToUser("Account edited");
    return user.getRole() + "List";
}

我的方法 editStart 仅用于导航。

4

2 回答 2

1

You can either consider Przemek's answer or use one of the 4 ways of passing a parameter to a backing bean. 1. Method expression 2. f:param 3. f:atribute 4. f:setPropertyActionListener

For very well explained full explanation refer to this

It has easy to understand examples. Take a look and pick what fits your needs.

Or simply:

In the actual managed bean...

public String navigationButtonListener(User parameter) {
    FacesContext.getCurrentInstance().getExternalContext().getRequestMap()
            .put("parameterAttribute", parameter);
    return "my-destination-page";
}

In the destination managed bean...

@PostConstruct
public void init(){
    myAttribute= (User) FacesContext.getCurrentInstance()
    .getExternalContext().getRequestMap().get("parameterAttribute");

}

Good Luck!

于 2013-06-28T23:44:02.517 回答
0

您可以通过会话传递您的用户 ID。

将参数添加到您的 commandLink

<p:commandButton action="#{userMB.editStart()}" value="#{msg.edit}>
    <f:param name="userId" value="#{userMB.user.id}" />
</p:commandButton>

在你的支持 bean 中执行此操作

FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest myRequest = (HttpServletRequest)context.getExternalContext().getRequest();
HttpSession mySession = myRequest.getSession(); 
Integer userId = Integer.parseInt(myRequest.getParameter("userId"));

之后,您可以使用您获得的 ID 重新加载要编辑的用户

于 2013-06-27T14:39:25.337 回答