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

时间:2015-03-23 16:26:31

标签: java jsp servlets jena

我在JSP中打印列表时遇到问题。我正在使用Literal和RDFNode,因为我正在使用RDF。

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

依赖于吸气剂和制定者。

我也有这种方法:

 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,我希望在表中显示所有值,所以我做了以下内容:

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

问题在于我尝试打印所有这些值,我只是得到一个空白区域:

enter image description here

我不知道我做错了什么。我已经有几天这个问题,我不知道什么是错的。有什么帮助吗?

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

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

1 个答案:

答案 0 :(得分:3)

您在代码中遗漏的内容很少:

最重要的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;