2

My managed bean gets null values from a simple form in a jsf page. In reality I have a fairly developed interface but the problem is happening even with a simple form.

(JSF 2.1.0, java compiler 1.6, primefaces 6.0 and tomcat7)

It seems to be a duplicated question. But, 5 days ago I am looking for the solution to this strange problem. I found a lot of solutions, but it does not work for me. Maybe I did not quite understand them as the answer from BalusC (commandButton/commandLink/ajax action/listener method not invoked or input value not updated) .

The JSF page:

<?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">
<html 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:p="http://primefaces.org/ui"
    xmlns:c="http://java.sun.com/jsp/jstl/core"> 
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</h:head>    
<h:body>
    <h:form>
            <h:inputText value="#{notification.typeSelected}"></h:inputText>
            <p:commandButton value="Enregistrer"
                actionListener="#{notificationBean.filtrer}" process="@this"
                style="float:right">
            </p:commandButton>
        </h:form>
</h:body>
</html>

The managed bean:

package presentation;
import java.util.ArrayList;
import java.util.List;    
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
//import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;    
import org.apache.log4j.Logger;    
import entities.Contrainte;
import metier.ContrainteMetier;
import metier.ContrainteMetierImp;    
@ManagedBean
@SessionScoped
public class NotificationBean implements java.io.Serializable{
    private static final long serialVersionUID = 1L;

    public static Logger log = Logger.getLogger(ProjetMODBean.class);

    private ContrainteMetier contrainteM = new ContrainteMetierImp();
    private List<Contrainte> contrainteL;
    private List<Contrainte> filtreL;
    private List<SelectItem> classificationL;
    private String typeSelected;
    //Constructeurs
    public NotificationBean() {
    }
    public NotificationBean(ContrainteMetier contrainteM, List<Contrainte> contrainteL) {
        super();
        this.contrainteM = contrainteM;
        this.contrainteL = contrainteL;
    }

    @PostConstruct
    public void init() {

        contrainteL = contrainteM.findAll();
        classificationL = new ArrayList<SelectItem>();

        for(Contrainte c:contrainteL)
        {//Attention, il ne faut pas dupliquer les types dans la liste déroulante           
            classificationL.add(new SelectItem(c.getClassification()));
        }
        log.info(" dans init je filtre selon: " + getTypeSelected());


    }


    //Méthodes
    public void filtrer(ActionEvent e)
    {
        setTypeSelected(typeSelected);
        log.info("je filtre selon: " + getTypeSelected());      
    }

The console output:

2017-03-24 18:06:43 INFO  ProjetMODBean:57 -  dans init je filtre selon: null
2017-03-24 18:06:43 INFO  ProjetMODBean:76 - je filtre selon: null  

I tried to correct my error but a detail escapes me.

Community
  • 1
  • 1
AhmedAmine
  • 21
  • 4
  • accessing a field in a postConstruct that is not injected is strange. I'd expect NPE's then or you use the bean in a wrong way – Kukeltje Apr 01 '17 at 09:25

1 Answers1

4

Imho your problem is process="@this" change it to process="@form" (it is default value so change it only if you have good reason) or add id to input and use process="@this idOfInputField".

See question.

Please post questions fully in english. I will assume, that //Méthodes mean getter/setter for fields (JSF require getter/setter for inputs).

Also use injection for services. Change method header from public void filtrer(ActionEvent e) to public void filtrer()

Community
  • 1
  • 1
Vaclav Stengl
  • 359
  • 3
  • 8