簡體   English   中英

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

[英]Print a list using java and html in a jsp

我正在嘗試使用java和html將列表打印到表中,但是我無法做到這一點。 我正在使用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課:

@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>

我還有另一個班級,將結果保存在列表中。

我想得到這樣的東西:

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;

暫無
暫無

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

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