Page does not return Managedbean

Asked

Viewed 19 times

-1

I’ve seen several similar posts, but it didn’t help much. My problem is very simple. I have a form page that will record data and display in a table using Managedbean. But when I click to write, the console shows the following error:

Caused by: javax.el.Propertynotfoundexception: Target Unreachable, Identifier [carroBean] resolved to null

Bean code:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@SessionScoped
@ManagedBean
public class CarroBean {

    private Carro carro;
    private List<Carro> listaCarros = new ArrayList<>();
    
    public void adicionar() {
        listaCarros.add(carro);
        carro = new Carro();
    }
    public Carro getCarro() {
        return carro;
    }
    public void setCarro(Carro carro) {
        this.carro = carro;
    }
    public List<Carro> getListaCarros() {
        return listaCarros;
    }
    public void setListaCarros(List<Carro> listaCarros) {
        this.listaCarros = listaCarros;
    }
}

XHTML code

<h:form>
        <p:fieldset legend="Objetos" style="margin-bottom:20px">
            <h:panelGrid columns="1" cellpadding="2">
                
                <p:outputLabel value="Modelo"/>
                <p:inputText value="#{carroBean.carro.modelo}"/>
                
                <p:outputLabel value="Fabricante"/>
                <p:inputText value="#{carroBean.carro.fabricante}"/>
                
                <p:outputLabel value="Cor"/>
                <p:inputText value="#{carroBean.carro.cor}"/>
                
                <p:outputLabel value="Ano"/>
                <p:inputText value="#{carroBean.carro.ano}"/>
                
                <p:commandButton value="Salvar" action="#{carroBean.adicionar}" update="@form"/>
                
                <p:dataTable value="#{carroBean.listaCarros}" var="carros">
                    <p:column headerText="Modelo">
                        <h:outputText value="#{carros.modelo}"/>
                    </p:column>
                    <p:column headerText="Fabricante">
                        <h:outputText value="#{carros.fabricante}"/>
                    </p:column>
                    <p:column headerText="Cor">
                        <h:outputText value="#{carros.cor}"/>
                    </p:column>
                    <p:column headerText="Ano">
                        <h:outputText value="#{carros.ano}"/>
                    </p:column>
                </p:dataTable>
                
            </h:panelGrid>
        </p:fieldset>
    </h:form>

I tried to change the button to actionListener, add Viewscoped, Requestscoped, nothing. Another thing, I don’t know if it’s an Eclipse bug, but on the xhtml page when you try to autocomplete the Bean, it only completes the attributes, but the method doesn’t, I have to write manually. As a complement, my configuration, I am using JSF 2.2.9 and am not using Maven, only Dynamic Project 4.0.

1 answer

0


my problem was more of attention anyway. After reviewing the code, I could see that I forgot to instantiate a new object in my Bean code.

private Carro carro = new Carro();

public void adicionar() {
        listaCarros.add(carro);
        carro = new Carro();
    }

So now it’s finished and solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.