0

I'm making a small application and I have a few classes that have nothing in common (Product, Country, Customer etc) and my goal is to have a jsp file that can display in an html table the object (or list of object it's given) because I currently have a jsp file for every class (products.jsp, country.jsp etc).

I've been thinking about this the whole day and I can't figure how to do it.

Any suggestion ?

edit: trying to be a bit more clear and specific: I have a "target" argument that contains the type of data that the user want to visualize ("product", "country" etc). The servlet will then load in a variable a list of Products/countries etc and send it to the jsp. I want that jsp file to display whatever he just received in a table. The question is how can I do that ? Currently my jsp fileS are doing it "manually" using the getters of each type of object, how can I make that generic ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Spare
  • 82
  • 8

2 Answers2

1

I'm not sure I understand correctly the problem, anyway these are my suggestions.

If you decide the column of the table, then you can create a wrapper object having an attribute for each field of the table, and "transform" each different object you have (Product, Country...) into this wrapper and then create a list of these wrapper object to show in the table.

Otherwise put all the objects as they are in a list and then do as much if as needed to display the information in the table.

I would choose the first way.

Jkike
  • 807
  • 6
  • 12
  • I edited my post, made it more specific and hopefully clearer ! – Spare Jul 03 '15 at 14:53
  • I whould use a wrapper object, or, you may try to convert the objects into json elements and then the jsp will just list the generic json elements. Json Elements are key-value simple "objects". – Jkike Jul 07 '15 at 15:15
0

After a while I found a solution to my problem. I implemented the following method to a class all of my data classes inherit from.

public ArrayList<Pair<String, String>> toPairs() {

    ArrayList<Pair<String, String>> list = new ArrayList<Pair<String, String>>();
    try {
        Class<?> objClass = this.getClass();
        Field[] fields = objClass.getDeclaredFields();
        for(Field field : fields) {
            field.setAccessible(true);
            String value = field.get(this).toString();
            String name = field.getName();

            Pair<String, String> pair = new Pair<String, String>(name, value);
            list.add(pair);
        }
        return list;
    } catch(Exception e) {
        System.out.println(e.getMessage());
        return null;
    }
}

It's probably not the most effective way to do it but it's how I ended up making it.

My servlet gets the data from the database and puts everything in Pair (from this answer). In the end, I pass a list of list of Pairs to my jsp file.

<table>
    <c:forEach items="${data}" var="lpair" varStatus="loop">
        <c:if test="${loop.first}">
            <tr>
                <c:forEach items="${lpair}" var="pair">
                    <th>${pair.getL()}</th>
                </c:forEach>
            </tr>
        </c:if>
        <tr>
            <c:forEach items="${lpair}" var="pair">
                <td>${pair.getR()}</td>
            </c:forEach>
        </tr>

    </c:forEach>
</table>

An example to understand it easily : if I want to display the countries the servlet will get them from the database and make each country a list of pairs, a Country object will become something like that :

"id":"*someid"
"short":"USA"
"name":"United States of America"

So the jsp file receive a list of countries that are stocked in lists themselves hence the double foreach.

This is my first attempt at an answer (even if it's my own question) so I hope it's understandable. Sorry for the possible languages mistake, English is not my mother tongue.

Community
  • 1
  • 1
Spare
  • 82
  • 8