簡體   English   中英

刪除JSF dataTable中數據庫的行

[英]Delete rows of a database in JSF dataTable

當我運行並按下按鈕“刪除”時,頁面向我顯示此錯誤:

無法找到具有操作結果'[entities.Categorias [categoria = Carro]]'的動作'#{registarVendedor.deleteCtg(c)}'的,具有from-view-id'/ApagarCategoria.xhtml'的匹配導航案例。

調節器

在RegistoVendedor.java中,該函數是公共List <Categorias> deleteCtg(Categorias ct)

package Controller;


public class RegistoVendedor {

@EJB
RegistoBean registarVendedor;

String contacto;
String password;

Categorias categoria = new Categorias();


List<Categorias> categoriasList = new ArrayList<>();

public List<Categorias> getCategoriasList() {
    return categoriasList;
}

public void setCategoriasList(List<Categorias> categoriasList) {
    this.categoriasList = categoriasList;
}

public List<Categorias> deleteCtg(Categorias ct) {        
    categoriasList = registarVendedor.removeCtg(ct);
    return categoriasList;
} 
}

在RegistoBean.java中,該函數是公共List <Categorias> removeCtg(Categorias ct)

@Stateless
public class RegistoBean {

@PersistenceContext
EntityManager em;

public List<Categorias> removeCtg(Categorias ct){
    em.createNamedQuery("Categorias.removeCategoria");
    return getCtg();
}

public List<Categorias> getCtg(){
    return (List<Categorias>) em.createNamedQuery("Categorias.findAll").getResultList();
}
}

Categorias.java數據庫

@Entity
@Table(name = "CATEGORIAS")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Categorias.findAll", query = "SELECT c FROM Categorias c")
, @NamedQuery(name = "Categorias.findByCategoria", query = "SELECT c FROM Categorias c WHERE c.categoria = :categoria")
, @NamedQuery(name = "Categorias.removeCategoria", query = "DELETE FROM Categorias c WHERE c.categoria = :categoria")})
public class Categorias implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 200)
@Column(name = "CATEGORIA")
private String categoria;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "categoriaCategorias")
private Collection<Produtos> produtosCollection;

public Categorias() {
}

public Categorias(String categoria) {
    this.categoria = categoria;
}

public String getCategoria() {
    return categoria;
}

public void setCategoria(String categoria) {
    this.categoria = categoria;
}

@XmlTransient
public Collection<Produtos> getProdutosCollection() {
    return produtosCollection;
}

public void setProdutosCollection(Collection<Produtos> produtosCollection) {
    this.produtosCollection = produtosCollection;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (categoria != null ? categoria.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Categorias)) {
        return false;
    }
    Categorias other = (Categorias) object;
    if ((this.categoria == null && other.categoria != null) || (this.categoria != null && !this.categoria.equals(other.categoria))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "entities.Categorias[ categoria=" + categoria + " ]";
}

}

ApagarCategoria.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://xmlns.jcp.org/jsf/html"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Apagar Categoria</title>
</h:head>
<h:body>
    <br></br>
    <div align="center" >
        <h1 align="center">Lista de Categorias</h1>

        <div align ="center">
<h:form>
            <h:dataTable value = "#{registarVendedor.listarCategorias()}" var = "c" 
                         styleClass = "authorTable" 
                         headerClass = "authorTableHeader" 
                         rowClasses = "authorTableOddRow,authorTableEvenRow"
                         width = "600">

                <h:column><f:facet name = "header"> Categoria </f:facet>
                        #{c.categoria}
                    <f:facet name = "header2"> </f:facet> 
                </h:column>
                <h:column>

                    <p> <h:commandButton value="Delete" class="button1" action="#{registarVendedor.deleteCtg(c)}">
                                    <f:setPropertyActionListener target="#{registarVendedor.categoria}" value="#{c}"/> 
                                </h:commandButton></p>  

                </h:column>

            </h:dataTable>
</h:form>
        </div>
        <h:form >
            <p> <h:commandButton value="Voltar" class="button1" action="MenuVendedor"></h:commandButton></p>
        </h:form>
    </div>
</h:body>

我知道同樣有疑問,但我已經嘗試過解決方案,但沒有用

錯誤是

找不到匹配的導航

JSF使用返回值“#{registarVendedor.deleteCtg(c)}”來確定接下來應該查看哪個視圖。 但是您返回一個列表,所以jsf迷路了。 您需要返回Stringvoid

如果這樣做,它將起作用

public void deleteCtg(Categorias ct) {        
     categoriasList = registarVendedor.removeCtg(ct);
} 

您真的需要那個清單嗎? 您可以使用getCategoriasList()獲得它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM