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

标签 java jsp servlets jena

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

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

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,我想在其中显示表中的所有值,因此我执行了以下操作:

<%@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("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;

关于java - 无法通过 JSP 中的列表获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29215338/

相关文章:

java - JSTL:从自定义标签获取变量

apache - apache tomcat 的重写规则

jsp - struts2 url 到默认 namespace 图像

java - 提交 Struts2 迭代器索引作为映射键不起作用

java - 如何使用Servlet验证JSP并保留输入数据?

java - Tomcat 服务器启动失败

java - 在 doPost 方法中保存 request.inputstream 数据

java - 查找 JUnit TestCase 中测试方法的数量

java - 无法通过过滤器导航到jsp页面

c# - Android Java 到 Mono C# - MapView.LayoutParams