【问题标题】:Delete rows of a database in JSF dataTable在 JSF dataTable 中删除数据库的行
【发布时间】:2018-11-09 15:35:54
【问题描述】:

当我运行并按下“删除”按钮时,页面显示此错误:

对于结果为“[entities.Categorias[ categoria=Carro ]]”的操作“#{registarVendedor.deleteCtg(c)}”,找不到匹配的导航案例与 from-view-id '/ApagarCategoria.xhtml'

控制器

在 RegistoVendedor.java 中,函数是 public List 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 中,函数是 public List 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>

我知道有类似的疑问,但我已经尝试了解决方案,但它不起作用

【问题讨论】:

  • 欢迎来到 Stack Overflow!这看起来比严格需要的代码更多。您能否进一步减少代码,直到在仍然遇到问题的情况下无法删除任何代码?
  • 下次缩小问题范围。 99.99% 的问题与 jsf 和 sql 无关。如果您尝试使用静态列表,则会遇到同样的问题。 Java(-se) 适用于基础 jdk 中的问题,因此您可以仅使用带有 main 的类来重现。如果你只用一个命令按钮和相同的方法调用完成了minimal reproducible example,你就会遇到同样的问题。所以也不与数据表相关。您的标题是针对您的功能性 问题,但通常(几乎总是)存在潜在的技术问题。专注于此,在标题中描述。干杯

标签: java sql jsf datatable xhtml


【解决方案1】:

错误是

找不到匹配的导航

JSF 使用返回值“#{registarVendedor.deleteCtg(c)}”来确定接下来应该查看哪个视图。但是你返回了一个列表,所以 jsf 迷路了。您需要返回Stringvoid

如果你这样做,它会起作用

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

你真的需要这份清单吗?您可以通过getCategoriasList() 获取它。

【讨论】:

  • 谢谢,我改了代码,错误消失了,但没有从数据库中删除
猜你喜欢
  • 2012-12-01
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 2015-02-09
  • 2014-10-19
  • 2019-04-15
  • 2017-05-19
  • 2015-06-13
相关资源
最近更新 更多