1

I am writing an Java web application that fetches RDF data from URIs and stores them in a Jena Model object. Currently the models contain ordinary triples, some Blank Nodes, no inference. (That is described here already 1). Using Sesame would be an option, too.

After data collection, the application displays some of the collected information in a specified order. It would be necessary to build filters on the model using predicates only and filters specifiying subject & predicate (as we will collect data from several sources); in each case, the object-literal or URI would be displayed.

What would be the best way to generically access the model's contents from a JSP-View (without writing custom tags for each function)?

The JSP-View would look like this:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
    <head></head>
    <body>
        <img src="${modelBean.model.depictionPred}"/>
        Name: <c:out value="${modelBean.model.namePred}"/><br/>
        Life dates: <c:out value="${modelBean.model.birthPred}"/>-<c:out value="${modelBean.model.deathPred}"/><br/>
        Links: <a href="${modelBean.model.linkPred}">Linktext</a><br/>
        ...
    </body>
</html>

Jena-Servlet to collect data:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            ModelBean modelBean = new ModelBean();
            Model model = ModelFactory.createDefaultModel();
            model.read("http://dbpedia.org/resource/Ludwig_van_Beethoven");
            modelBean.setModel(model);
            request.setAttribute("modelBean", modelBean);
            RequestDispatcher rd = request.getRequestDispatcher("/view.jsp");
            rd.forward(request, response);
        }

Sesame-Variant of the Servlet:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ModelBean modelBean = new ModelBean();

        URL url = new URL("http://dbpedia.org/resource/Ludwig_van_Beethoven");
        URLConnection conn = url.openConnection();
        conn.addRequestProperty("Accept", RDFFormat.TURTLE.getDefaultMIMEType());
        InputStream is = conn.getInputStream();
        try {
           Model model = Rio.parse(is, url.toString(), RDFFormat.forMIMEType(conn.getContentType()));
        } catch (RDFParseException ex) {
           Logger.getLogger(ModelViewTester.class.getName()).log(Level.SEVERE, null, ex);
        } 
        finally {
           is.close(); 
        }
        modelBean.setModel(model);
        request.setAttribute("modelBean", modelBean);
        RequestDispatcher rd = request.getRequestDispatcher("/view.jsp");
        rd.forward(request, response);
    }
Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
Andreas
  • 303
  • 1
  • 17
  • 1
    Well, as a `Model` in Sesame is simply a Java Collection, you could iterate over it from JSP like any other `Set`, `List`, or `Map`. You will need to provide a bit more detail for this question to be answerable though. A code example that illustrates what you're trying to achieve would be useful. – Jeen Broekstra Feb 14 '14 at 21:47
  • Thanks for the advice. I now added a code example implementing this. Now the question would be how to actually access the Literals in the Graph (My example simply prints the contents via toString()). E.g. How could I access the name of Beethoven via in the JSP? – Andreas Feb 15 '14 at 07:11
  • Now I've added a Sesame example, too. – Andreas Feb 16 '14 at 07:04
  • Ah, I think I see what you're getting at. Does this help: http://stackoverflow.com/questions/7121303/how-to-call-parameterized-method-from-jsp-using-jstl ? – Jeen Broekstra Feb 16 '14 at 20:13
  • Thanks, Jeen; yes, passing parameters to bean methods is exactly what I was missing. If you put this into an answer; I'll accept it. Thanks also for the edit - my old Sesame code felt a bit lengthy to me, too. – Andreas Feb 16 '14 at 20:55

1 Answers1

0

Apart from the fact that something may be possible by providing parameters to calls on bean methods (see How to call parameterized method from JSP using JSTL/EL ) I'm afraid you'll have to either create custom tags, or you need to rethink your access strategy.

For example, rather than putting the entire RDF Model into your response, you could do the extraction of relevant data at the servlet-side, and just put the relevant properties that you want to display in your view response, as separate bean properties.

Community
  • 1
  • 1
Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73