无法通过 JSP 中的列表获取结果

Unable to get results over a list in a JSP

我在 JSP 中打印列表时遇到问题。我正在使用 Literal 和 RDFNode,就像我正在使用 RDF 一样。

我有 class 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;
}

对于 getter 和 setter 等等。

我也有这个方法:

 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()) {

            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;
}

我从数据库中获取所需信息并将其保存在列表中的位置。 我还有 JSP,我想在其中显示 table 中的所有值,所以我执行了以下操作:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>name: <c:out value=${receipt.nombreReceta}/></td>
        <td>Cant: <c:out value=${receipt.catidadIngredientes}/></td>
        <td>List: <c:out value=${receipt.ingredientes}/></td>
        <td>Preparation: <c:out value=${receipt.modoPreparacion}/></td>
        <td>Calories: <c:out value=${receipt.calorias}/></td>
        <td>Time: <c:out value=${receipt.tiempo}/></td>
        <td>Dificult: <c:o<ut value=${receipt.dificultad}/></td>
    </tr>
</c:forEach>
</table>
    </form>
</body>
</html>

问题是我尝试打印所有这些值的地方,我只得到一个空 space:

我不知道我做错了什么。我已经有几天遇到这个问题了,我无法找出问题所在。有什么帮助吗?

编辑: protected void processRequest(HttpServletRequest 请求,HttpServletResponse 响应) 抛出 ServletException,IOException { HttpSession 会话 = request.getSession(); 列出 lIngredients = (List) session.getAttribute("Ingredientes");

    response.setContentType("text/html;charset=UTF-8");
    if (lIngredients == null) {
       lIngredients = new ArrayList<>();
       session.setAttribute("Ingredientes", lIngredients);
    }
    String ingrediente = request.getParameter("Ingredientes");
    String cal = request.getParameter("Calorias");
    String tiempoMaximo = request.getParameter("TiempoMax");
    String tiempoMinimo = request.getParameter("TiempoMin");

    String action = request.getParameter("action"); //elegimos a qué pantalla pasar en función de la acción que nos llegue de la interfaz

    if ("Buscar todas las recetas".equalsIgnoreCase(action)) {
        request.setAttribute("AllReceipes", RecetaDao.getAllReceipes());
        request.getRequestDispatcher("receipes.jsp").forward(request, response);
    }else if ("Buscar por ingredientes".equalsIgnoreCase(action)){
              lIngredients.add(ingrediente);
              request.setAttribute("AllIngredients", RecetaDao.getSomeReceipes(lIngredients));
              request.getRequestDispatcher("perIngredient.jsp").forward(request, response);
    }else if ("Agregar ingrediente".equalsIgnoreCase(action)){
             lIngredients.add(ingrediente);
             request.getRequestDispatcher("option.jsp").forward(request, response);
             ingrediente = request.getParameter("Ingredientes");
             action = request.getParameter("action");
    }else if ("Buscar por calorias".equalsIgnoreCase(action)){
                request.setAttribute("AllIngredients", RecetaDao.getReceipesCalories(cal));
                request.getRequestDispatcher("perIngredient.jsp").forward(request, response);
    }else if ("Buscar por tiempo de preparacion".equalsIgnoreCase(action)){
                request.setAttribute("AllReceipes", RecetaDao.getReceipesTime());
                request.getRequestDispatcher("receipes.jsp").forward(request, response);
    }else if ("Buscar por tiempo maximo de preparacion".equalsIgnoreCase(action)){
                request.setAttribute("AllReceipes", RecetaDao.getReceipesMaxTime(tiempoMaximo));
                request.getRequestDispatcher("receipes.jsp").forward(request, response);
    }else if ("Buscar por tiempo minimo de preparacion".equalsIgnoreCase(action)){
                request.setAttribute("AllReceipes", RecetaDao.getReceipesMinTime(tiempoMinimo));
                request.getRequestDispatcher("receipes.jsp").forward(request, response);
    }else if ("Buscar por tiempo acotado de preparacion".equalsIgnoreCase(action)){
                request.setAttribute("AllReceipes", RecetaDao.getReceipesBetweenMinMax(tiempoMinimo, tiempoMaximo));
                request.getRequestDispatcher("receipes.jsp").forward(request, response);
    }else if ("Buscar recetas por dificultad".equalsIgnoreCase(action)){
                request.setAttribute("AllReceipes", RecetaDao.getReceipesDifficulty());
                request.getRequestDispatcher("receipes.jsp").forward(request, response);
    }else if ("Buscar recetas de dificultad baja y media".equalsIgnoreCase(action)){
                request.setAttribute("AllReceipes", RecetaDao.getReceipesEasyMediumDif());
                request.getRequestDispatcher("receipes.jsp").forward(request, response);
    }else if ("Buscar recetas de dificultad media y alta".equalsIgnoreCase(action)){
                request.setAttribute("AllReceipes", RecetaDao.getReceipesMediumHardDif());
                request.getRequestDispatcher("receipes.jsp").forward(request, response);
    }
}

您在代码中遗漏的几件事:

最重要的JSTL标签库:

Add <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> in your JSP.

成员变量拼写错误:

<td>Cant: <c:out value=${receipt.catidadIngredientes}/></td>
isn't matching ur member variable cantidadIngredientes

@Column
private Literal cantidadIngredientes;