0

I have one method, this method returnes a list of items, inside java bean it works now I want to print the output of the method inside jsp, but I searched a lot couldn't find something useful, if someone can help me I really appreciated this is my method for print a list

public static List printDirect() {
    List returned_list = new ArrayList ();

    //StringBuilder text=new  StringBuilder();
    manager = OntologyManagement.ontology.getOWLOntologyManager();
    factory = manager.getOWLDataFactory();

    reasonerFactory = new StructuralReasonerFactory();
    progressMonitor = new ConsoleProgressMonitor();
    config = new SimpleConfiguration(progressMonitor);
    reasoner = reasonerFactory.createReasoner(ontology, config);
    // Ask the reasoner to do all the necessary work now
    reasoner.precomputeInferences();
    OWLClass thing = factory.getOWLThing();
    NodeSet<OWLClass> subClses = reasoner.getSubClasses(thing, true);
    Set<OWLClass> clses = subClses.getFlattened();
    System.out.println("Subclasses of owl:thing =  ");
    for (OWLClass cls : clses) {
        String row = cls.toString();
        String[] split = row.split("#");
        String word = split[1].substring(0, (split[1].length() -1));
        returned_list.add(word);
        System.out.println("    " + word);
    }
    return returned_list;
}
mike_m
  • 1,526
  • 4
  • 14
  • 19
Dosti
  • 63
  • 1
  • 8

4 Answers4

0

JSP alone has nothing for tests or iterations, so you must use JSTL.

That being said, your question has already an answer in How to iterate an ArrayList inside a HashMap using JSTL?.

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

You may find this link useful,

how to Print arraylist in jsp using java method

By using scriptlet you can iterate the list to print. But that's not a good practice. Use JSTL instead to make your job much more easier.

Community
  • 1
  • 1
Rajaah
  • 51
  • 1
  • 2
  • 11
0

So you need to do a few things to make this work.

First, you need to load the list into a variable in your JSP. I don't know the package or name of your class, so swap out "com.yourpath" for your package name and "YourClass" for your class's name.

<%
    java.util.List list = com.yourpath.YourClass.printDirect();
%>

Next, you need to add the "list" to the pageContext so that you can loop on it using JSTL:

<%
    pageContext.setAttribute("list", list);
%>

Finally, you can iterate over the list easily, using JSTL:

List contents:<br>
<c:forEach var="element" items="${list}">
    ${element}<br>
</c:forEach>

You should consider using @page at the top of your page in order to import your classes so that the above code doesn't look so messy:

<%@ page import="com.yourpath.YourClass" %>
<%@ page import="java.util.List" %>

So putting it altogether it would look like:

<%@ page import="com.yourpath.YourClass" %>
<%@ page import="java.util.List" %>

<%
    List list = YourClass.printDirect();
    pageContext.setAttribute("list", list);
%>

List contents:<br>
<c:forEach var="element" items="${list}">
    ${element}<br>
</c:forEach>

The following should work without your class to give you an easy test to see if you are having any issues with your class or just the technique in general:

<%
    java.util.Map map = java.lang.System.getenv();

    pageContext.setAttribute("map", map);
%>

<c:forEach var="element" items="${map}">
    ${element.key} = ${element.value}<br>
</c:forEach>

You can, of course, skip creating the variable and load the contents of printDirect() directly into the pageContext like this, saving you a line of code:

<%
    pageContext.setAttribute("list", com.yourpath.YourClass.printDirect());
%>
alfreema
  • 1,308
  • 14
  • 26
0

First get the List object in jsp like this

<%
    List list = fullQualifiedClassName.printDirect();
%>

Then iterate over the listObject using JSTL

<c:forEach items="${list}" var="item">
    ${item}<br>
</c:forEach>

Dont forget to add this along with the necessary imports

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

You can even use simple Scriptlets to iterate over the List(But it is not Recommended)

<% for (int i=0;i<list.size();i++)
  {
     out.println(list.get(i));  
  } 
%>

or

<%  Iterator iterator = list.iterator();
    while (iterator.hasNext()) {    
        out.println(iterator.next());
    }
 %>
SparkOn
  • 8,806
  • 4
  • 29
  • 34