How to access a "List" from the screen?

Asked

Viewed 84 times

1

I am trying to access a list of values in my code through the JSF screen, but the screen does not find the method that lists the values, someone can help me?

I want to access the method values listaNatures in f:selectItens.

Follow the Java code:

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class Pokemon {
    private String nome;
    private Integer hp;
    private Integer attack;
    private Integer defence;
    private Integer specAttack;
    private Integer specDefense;
    private Integer speed;
    private String nature;


    public String getNature() {
        return nature;
    }
    public void setNature(String nature) {
        this.nature = nature;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public Integer getHp() {
        return hp;
    }
    public void setHp(Integer hp) {
        this.hp = hp;
    }
    public Integer getAttack() {
        return attack;
    }
    public void setAttack(Integer attack) {
        this.attack = attack;
    }
    public Integer getDefence() {
        return defence;
    }
    public void setDefence(Integer defence) {
        this.defence = defence;
    }
    public Integer getSpecAttack() {
        return specAttack;
    }
    public void setSpecAttack(Integer specAttack) {
        this.specAttack = specAttack;
    }
    public Integer getSpecDefense() {
        return specDefense;
    }
    public void setSpecDefense(Integer specDefense) {
        this.specDefense = specDefense;
    }
    public Integer getSpeed() {
        return speed;
    }
    public void setSpeed(Integer speed) {
        this.speed = speed;
    }

    public List<String> listaNatures(){
        List<String> lista = new ArrayList<String>();


        lista.add("Adamant");
        lista.add("Jolly");

        return lista;
    }


}

And this is the 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">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <link rel="StyleSheet" type="text/css" href="css/estilos.css" />

    <title>Tela</title>
</h:head>
<h:body>
    <h:form>
        <p:panel header="Calcular IV's" styleClass="cabecalho">
            <h:panelGrid columns="6">
                <p:outputLabel value="Pokémon" for="pokemon" />
                <p:selectOneMenu id="pokemon">
                    <f:selectItem itemLabel="Selecione um Pokémon" />
                </p:selectOneMenu>

                <p:outputLabel value="Nature" for="nature" styleClass="a" /> 
                <p:selectOneMenu id="nature" value="#{pokemon.nature}">
                <f:selectItems value="#{pokemon}"/>
                </p:selectOneMenu>
                <p:outputLabel value="Level" styleClass="a" />
                <p:inputText maxlength="3" size="4" />
                <f:facet name="reader">
                    <p:row>
                        <p:column rowspan="4">Status</p:column>
                    </p:row>
                </f:facet>
            </h:panelGrid>

        </p:panel>
    </h:form>
</h:body>
</html>

1 answer

2


Just use:

<p:selectOneMenu id="nature" value="#{pokemon.nature}">
     <f:selectItems value="#{pokemon.listaNatures()}"/>
</p:selectOneMenu>

And another suggestion, it seems to me you are using your Entity as a Managedbean, recommend creating a Bean that will control your Entity.

  • thank you very much, could you give an example of what this controller class looks like? I’m new to the subject yet.

  • http://www.crisaltmann.com.br/blog/index.php/2011/11/10/tutorial-jsf-2-0-criando-e-utilizando-managedbean/

Browser other questions tagged

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