在html(servlet/jsp)中打印jena结果集

发布于 2024-09-04 20:46:16 字数 367 浏览 4 评论 0原文

我正在使用 servlet 来操作本体。我得到了 SPARQL 查询的结果,我想在 JSP (Servlet) 中显示(打印)该结果。

以下代码段可用于在控制台中打印结果。

com.hp.hpl.jena.query.Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query,model);
com.hp.hpl.jena.query.ResultSet rs = qe.execSelect();
ResultSetFormatter.out(System.out, rs);

有什么想法吗?

I'm using servlet for manipulating ontology. I got the result of my SPARQL query and I want to display(print) that result in JSP (Servlet).

Following code segment can be used to print the result in console.

com.hp.hpl.jena.query.Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query,model);
com.hp.hpl.jena.query.ResultSet rs = qe.execSelect();
ResultSetFormatter.out(System.out, rs);

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

关于从前 2024-09-11 20:46:16

我不做 Jena,但基本上您希望迭代 com.hp.hpl.jena.query.ResultSet 并将信息映射到 List > 其中 RowObject 是您自己的模型类,它表示您想要在 HTML 表中显示的单行。映射后,将List放入请求范围并将请求转发到JSP。

List<RowObject> results = getItSomeHow();
request.setAttribute("results", results); // Will be available as ${results} in JSP
request.getRequestDispatcher("page.jsp").forward(request, response);

然后在 JSP 中,使用 JSTL c: forEach 迭代 List,打印 HTML 表格。

<table>
    <c:forEach items="${results}" var="rowObject">
        <tr>
            <td>${rowObject.someProperty}</td>
            <td>${rowObject.anotherProperty}</td>
            ...
        </tr>
    </c:forEach>
</table>

根据您的其他答案更新,以下是如何基于Jena的ResultSet创建List

List<RowObject> results = new ArrayList<RowObject>();
while (rs.hasNext()) {
    RowObject result = new RowObject();
    QuerySolution binding = result.nextSolution();
    result.setInd(binding.get("ind"));
    result.setSomethingElse(binding.get("something_else"));
    // ...
    results.add(result);
}

并显示如下:

...
<td>${rowObject.ind}</td>
<td>${rowObject.somethingElse}</td>
...

I don't do Jena, but basically you would like to iterate over the com.hp.hpl.jena.query.ResultSet and map the information into a List<RowObject> where RowObject is your own model class which represents a single row you'd like to display in a HTML table. After mapping, put the List<RowObject> in the request scope and forward the request to a JSP.

List<RowObject> results = getItSomeHow();
request.setAttribute("results", results); // Will be available as ${results} in JSP
request.getRequestDispatcher("page.jsp").forward(request, response);

Then in JSP, use JSTL c:forEach to iterate over the List<RowObject>, printing a HTML table.

<table>
    <c:forEach items="${results}" var="rowObject">
        <tr>
            <td>${rowObject.someProperty}</td>
            <td>${rowObject.anotherProperty}</td>
            ...
        </tr>
    </c:forEach>
</table>

Update based on your other answer, here's how you could create a List<RowObject> based on the Jena's ResultSet:

List<RowObject> results = new ArrayList<RowObject>();
while (rs.hasNext()) {
    RowObject result = new RowObject();
    QuerySolution binding = result.nextSolution();
    result.setInd(binding.get("ind"));
    result.setSomethingElse(binding.get("something_else"));
    // ...
    results.add(result);
}

And display it as follows:

...
<td>${rowObject.ind}</td>
<td>${rowObject.somethingElse}</td>
...
给妤﹃绝世温柔 2024-09-11 20:46:16

此代码段将转到您的 Servlet,或者您也可以使用单独的 java 类来实现它。

com.hp.hpl.jena.query.Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query,model);
com.hp.hpl.jena.query.ResultSet rs = qe.execSelect();

while(rs.hasNext()){

 QuerySolution binding = rs.nextSolution();                     
 System.out.println(binding.get("ind")); 
}

注意:

“ind”是您在 SPARQL 查询 SELECT 子句中引用的变量。

谢谢你!

This code segment is going to your Servlet or You can implement that using seperate java class too.

com.hp.hpl.jena.query.Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query,model);
com.hp.hpl.jena.query.ResultSet rs = qe.execSelect();

while(rs.hasNext()){

 QuerySolution binding = rs.nextSolution();                     
 System.out.println(binding.get("ind")); 
}

Note:

"ind" is a variable that you are refering in SPARQL query SELECT clause.

Thank you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文