Select option from c:foreach | JSP - JSTL

Asked

Viewed 341 times

0

Goodnight,

I need help with a JSTL code. I need to select an option listed by a c:foreach and send it to my Contoller. This data comes from the database.

To better understand the logic, I have in the bank saved several types of parts (in the system I call groupPeca) as bolt, nut and washer. I want to save a new code from a new piece that has arrived, so I look for a type of piece (groupPeca) in which it belongs. I select the type of the part and save along with the new code. A code has only one groupPeca and a groupPeca can have several codes.

This is my controller that sends the data to the JSP page

    todasGrupoPeca = CRUDBestRoute.buscarTodos(GrupoPeca.class);
    result.include("grupoPeca", todasGrupoPeca);

This is my code that loops the data screen from the database

<div class="form-group row">
            <label for="descricao" class="col-sm-2 col-form-label">Descrição</label>
            <div class="col-sm-10">
                <div class="form-row align-items-center">
                    <div class="col-sm-12">
                        <label class="mr-sm-2 sr-only" for="descricao">Preference</label>

                        <select name="descricao">
                            <option  selected="selected" >Selecione a descrição</option>
                            <c:forEach var="todasGrupoPeca" items="${grupoPeca}">
                                <option selected="selected" value="${p.grupoPeca}">
                                    <c:out value="${todasGrupoPeca.descricao}" />
                                </option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
            </div>
        </div>

After I select the desired option on the page, I want to save this information in a variable so I can save in the bank with the new code, I have the following code

    public static Peca p = new Peca();  
    public void adicionarPeca() {       
        System.out.println("Chamou o adicionar peca");
        System.out.println(p.getGrupoPeca());           
    }

Peca is my entity, where I want to store what I selected to finally save in the bank, below my entity Peca

@Entity
public class Peca {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idPeca")
private int idPeca;

@Column(nullable = false, length = 50)
private String codigo;

@ManyToOne
@JoinColumn(name = "grupoPeca")
private GrupoPeca grupoPeca;

@ManyToOne
@JoinColumn(name = "celula")
private Celula celula;

@ManyToOne
@JoinColumn(name = "prateleira")
private Prateleira prateleira;

@ManyToOne
@JoinColumn(name = "idPonto")
private Ponto ponto;



public Ponto getPonto() {
    return ponto;
}

public void setPonto(Ponto ponto) {
    this.ponto = ponto;
}

public Celula getCelula() {
    return celula;
}

public void setCelula(Celula celula) {
    this.celula = celula;
}

public Prateleira getPrateleira() {
    return prateleira;
}

public void setPrateleira(Prateleira prateleira) {
    this.prateleira = prateleira;
}

public GrupoPeca getGrupoPeca() {
    return grupoPeca;
}

public void setGrupoPeca(GrupoPeca grupoPeca) {
    this.grupoPeca = grupoPeca;
}

public int getIdPeca() {
    return idPeca;
}

public void setIdPeca(int idPeca) {
    this.idPeca = idPeca;
}

public String getCodigo() {
    return codigo;
}

public void setCodigo(String codigo) {
    this.codigo = codigo;
}

The entity Peca has several attributes code, groupPeca, shelf, cell and point. Apart from the code the other information is listed from the bank so I choose the desired option.

I have been trying to do this way that I have shown above based on my research, if you can help me to make this code work or suggest and show a way so that I can select the information.

Thanks in advance.

1 answer

0


Good evening, settled as follows.

JSP

            <div class="form-group row">
            <label for="exampleFormControlSelect1" class="col-sm-2 col-form-label">Descrição</label>
            <div class="col-sm-10">
                <div class="form-row align-items-center">
                    <div class="col-sm-12">
                        <label class="mr-sm-2 sr-only" for="descricao">Preference</label>

                        <select class="form-control" name="idGrupoPeca" required>
                            <option value="">Selecione a descrição</option>
                            <c:forEach var="todasGrupoPeca" items="${grupoPeca}">
                                <option value="${todasGrupoPeca.idGrupoPeca}"
                                    <c:out value="${todasGrupoPeca.descricao}"> </c:out>>
                                    ${todasGrupoPeca.descricao}
                                </option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
            </div>
        </div>

Controller that receives the id of the selected option

public void adicionarPeca(String codigo, int idGrupoPeca, int idPrateleira, int idCelula) {
    System.out.println("Chamou o adicionar peca");

    System.out.printf("Grupo Peça -->" + idGrupoPeca);


}

Browser other questions tagged

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