Página não passa valores para o controller [Resolvido]

4 respostas
D
Olá, estou começando em JEE e estou fazendo um CRUD simples. Porém no Cadastro, a página está passando valores nulos e não os digitados para o controller. Alguém pode me ajudar? Seguem os códigos: Página:
<h:form>
				<h:outputLabel value="Nome: " />
				<p:inputText value="#{setorController.setor.nome}"  />
				<p:separator />
				<h:outputLabel value="Sigla: " />
				<p:inputText value="#{setorController.setor.sigla}" />
				<p:separator />
				<p:commandButton action="#{setorController.adiciona}" value="Salvar" />
			</h:form>
Controlador:
@ManagedBean
@SessionScoped
public class SetorController {

	private SetorDao repositorio = new SetorDao();

	private Setor setor = new Setor();

	private List<Setor> setorCache;

	public void adiciona() {
		System.out.println(setor.getNome());
		System.out.println(setor.getSigla());
		System.out.println(setor.getClass().toString());

		this.repositorio.insert(this.setor);
		this.setor = new Setor();
		this.setorCache = null;
	}

	public List<Setor> getSetores() {
		if (this.setorCache == null) {
			this.setorCache = this.repositorio.listAll();
		}
		return this.setorCache;
	}

	public Setor getSetor() {
		return setor;
	}

	public void setSetor(Setor setor) {
		this.setor = setor;
	}
}
Modelo:
@Entity
public class Setor implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	private String sigla;

	private String nome;

	@OneToMany(mappedBy = "setor")
	private List<Funcionario> funcionarios;

	public Setor() {
	}
//getters e setters
	}

4 Respostas

A

adicione o getSetor() como no exemplo e tente novamente

public void adiciona() {  
        System.out.println(getSetor().getNome());  
        System.out.println(getSetor().getSigla());  
        System.out.println(getSetor().getClass().toString());  
  
        this.repositorio.insert(this.setor);  
        this.setor = new Setor();  
        this.setorCache = null;  
    }
D

Não entendi, o código que você passou é o mesmo que eu colei.
Obrigado pela atenção.

A

Leia o código, não passe o olho correndo :evil:

Seu código

public void adiciona() {  
        System.out.println(setor.getNome());  // &lt;-- setor.get...
        System.out.println(setor.getSigla());   // &lt;-- setor.get... 
        System.out.println(setor.getClass().toString());    // &lt;-- setor.get...
  
        this.repositorio.insert(this.setor);  
        this.setor = new Setor();  
        this.setorCache = null;  
    }

o que eu postei

public void adiciona() {    
        System.out.println(getSetor().getNome());    // &lt;-- getSetor().get...
        System.out.println(getSetor().getSigla());     // &lt;-- getSetor().get...   
        System.out.println(getSetor().getClass().toString());        // &lt;-- getSetor().get...
    
        this.repositorio.insert(getSetor());   // &lt;-- getSetor()
        this.setor = new Setor();    
        this.setorCache = null;    
    }

recuperar o teu objeto por getSetor, e não apenas por setor.
na tua view, ao fazer isso:

&lt;p:inputText value="#{setorController.setor.nome}"  /&gt;

você vinculou o nome no setor por um getSetor().setNome(String nome) ao fazer aquilo ali
então, para recuperar o valor dele, você deverá fazer getSetor().getNome(), e não apenas setor.getNome()

D

Problema resolvido. O problema é que eu tinha setado o escopo da sessão como none no no faces-config. Por isto ele estava criando um objeto toda hora.

Obrigado andre.froes.

Criado 14 de maio de 2012
Ultima resposta 14 de mai. de 2012
Respostas 4
Participantes 2