Reputation: 3271
I am trying to print a list into a table using java and html, but I am not able to do it. I am using servlets and JSP.
I have the following code:
This is the 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;
}
I also have a 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;
}
}
With the constructor and getters and setters for each variable.
And the jsp file:
<%@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>
And I also have an other class where I save the results in a list.
I would like to get something like this:
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
On the contrary, what I actually have is all the values in a same row, and I don't know how to print it properly.
Any idea?
Upvotes: 0
Views: 1833
Reputation: 2894
You should add some style to the page, something like this:
<style>
table, td, th {
border: 1px solid black;
}
table {
width: 100%;
}
th {
height: 50px;
}
</style>
to get rid of all those FACE,SIZE,BORDER ,etc.
Also you are messing with tags.
The following example should clear your ideas, hopefully:
<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>
EDIT:
your for-loop should look like this:
<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>
EDIT:
your Dao method should look similar to this:
...
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;
Upvotes: 1