Search code examples
jsficefaces

Commandbuttons problems


I have very strange problem. I can't use function in second button.
Steps:
1. Write forename and surename.
2. Click "find" button. Result: items in datatable.
3. Click "TEMPPP" only refresh page.
I have to use function sendUserInformation()

Administrator.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ace="http://www.icefaces.org/icefaces/components"
template="/WEB-INF/templates/template.xhtml">
<ui:define name="menuItems">

</ui:define>
<ui:define name="logedContent">


<h:form id="form1t">
    <center><h:outputLabel value="#{msg.find_employee}"/></center>
    <table class="tableGeneral">
        <tr>
            <td><h:outputLabel value="#{msg.forename}*"/></td>
            <td>
                <h:inputText id="name1" value="#{administrationController.forename}">
                    <f:validator validatorId="DefaultStringValidator"/>
                </h:inputText>
            </td>
            <td>
                <h:message styleClass="error" for="name1"/>
            </td>
        </tr>
        <tr>
            <td><h:outputLabel value="#{msg.surename}*"/></td>
            <td>
                <h:inputText id="name2" value="#{administrationController.surename}">
                    <f:validator validatorId="DefaultStringValidator"/>
                </h:inputText>
            </td>
            <td>
                <h:message styleClass="error" for="name2"/>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <h:commandButton styleClass="buttonGeneral" value="#{msg.find}" >
                    <!--  <f:ajax event="click" execute="name1 name2" render=":form1t:form2:tab" listener="#{administrationController.find_employee()}"/> -->
                    <f:ajax event="click" execute="name1 name2" render="@all" listener="#{administrationController.find_employee()}"/>
                </h:commandButton>
            </td>
        </tr>
    </table>
</h:form>
    <hr/>
    <br/>
<h:form id="form2">
    <center><h:outputLabel value="#{msg.found}"/></center>
    <h:dataTable id="tab" value="#{administrationController.foundUsers}" styleClass="tableGeneral" var="o">
        <h:column>
            <f:facet name="header"><h:outputLabel value="#{msg.forename}"/></f:facet>
            <h:outputText value="#{o.forename}"/>
        </h:column>
        <h:column>
            <f:facet name="header"><h:outputLabel value="#{msg.surename}"/></f:facet>
            <h:outputText value="#{o.surename}"/>
        </h:column>
        <h:column>
            <f:facet name="header"><h:outputLabel value="#{msg.job}"/></f:facet>
            <h:outputText value="#{o.job}"/>
        </h:column>
        <h:column>
            <f:facet name="header"><h:outputLabel value="#{msg.user}"/></f:facet>
            <h:outputText value="#{o.login}"/>
        </h:column>
        <h:column>
            <f:facet name="header"><h:outputLabel value="#{msg.activated}"/></f:facet>
            <h:outputText value="#{msg.yes}" rendered="#{o.activated}"/>
            <h:outputText value="#{msg.no}" rendered="#{!o.activated}"/>
        </h:column>
        <h:column>
        <!-- <h:commandButton value="TEMPPP #{o.id}" action="#{administrationController.sendUserInformation(o.id)}"/> -->
            <!--  
            <h:commandButton value="TEMPPP #{o.id}" action="#{administrationController.sendUserInformation()}">
                <f:param name="id" value="#{o.id}"/>
            </h:commandButton>
            -->

            <h:commandButton value="TEMPPP #{o.id}" action="#{administrationController.sendUserInformation(o.id)}" styleClass="buttonGeneral" >

            </h:commandButton>
            <!--  
            <h:button value="TEMPPP #{o.id}" outcome="userInformation.xhtml">
                <f:param name="id" value="#{o.id}"/>
            </h:button>
            -->
        </h:column>
    </h:dataTable>
</h:form>

</ui:define>
</ui:composition>



AdministrationController

package controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import others.MenuBars;
import database.Queries;
import users.User;

public class AdministrationController implements Serializable {

private static final long serialVersionUID = -2151888463865423931L;
private Queries queries = (Queries) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("queries");
private MenuBars menubars = (MenuBars)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("menuBars");
private List<User> users = new ArrayList<User>();
private List<User> foundUsers = new ArrayList<User>();
private String forename = "";
private String surename = "";

public AdministrationController(){
    System.out.println("AdministrationController()");
    users = queries.getAllUsers();
}
/*
public void tempUser(ValueChangeEvent event){
    System.out.println(event.getNewValue().getClass());
    User u = (User) event.getNewValue();
    //TODO
    System.out.println("idz do informacji o uzytkowniku"+u.name());
}*/

public String getForename() {
    return forename;
}

public void setForename(String forename) {
    this.forename = forename;
}

public String getSurename() {
    return surename;
}

public void setSurename(String surename) {
    this.surename = surename;
}

public void find_employee(){
    System.out.println("find_employee()");
    foundUsers.clear();
    for(User u: users){
        if(u.getForename().equalsIgnoreCase(getForename()) && u.getSurename().equalsIgnoreCase(getSurename())){
            foundUsers.add(u);
        }
    }
}

public List<User> getFoundUsers() {
    return foundUsers;
}

public void setFoundUsers(List<User> foundUsers) {
    this.foundUsers = foundUsers;
}

public String sendUserInformation(String id){
    System.out.println("sendUserInformation; id = "+id);
    menubars.addToMap("id", id);
    return "userInformation.xhtml";
}
}

SOLVED!
f:ajax was needed (and probably viewscope)

<h:commandButton action="#{administrationController.sendUserInformation(o.id)}" styleClass="buttonGeneral" value="TEMP #{o.id}">
                <f:ajax/>
            </h:commandButton>

Solution

  • Assuming that your AdministrationController bean is @RequestScoped (since you haven't specified it's scope), then the data in your <h:dataTable> will be reloaded per every request in the same view. This means, when you select

    <h:commandButton value="TEMPPP #{o.id}" action="#{administrationController.sendUserInformation(o.id)}"/>
    

    This will fire a new AdministrationController and the data in the List will be lost, which will break your action since o.id is not available anymore.

    To solve this, use a wider scope like @ViewScoped at least.

    @ManagedBean
    @ViewScoped
    public class AdministrationController implements Serializable {
        //rest of your code
    }