在 jsp 中使用 java 和 html 打印列表

Print a list using java and html in a jsp

我正在尝试使用 java 和 html 将列表打印到 table,但我做不到。我正在使用 servlet 和 JSP.

我有以下代码:

这是 servlet:

    public static List<Receta> getAllReceipes() {
    List<Receta> list = new ArrayList<>();

    String log4jConfPath = "C:/Users/Karen/workspace/Jena/src/Tutorial/log4j.properties";
    PropertyConfigurator.configure(log4jConfPath);
    try {
        //opening owl file
        Model model = ModelFactory.createDefaultModel();
        model.read(new FileInputStream("C:/Users/Karen/Desktop/Proyecto/bbdd.owl"), null, "TTL");
        //System.out.println(model);

        //create a new query
        String queryString
                = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
                + " PREFIX owl: <http://www.w3.org/2002/07/owl#>"
                + " PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
                + " PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
                + " PREFIX rec:<http://www.receta.org#>"
                + " SELECT ?r ?cal ?tiempo ?dif (COUNT (distinct ?Ingrediente) as ?cantIng) (GROUP_CONCAT(DISTINCT ?modoPreparacion) as ?Preparacion) (GROUP_CONCAT(DISTINCT ?listaIngredientes) as ?listaIng)   "
                + "  WHERE { "
                + "  ?x rdf:type rec:Receta."
                + "  ?x rdfs:label ?r."
                + "  ?x rec:Ingrediente ?Ingrediente."
                + "  ?x rec:modoPreparacion ?modoPreparacion."
                + "  ?x rec:listaIngredientes ?listaIngredientes."
                + "  ?x rec:Calorias ?cal."
                + "  ?x rec:tiempoPreparacion ?tiempo."
                + "  ?x rec:dificultad ?dif."
                + "  } "
                + " GROUP BY ?r ?cal ?tiempo ?dif";

        com.hp.hpl.jena.query.Query q = QueryFactory.create(queryString);
        //execute the query and obtain results
        QueryExecution qe = QueryExecutionFactory.create(q, model);
        ResultSet results = qe.execSelect();

        //print query results
        while (results.hasNext()) {

            //System.out.println(results.getResourceModel());
            //ResultSetFormatter.out(System.out,results, q);
            QuerySolution qs = results.next();


            Receta rec = new Receta();

            rec.setNombreReceta(qs.getLiteral("r"));
            rec.setCantidadIngredientes(qs.getLiteral("cantIng"));
            rec.setIngredientes(qs.get("listaIng"));
            rec.setModoPreparacion(qs.get("Preparacion"));
            rec.setTiempo(qs.getLiteral("tiempo"));
            rec.setCalorias(qs.getLiteral("cal"));
            rec.setDificultad(qs.getLiteral("dif"));

            list.add(rec);

            System.out.print(rec.getNombreReceta());
        }

    } catch (java.lang.NullPointerException e) {
        System.out.println(e);
    } catch (Exception e) {
        System.out.println("Query Failed !");
    }
    return list;
}

我也有一个 Receta class:

@Table
public class Receta implements Serializable{
@Id
@Column
private Literal nombreReceta;
@Column
private Literal cantidadIngredientes;
@Column
private RDFNode ingredientes;
@Column
private RDFNode modoPreparacion;
@Column
private Literal dificultad;
@Column
private Literal tiempo;
@Column
private Literal calorias;

public Receta(){}

public Receta(Literal nombreReceta, Literal catidadIngredientes, RDFNode ingredientes, RDFNode modoPreparacion, Literal dificultad, Literal tiempo, Literal calorias) {
    this.nombreReceta = nombreReceta;
    this.cantidadIngredientes = catidadIngredientes;
    this.ingredientes = ingredientes;
    this.modoPreparacion = modoPreparacion;
    this.dificultad = dificultad;
    this.tiempo = tiempo;
    this.calorias = calorias;
}

public Literal getNombreReceta() {
    return nombreReceta;
}

public void setNombreReceta(Literal nombreReceta) {
    this.nombreReceta = nombreReceta;
}

/**
 * @return the catidadIngredientes
 */
public Literal getCantidadIngredientes() {
    return cantidadIngredientes;
}

/**
 * @param catidadIngredientes the catidadIngredientes to set
 */
public void setCantidadIngredientes(Literal cantidadIngredientes) {
    this.cantidadIngredientes = cantidadIngredientes;
}

/**
 * @return the ingredientes
 */
public RDFNode getIngredientes() {
    return ingredientes;
}

/**
 * @param ingredientes the ingredientes to set
 */
public void setIngredientes(RDFNode ingredientes) {
    this.ingredientes = ingredientes;
}

/**
 * @return the modoPreparacion
 */
public RDFNode getModoPreparacion() {
    return modoPreparacion;
}

/**
 * @param modoPreparacion the modoPreparacion to set
 */
public void setModoPreparacion(RDFNode modoPreparacion) {
    this.modoPreparacion = modoPreparacion;
}

/**
 * @return the dificultad
 */
public Literal getDificultad() {
    return dificultad;
}

/**
 * @param dificultad the dificultad to set
 */
public void setDificultad(Literal dificultad) {
    this.dificultad = dificultad;
}

/**
 * @return the tiempo
 */
public Literal getTiempo() {
    return tiempo;
}

/**
 * @param tiempo the tiempo to set
 */
public void setTiempo(Literal tiempo) {
    this.tiempo = tiempo;
}

/**
 * @return the calorias
 */
public Literal getCalorias() {
    return calorias;
}

/**
 * @param calorias the calorias to set
 */
public void setCalorias(Literal calorias) {
    this.calorias = calorias;
}

}

每个变量都有构造函数和 getter 和 setter。

和 jsp 文件:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <form action="./BuscarRecetaServlet" method="POST">     
    <table border="1">
        <tr>
            <th><FONT FACE="Times New Roman" SIZE=3> NombreReceta </th>
            <th><FONT FACE= "Times New Roman" SIZE=3> CantidadIngredientes </th>
            <th><FONT FACE= "Times New Roman" SIZE=3> Ingredientes </th>
            <th><FONT FACE= "Times New Roman" SIZE=3> ModoPreparacion </th>
            <th><FONT FACE= "Times New Roman" SIZE=3> Calorias </th>
            <th><FONT FACE= "Times New Roman" SIZE=3> Tiempo </th>
            <th><FONT FACE= "Times New Roman" SIZE=3> Dificultad </th>
        </tr>
  <c:forEach items="${AllReceipes}" var="receipt">        
    <tr>
        <td><c:out value="${receipt.nombreReceta}"/></td>
        <td><c:out value="${receipt.cantidadIngredientes}"/></td>
        <td><c:out value="${receipt.ingredientes}"/></td>
        <td><c:out value="${receipt.modoPreparacion}"/></td>
        <td><c:out value="${receipt.calorias}"/></td>
        <td><c:out value="${receipt.tiempo}"/></td>
        <td><c:out value="${receipt.dificultad}"/></td>
    </tr>
</c:forEach>
</table>
    </form>
</body>
</html>

我还有另一个 class,我将结果保存在列表中。

我想得到这样的东西:

NombreReceta  CantidadIngredientes Ingredientes  ModoPreparacion  Calorias  Tiempo Dificultad
ReceipeName1   10                  Tomato,egg... Clean the tomato..  70        20  3
ReceipeName2   13                  Tomato,egg... Clean the tomato..  40        10  2

相反,我实际上拥有的是同一行中的所有值,我不知道如何正确打印。

有什么想法吗?

您应该为页面添加一些样式, 像这样:

<style>
table, td, th {
    border: 1px solid black;
}

table {
    width: 100%;
}

th {
    height: 50px;
}
</style>

摆脱所有那些FACE、SIZE、BORDER等

你也搞乱了标签。
希望以下示例应该清楚您的想法:

<table>
  <tr>
    <th>table header1</th>
    <th>table header2</th>
    <th>table header3</th>
  </tr>
  <tr>
    <td>column value</td>
    <td>column value<</td>
    <td>column value<</td>
  </tr>
  <tr>
    <td>column value<</td>
    <td>column value<</td>
    <td>column value<</td>
 </tr>
</table>

编辑:

您的 for 循环应该如下所示:

<c:forEach items="${AllReceipes}" var="receipt">
        <tr>
            <td>${receipt.nombreReceta}</td>
            <td>${receipt.catidadIngredientes}</td>
            <td>${receipt.ingredientes}</td>
            <td>${receipt.modoPreparacion}</td>
            <td>${receipt.calorias}</td>
            <td>${receipt.tiempo}</td>
            <td>${receipt.dificultad}</td>
        </tr>
    </c:forEach>

编辑:

您的 Dao 方法应该与此类似:

 ...
 List<Receta> recetas = new ArrayList<Receta>();

   query = "SELECT * FROM  RecetaTable";
        stmt = dbCon.createStatement();
        rs = stmt.executeQuery(query);

        while (rs.next())
        {
            Receta rec = new Receta();
            rec.setNombreReceta(rs.getString("nombreReceta"));
            // and so on
            recetas.add(rec);
        }

        return recetas;