0

I have a Users table. Added, view and delete work. But I have problem to edit.

My list page:

    <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>

I want to send the selected user on the next page: I use aciontListener:

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

My edit page where I want to send user:

        <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>

I added: <h:inputHidden value="#{userMB.user}" /> but I do not see data user, only empty field. How do I send this person? I used once this method in other project, a little different without primefaces and it worked. Why now does not work?

Method editStart:

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

faces-config:

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

When I'm on the side adminEdit I edited the pool and do editUser method:

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";
}

My method editStart use only for navigation.

The Nightmare
  • 701
  • 5
  • 16
  • 36
  • what PF version do you use? in newer versions you can pass parameters in EL context, so you could just pass the user or its id to the Method directly – Przemek Jun 27 '13 at 13:42
  • @Przemek: sorry, but this is nonsense. That's absolutely not specific to PF version being used. That's specific to EL version being used. And yes, EL 2.2 is supported given that OP has an apparently working `#{userMB.deleteUser()}` elsewhere in the code. See also http://stackoverflow.com/a/3284328 – BalusC Jun 27 '13 at 17:05
  • possible duplicate of [How can I pass a parameter to a commandLink inside a datatable?](http://stackoverflow.com/questions/4994458/how-can-i-pass-a-parameter-to-a-commandlink-inside-a-datatable) – BalusC Jun 27 '13 at 17:08

2 Answers2

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!

Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
theCowboy
  • 186
  • 2
  • 6
0

You can pass your userid through the session.

Add a parameter to your commandLink

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

In your backing bean do this

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

After that you can reload the user you want to edit with the Id you got

Przemek
  • 623
  • 2
  • 11
  • 24
  • It's not that simple. My meyhod ediStart return the String: "editStart" and in my faces-config navigation when in page adminList returnend this value I go to page adminEdit. I edited my first post, look it. – The Nightmare Jun 27 '13 at 14:53
  • okay. at first you can make it easier to forward to the next page if you return the path to the next page in your string like 'return "/path/to/file.xhtml?faces-redirect=true'. therefore you do not need to map that in the faces-config. for the second part take a look at how to pass parameters through the session. then you pass the id of the user and reload it in the next bean for use. – Przemek Jun 27 '13 at 15:16
  • Changed/updated my answer to session handled parameter passing – Przemek Jun 27 '13 at 15:43
  • 2
    Abusing HTTP session is **never** a good idea. You need to use tools designed for the purpose under question, i.e. plain get link with parameter on the sender page and view parameter on the recipient page. There are way too many examples of that trick on SO. – skuntsel Jun 27 '13 at 16:18
  • Referencing to this take a look at that http://stackoverflow.com/questions/9701590/passing-parameters-between-managed-beans-with-request-scope – Przemek Jun 27 '13 at 16:27
  • In page editUser i set when I delete it, all work. Is this a good way? All works, but i don't know whether it is good practice? – The Nightmare Jun 27 '13 at 19:06