1

My edit page does not display bean values when it's called from a datalist populated by a JDBC search but works just fine if the list is not from the JDBC search.

The delete link works just fine but the edit link just returns the edit page with no populated values in the fields.

My bean is a ViewScoped bean.

The datalist display page:

<h:form rendered="#{not empty usersManagedBean.usersList}" id="form">                            
<div class="table-responsive">
    &nbsp;
    <h:dataTable value="#{usersManagedBean.userModel}" 
                 var="showUser"
                 styleClass="table table-striped table-bordered table-hover">
        <h:column>
            <f:facet name="header">
                Username
            </f:facet> 
            <h:outputText value="#{showUser.username}"/>
        </h:column>
        <h:column>
            <f:facet name="header">
                First Name
            </f:facet>                                         
            <h:outputText value="#{showUser.firstname}"/>
        </h:column>
        <h:column>
            <f:facet name="header">
                Last Name
            </f:facet>                                         
            <h:outputText value="#{showUser.lastname}"/>
        </h:column>
        <h:column>
            <f:facet name="header">
                Security Level
            </f:facet>                                         
            <h:outputText value="#{showUser.groupname}"/>
        </h:column> 
        <h:column rendered="false">
            <f:facet name="header">
                Security Level
            </f:facet>                                         
            <h:outputText value="#{showUser.id}"/>
        </h:column>                                             
        <h:column>
            <h:commandLink value="Edit" 
                           action="#{usersManagedBean.updateUser()}">
            </h:commandLink>
            ||
            <h:commandLink value="Delete" 
                           onclick="if (!confirm('Are you sure, you want to delete #{showUser.username}?')) {
                        return false;
                    }
                    ;
                    return true;
                    " action="#{usersManagedBean.deleteUser(showUser)}">
            </h:commandLink>
        </h:column>
    </h:dataTable>                          
        <h:link outcome="AddUser.xhtml" value="Add New User"/>
</div>

The edit page:

                                    <h:form>
                                    <div class="form-group">
                                        <label>First Name:</label>
                                        <h:inputText id="firstname" styleClass="form-control" value="#{usersManagedBean.user.firstname}"></h:inputText>
                                        <label>Last Name:</label>
                                        <h:inputText id="lastname" styleClass="form-control" value="#{usersManagedBean.user.lastname}"></h:inputText>
                                        <label>Username:</label>
                                        <h:inputText id="username" styleClass="form-control" value="#{usersManagedBean.user.username}"></h:inputText>
                                    <div class="form-group">
                                        <label>Security Level:</label>
                                        <h:selectOneMenu id="roleSelect" value="#{usersManagedBean.user.groupname}" styleClass="form-control">
                                            <f:selectItem itemLabel="----" itemValue="----"/>
                                            <f:selectItem itemLabel="Administator" itemValue="admin"/> 
                                            <f:selectItem itemLabel="User" itemValue="user"/>                                                                                                                   
                                        </h:selectOneMenu>
                                    </div>
                                        <label>Password:</label>                                            
                                        <h:inputSecret id="password" styleClass="form-control" value="#{usersManagedBean.user.password}" required="true" requiredMessage="Enter Password"/>
                                        <h:message for="password" style="color:red"/>
                                        <label>Confirm Password:</label>
                                        <h:inputSecret id="password2" styleClass="form-control" required="true" requiredMessage="Re-enter Password"/>
                                        <h:message for="password2" style="color:red" />
                                        <o:validateEqual components="password password2" message="Passwords don't match" showMessageFor="password2"/>

                                    </div>
                                    <h:commandButton value="Update" type="submit" styleClass="btn btn-default" action="#{usersManagedBean.saveUser()}"></h:commandButton>
                                    ||
                                    <h:commandButton value="Cancel" onclick="history.back(-1);return false" styleClass="btn btn-default"></h:commandButton>

                                </h:form>

The method that calls the edit page:

    public String updateUser(){

    user = userModel.getRowData();
    return "EditUser.xhtml";
}

No exceptions are thrown at all.

ollaollu
  • 473
  • 7
  • 19

1 Answers1

0

if you use a @ViewScoped bean, jsf will create a new instance of it when you navigate from list page to edit page. you have 3 alternatives i can think of:

  • use @SessionScoped.
  • open the edit page via ajax and as a popup panel.
  • use @ViewScoped, add the user id to the url as a view parameter (ie. EditUser.xhtml?id=123), and load user data from db in edit page instead of list page, on page load. in this scenario, it's best to have different managed beans for list and edit pages.
tt_emrah
  • 1,043
  • 1
  • 8
  • 19
  • further explanation on how I can go about option 2/3 please? just about 3 weeks new to jsf @tt_emrah – ollaollu Oct 10 '14 at 20:19
  • after so many progressive errors and continous search, This http://stackoverflow.com/questions/4831888/does-view-scope-bean-survive-navigation-jsf mad me understand the real reason why JSF creates a new instance when I navigate away. This http://stackoverflow.com/questions/8459903/correct-usage-of-session-scope-in-two-page-master-detail-user-interface/8464636#8464636 helped me in solving it successfully. Thank you to BalusC who answered both posts in a clear way and thanks tt_emrah for your answer. – ollaollu Oct 23 '14 at 03:12
  • i couldn't have the chance to explain it in detail, sorry about that. i hope at least i pointed you to the right direction. thanks... – tt_emrah Oct 23 '14 at 06:01